在mapreduce的reducer部分,我有代码
public static class IntSumReducer extends Reducer<Text, Text, Text, Text> {
private Text textValue = new Text();
private FloatWritable floatWritable = new FloatWritable();
@Override
public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
double total = 0.00;
int count = 0;
for (Text val: values) {
String line = val.toString();
String[] field = line.split(",");
count+=1;
total += Float.parseFloat(field[1]);
}
String v = String.valueOf(count) + "," + String.valueOf(total);
textValue.set(v);
context.write(key, textValue);
}
}
但是,对于每个键,输出都返回1,而不是该键的出现次数。另外,我如何将总数格式化为一个有“,”的数字,表示一个有2位小数的千位值?
电流输出
1 1, 1201.22135
期望输出
1 3, 1,201.22
(3是键1的出现次数,1201.22是所有键1的总值)
暂无答案!
目前还没有任何答案,快来回答吧!