【发布时间】:2018-03-09 03:01:22
【问题描述】:
我有一个名为Pair 的简单类,它实现了org.apache.hadoop.io.Writable。它包含两个字段,在 MapReduce 过程中用作 Value。
对于每个键,我想找到 Pair 的一个字段(preco)中值最大的对。在 reducer 中,下面的代码会产生预期的结果:
float max = 0;
String country = "";
for (Pair p : values){
if (p.getPreco().get() > max)
{
max = p.getPreco().get();
country = p.getPais().toString();
}
}
context.write(key, new Pair(new FloatWritable(max), new Text(country)));
另一方面,下面的代码没有:
Pair max = new Pair();
for (Pair p : values)
if (p.getPreco().get() > max.getPreco().get())
max = p;
context.write(key, max);
第二个代码为每个键生成输入文件中与之关联的最后一个值,而不是最大值。
这种明显奇怪的行为是否有原因?
【问题讨论】: