hadoop-text-to-float转换问题

e0bqpujr  于 2021-05-27  发布在  Hadoop
关注(0)|答案(1)|浏览(387)

我有一个包含键值对的csv文件;同一密钥可以有多条记录。我正在编写一个mapreduce程序来聚合这些数据-对于每个键,它应该给出键的频率和键的值之和。
我的Map器读取csv文件,并以文本类型发出key和value,即使它们是数字(这样做是因为我在使用floatwritable for value时遇到了问题)。
在reducer中,当我尝试将文本值转换为float时,我遇到了numberformatexception,错误中显示的值甚至不在我的输入中。
这是我的密码:

public static class AggReducer
       extends Reducer<Text,Text,Text,Text> {
    private Text result = new Text();

    public void reduce(Text key, Iterable<FloatWritable> values,
                       Context context
                       ) throws IOException, InterruptedException {
      int numTrips = 0;
      int totalFare = 0;

      for (Text val : values) {
        totalFare += Float.parseFloat(val.toString());
        numTrips++;
      }

      String resultStr = String.format("%1s,%2s", numTrips, totalFare);
      result.set(resultStr);
      context.write(key, result);
    }
  }

注意:我让reducer生成mapper的输出而不做任何更改,这样就得到了预期的输出

qpgpyjmq

qpgpyjmq1#

运行numberformatexception时,错误中显示的值甚至不在我的输入中
嗯,那是不可能的。值需要在输入或生成的Map器输出中的某个位置。不过,try-catch在减速机中的效果和其他地方一样好
fwiw,使用doublewritable

相关问题