hadoop 设置了reduce但是无法执行的bug

今天做mapreduce开发的时候,遇到个诡异的问题,设置了reduce方法,但是就是没有执行。

为了进一步验证reduce是否执行,特地在reduce方法里添加了一些提示信息的输出,查看后台task日志文件里面确实没有对应的打印内容,说明reduce没有执行。


hadoop版本:1.2.1

开发工具:eclipse


设计的Reducer方法如下:

public static class KPIBrowserIPReducer extends
Reducer<Text, IntWritable, Text, Text> {
private Text outvalue = new Text();


                public void reduce(Text key, Iterator<IntWritable> values,
Context context) throws IOException, InterruptedException {
int sum = 0;
while (values.hasNext()) {
sum = sum + 1;
}
outvalue.set(Integer.toString(sum));
System.err.println("key : " + key + "----outvalue : " + outvalue);
context.write(key, outvalue);
}

}


分析:

很有可能是reduce方法写错了。于是在KPIBrowserIPReducer 方法体里面右击重新生成reduce方法。

自动生成的reduce方法如下:

protected void reduce(
Text arg0,
java.lang.Iterable<IntWritable> arg1,
org.apache.hadoop.mapreduce.Reducer<Text, IntWritable, Text, Text>.Context arg2)
throws IOException, InterruptedException {
}

对比分析,第二个参数传入的对象不一样,一个是Iterator<IntWritable>,而自动生成的是java.lang.Iterable<IntWritable>,问题就出在这个地方,当我在自己写的reduce方法前加上关键字@Override 时,自己写的方法立马报错。Iterator这个是hadoop老版本之前的写法。


解决方案:使用自动生成的reduce方法,然后在里面重新写reduce处理逻辑。

经验:尽量在重写方法前加上@Override ,它对代码的检测是非常用帮助的。







发布了103 篇原创文章 · 获赞 14 · 访问量 29万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章