如何解决HashMap值总是被Hadoop MapReduce中的最后一个输入键覆盖
我在reducer步骤中创建了一个hashmap。目的是从reducer收集所有结果,然后在cleanup()方法中对其进行进一步处理。
我的Hashmap已将LongWritable
作为其键,并将reducer的输入键作为值。
以下是我的减速器代码:
public static class ComputeFinalReducer extends Reducer<Text,Text,LongWritable,Text> {
private HashMap<LongWritable,Text> mymap = new HashMap<LongWritable,Text>();
public void reduce(Text key,Iterable<Text> values,Context context)
throws IOException,InterruptedException {
LongWritable result;
//'result' is calculated here.
mymap.put(result,key);
//the final output in output folder is correct
context.write(result,key);
//both result and key are printed to console and verified ok.
System.out.println(result + key);
}
}
public void cleanup(Context context) throws IOException,InterruptedException {
//Doing further processing for mymap here.
}
}
这是我的观察结果
-
正如我在控制台日志中所验证的那样,
- “结果”和“键”值在每次迭代中都是正确的。来自context.write()的Reducer的最终输出也是正确的。也就是正确生成part-r-00000文件。
- 我有许多以前的映射器提供的不同的reducer输入键。当这些键值对被一对一处理时,mymap的值总是被最新的“键”所覆盖。如何避免这种情况?
例如,
预期的mymap:
result1,key1
result2,key2
result3,key3
...and so on
实际mymap:
result1,key3
result2,key3
result3,key3
...
然后在下一次调用中,它将再次变为:
result1,key4
result2,key4
result3,key4
result4,key4
...
如何解决此问题?谢谢大家!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。