写一端mapperreduce代码
时间: 2023-10-19 21:09:56 浏览: 106
一个MapReduce简单程序示例
4星 · 用户满意度95%
以下是一个示例 MapReduce 代码:
Mapper:
```java
public class MapperClass 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:
```java
public class ReducerClass extends Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
context.write(key, new IntWritable(sum));
}
}
```
Driver:
```java
public class DriverClass {
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "word count");
job.setJarByClass(DriverClass.class);
job.setMapperClass(MapperClass.class);
job.setCombinerClass(ReducerClass.class);
job.setReducerClass(ReducerClass.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);
}
}
```
这段代码实现了一个简单的单词计数 MapReduce 任务。Mapper 读取输入文件中的每一行文本,将每个单词作为 key,值为 1,并输出到 Reducer 进行处理。Reducer 对相同单词的值进行累加,输出每个单词及其出现次数。Driver 则设置了输入输出路径等参数,并启动 MapReduce 任务。
阅读全文