如果job 设置了 combiner ,则job的每个map运行的数据会先进入combiner,然后再通过patitioner分发到reduce。通过combiner能减少reduce的计算、空间压力。其实combiner就是继承了Reducer类了一个子类,运行在map排序后的输出上。可以理解为,对每个map中的数据先做一次reduce。
下面是一个例子,很简单,不多说了。
public static class MyCombiner extends Reducer<Text , Text , Text , Text>{ @Override protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException { StringBuilder sb = new StringBuilder(); for(Text value : values){ sb.append(value.toString()).append(StrUtils.tab); } context.write(key, new Text(sb.toString().trim())); } }