2017-11-10 151 views
0

我正在嘗試使用mapreduce查找每個小部件的平均值。該作業被成功完成,但使用Hadoop FS -cat用戶/流浪/示例-1 /部分-R-00000輸出不是在hadoop中產生的

public static class MaxWidgetReducer 
    extends Reducer<Text, FloatWritable, FloatWritable, NullWritable> { 

public void reduce(Text k, Iterable<FloatWritable> vals, Context context) 
    throws IOException, InterruptedException { 
    Float totalPrice = 0.0f; 
    Float avgPrice = 0.0f; 
    Integer count = null; 

    for (FloatWritable w : vals) { 

     totalPrice = (totalPrice + w.get()); 
     count++; 

}

avgPrice = (totalPrice)/(count); 

    context.write(new FloatWritable(avgPrice), NullWritable.get()); 

}

回答

0

我當生產出無強烈建議你在mapper和reducer中都使用try/catch塊,這樣你就可以知道這是否是由於處理數據時拋出了一個異常造成的,請嘗試將w.get()轉換爲float以便能夠將該價值添加到總價中。

乾杯。