hadoop - 计算键和数字格式的出现次数
问题描述
在 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 的总值)
解决方案
推荐阅读
- rust - 如何修复`未知语言项'panic_fmt'的定义`?
- apache-flink - 我在哪里可以找到我使用 Apache Flink 仪表板提交的 Apache Flink 服务器上的 jar
- c++ - 无法使用 strcpy 将字符串复制到静态结构成员
- android - 如何使 ScrollView 透明?
- java - iText - 循环读取pdf并一一合并
- javascript - 解构数组时如何忽略项目?
- r - 具有双重重置的复数累积和
- azure - 将 Azure Blob 存储帐户链接到 Azure 数据工厂时出错
- swift - 如何将数据从视图控制器传递到模型?
- python - 如何处理特征消除?