【发布时间】: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 的输出,没有任何更改,并且给出了预期的输出
【问题讨论】: