Hadoop之MapReduce的Combiner解析

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);

发布了63 篇原创文章 · 获赞 2 · 访问量 2758

猜你喜欢

转载自blog.csdn.net/zuodaoyong/article/details/104109226
今日推荐