今天做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();
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 ,它對代碼的檢測是非常用幫助的。