您的位置:首页 > 运维架构

Hadoop Combiner类 简单测试

2015-12-17 16:43 513 查看
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的后两个输出参数要和前两个参数类型一致。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: