奇怪的HashMap结果-Java、Hadoop
我已经编写了这个简单的代码,它相当于Reducer类中的默认run方法,但是发生了一些完全奇怪的事情 以下是默认的运行方法:奇怪的HashMap结果-Java、Hadoop,java,hadoop,hashmap,Java,Hadoop,Hashmap,我已经编写了这个简单的代码,它相当于Reducer类中的默认run方法,但是发生了一些完全奇怪的事情 以下是默认的运行方法: public void More ...run(Context context) throws IOException, InterruptedException { setup(context); while (context.nextKey()) { reduce(context.getCurrentKey(), context.getVal
public void More ...run(Context context) throws IOException, InterruptedException {
setup(context);
while (context.nextKey()) {
reduce(context.getCurrentKey(), context.getValues(), context);
}
cleanup(context);
}
输出:
New reducer: 0
Reducer: 0:9,2:5
end of this reducer
Reducer: 0:9,5:7
end of this reducer
New reducer: 0
Reducer: 7:7,7:6
end of this reducer
。。。(很多钥匙)
下面是我的重写方法:
@Override
public void run(Context context) throws IOException, InterruptedException {
setup(context);
HashMap<Text,HashSet<Text>> map = new HashMap<Text,HashSet<Text>>();
while (context.nextKey()) {
//reduce(context.getCurrentKey(),context.getValues(),context);
Text key = context.getCurrentKey();
map.put(key, new HashSet<Text>());
for(Text v : context.getValues()){
map.get(key).add(v);
}
}
for(Text k : map.keySet()){
reduce(k,map.get(k),context);
}
cleanup(context);
}
。。。(很多钥匙)
我的问题是,如果我首先将键和值复制到hashmap,则任何操作都不会正常进行,并且在reduce调用中,它会一次又一次地传递同一个键(存储在hashmap中的第一个键):/
有人能帮我吗?我怎样才能正常工作?我需要这样做,因为我想在将钥匙发送到减速器之前对其进行预处理。
提前谢谢 Hadoop重用了可写对象。因此,在将它们放入收藏之前,您需要创建新的 更改代码以复制内容将如下所示:
while (context.nextKey()) {
Text key = new Text(context.getCurrentKey());
map.put(key, new HashSet<Text>());
for(Text v : context.getValues()){
map.get(key).add(new Text(v));
}
}
while(context.nextKey()){
Text key=新文本(context.getCurrentKey());
put(key,newhashset());
对于(文本v:context.getValues()){
map.get(key.add)(新文本(v));
}
}
很抱歉输出太长!我希望现在看起来好多了;)当然……当我获得所需的声誉时。。。我完全支持你@开发者:)请接受我的回答,不用担心升级投票。当我尝试写Stings时,也会出现这个问题吗?或者它只发生在文本对象上?@developer您不能将字符串写为可写的,因为它们不可写(通过可写序列化程序)。但是,如果您使用java默认序列化程序,这应该不是问题,因为每次都会创建一个新实例。
Reducer: 7:7,7:6
end of this reducer
while (context.nextKey()) {
Text key = new Text(context.getCurrentKey());
map.put(key, new HashSet<Text>());
for(Text v : context.getValues()){
map.get(key).add(new Text(v));
}
}