mapreduce之combiner函数

一个例子说明combiner的作用:hadoop允许用户针对map任务输出指定一个combiner,combiner函数的输出作为reduce的输入

(1)假设第一个map的输出如下:
(1950,0)//1950表示年份,0表示地方A的最高温度
(1950,20)
(1950,10)

(2)假设第二个map的输出如下:
(1950,25)//1950表示年份,0表示地方A的最高温度
(1950,15)

(3)reduce函数被调用,输入如下:
(1950,[0,20,10,25,15])

(4)如果是使用combiner函数找出每个map任务输出结果中的最高温度。如此一来,reduce的数据如下:
(1950,[20,25])

在job中加入combiner函数:

job.setConbinerClass(MaxTemperarureReducer.class);//在job中加入combiner函数

注:combiner的优点是可以减少map传给reduce的数据量

下面以wordcount为例:

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

	@Override
	protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {

		int count=0;
		for(IntWritable v: values){
			
			count += v.get();
		}
		context.write(key, new IntWritable(count));	
	}
}

增加这样一个combiner如果遇到重复率很高的数据就可以用类似字典树的方法压缩数据解决了,如果有10000个z,原来需要发送10000次<z,1>的数据给reduce,现在只需要发一个<z,10000>即可,大大减小了网络传输的数据量,但是使用combiner一定不能影响最终结果才行。

猜你喜欢

转载自blog.csdn.net/qq_35688140/article/details/84111972