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 (将#修改为@)