首页 > 解决方案 > 计算键和数字格式的出现次数

问题描述

在 MapReduce 的减速器部分,我有代码

 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 的出现次数,1,201.22 是所有键 1 的总值)

标签: hadoopmapreduce

解决方案


推荐阅读