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); } }
迭代遍历values,取出所有的value,都是1, 简单加和。
然后结果写入到context中。 注意,这里的context是Reducer包的Context。
时间: 2024-12-29 07:24:22