Hadoop Combiner类 简单测试

public static class MyReduce extends
		Reducer<Text, IntWritable, Text, IntWritable>{
		
		private static IntWritable num = new IntWritable();
		@Override
		public void reduce(Text key,Iterable <IntWritable> val,Context context
				)throws IOException,InterruptedException {
			int sum=0;
			
			for(IntWritable each : val){
				sum++;   //  这是第一种情况
				//sum += val.get();  //  这是第二种情况
		        // IntWritable.get(); 返回 IntWritable的int值
			}
			
			num.set(sum);  //返回值并非本对象
			context.write(key, num);
		}
	}

接下来就是主函数的区别

	job.setCombinerClass(MyReduce.class);  // 是否加入Combiner
对了,这是最简单的WordCount程序。

不加入Combiner时,两种情况答案正确。

加入后,只有第二种方法正确。

分析: 没有Combiner,map  -->   (word,(1,1,1,1))  -->  reduce  -->  (word,4)

             加入Combiner ,  map  -->  (word,(1,1,1,1))   -->(Combiner,仍在执行map的主机上) (word,(4))  --> reduce (word,(1))

            // 所以加入Combiner后,第一种数集合中有多少元素的方法就失效了,输出的所有单词数量均为1。

           因为这里Combiner和Reduce用相同的函数,即都是reduce( ); 所以reduce的后两个输出参数要和前两个参数类型一致。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值