Hadoop之MapReduce的Combiner解析

情到浓时终转凉″ 提交于 2020-01-30 07:07:19

Combiner是MapReduce程序中Mapper和Reducer之外的一种组件,其父类是Reducer。

Combiner和Reducer的区别在于运行的位置

(1)Combiner是在每一个MapTask所在的节点运行

(2)Reducer是接受全局所有的Mapper端的输出结果

注意:

(1)Combiner对每一个MapTask的输出进行局部汇总,减少网络传输量

(2)Combiner不能影响业务逻辑,Combiner输出KV应该和Reducer的输入KV类型对于起来

不适用Combiner的例子:

Mapper

3   5   7->(3+5+7)/3=5

2   6 ->(2+6)/2=4

Reducer

(3+5+7+2+6)/5=23/5  不等于(5+4)/2=9/2

自定义Combiner

(1)自定义一个Combiner继承Reducer,重写Reduce方法

public class WordcountCombiner extends Reducer<Text, IntWritable,Text,IntWritable> {

    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        IntWritable v = new IntWritable();
        // 1 汇总
        int sum = 0;
        for(IntWritable value :values){
            sum += value.get();
        }
        v.set(sum);
        // 2 写出
        context.write(key, v);
    }
}

(2)在Job驱动类中设置

job.setCombinerClass(WordcountCombiner.class);

 

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!