【问题标题】:Hadoop - Problem with Text to float conversionHadoop - 文本到浮点转换的问题
【发布时间】:2023-03-31 03:09:01
【问题描述】:

我有一个包含键值对的 csv 文件;对于同一个键,它可以有多个记录。我正在编写一个 mapreduce 程序来聚合这些数据 - 对于每个键,它应该给出键的频率和键的值的总和。

我的映射器读取 csv 文件并将键和值都作为文本类型发出,尽管它们是数字类型(这样做是因为我在使用 FloatWritable 作为值时遇到了问题)。

在减速器中,当我尝试将 Text 值转换为浮点数时,我遇到了 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 的输出,没有任何更改,并且给出了预期的输出

【问题讨论】:

    标签: java hadoop mapreduce


    【解决方案1】:

    遇到 NumberFormatException 并且错误中显示的值甚至不在我的输入中

    嗯,那是完全不可能的。值需要在您的输入或生成的映射器输出中的某个位置。尝试 catch 在减速器中的效果与在其他任何地方一样好,不过

    FWIW,使用 DoubleWritable

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-03
      • 1970-01-01
      • 2019-04-08
      • 2022-01-09
      • 1970-01-01
      • 2012-05-09
      • 1970-01-01
      • 2016-06-04
      相关资源
      最近更新 更多