Reduce代码就是做加和统计,
package org.freebird.reducer;
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.mapreduce.Reducer.Context;
import org.apache.hadoop.mapreduce.Reducer;
public class LogReducer<Key> extends Reducer<Key, IntWritable, Key,IntWritable> {
private IntWritable result = new IntWritable();
public void reduce(Key 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);
}
}
然后结果写入到context中。 注意,这里的context是Reducer包的Context。
MapReduce 1.x 编程 系列三 Reduce阶段实现
原文地址:http://blog.csdn.net/csfreebird/article/details/39561885