hadoop mapreduce代码实现
时间: 2023-07-11 12:43:51 浏览: 108
Hadoop MapReduce是一种分布式计算框架,可以用于处理大规模数据集。下面是一个简单的Hadoop MapReduce代码示例:
- Mapper类
public class MyMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line);
while (tokenizer.hasMoreTokens()) {
word.set(tokenizer.nextToken());
context.write(word, one);
}
}
}
- Reducer类
public class MyReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
private IntWritable result = new IntWritable();
public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
result.set(sum);
context.write(key, result);
}
}
- Driver类
public class MyDriver {
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "word count");
job.setJarByClass(MyDriver.class);
job.setMapperClass(MyMapper.class);
job.setCombinerClass(MyReducer.class);
job.setReducerClass(MyReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
在这个示例中,Mapper类将输入数据按照单词进行分割,然后输出<单词,1>的键值对。Reducer类将相同的单词进行合并,并输出<单词,总数>的键值对。Driver类设置了MapReduce作业的输入和输出路径,并指定了Mapper和Reducer类。最后,启动作业并等待它的完成。
阅读全文
相关推荐


















