combiner与reducer的联系

combiner和reducer的父类都是reducer
combiner是在每一个maptask节点上运行
reducer是接收所有mapper输出的结果

combiner编程步骤如下

一、继承reducer类,重写reduce方法
二、在启动类设置combiner组件

  1. /**
  2. * 局部聚合
  3. * 在发送给reduce之前进行map端的单机局部聚合,减少IO网络传输
  4. * 如 A B C 三台机械
  5. * A机械 hello 1 hello 1 world 1 hello 1 ---> hello 3 world 1
  6. * B机械 world 1 world 1 world 1 hello 1 ---> hello 1 world 3
  7. * C机械 hello 1 world 1 hello 1 world 1 ---> hello 2 world 2
  8. */
  9. public class WordCountCombiner extends Reducer<Text, IntWritable, Text, IntWritable> {
  10. //与reducer代码一样
  11. @Override
  12. protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
  13. int count = 0;
  14. for(IntWritable v :values){
  15. count = count + v.get();
  16. }
  17. context.write(key, new IntWritable(count));
  18. }
  19. }
  20. 注意:在启动类中设置combiner组件
  21. //设置combiner组件, combiner不用重写,因为与reduce逻辑一样,直接复用即可
  22. //使用局部聚合不要影响业务结果,比如求平均值就不适合
  23. //job.setCombinerClass(WordCountCombiner.class);
  24. job.setCombinerClass(WordcountReducer.class);