gpt4 book ai didi

java - 第二次迭代 - 值保持不变

转载 作者:可可西里 更新时间:2023-11-01 16:43:36 25 4
gpt4 key购买 nike

我正在使用 hadoop 开发一个 mapreduce 程序。
我的 reducer 中有这部分代码:

public void reduce(Text key, Iterable<TextLongWritable> values,Context context) throws IOException, InterruptedException {

long word1count = 0;
List<TextLongWritable> cache = new ArrayList<TextLongWritable>();

String decade = key.toString().split("\t")[0];
String word1 = key.toString().split("\t")[1];

for (TextLongWritable val : values) {
if (val.getWord().equals("*")){
word1count += val.getCount();
continue;
}
cache.add(val);
log.info("***Reducer*** Word1: " + word1 + " Word2: " + val.getWord());
}

context.write(key, new Text("" + word1count));

for (TextLongWritable value : cache) {
if (value.getWord().equals("*")){
continue;
}
log.info("***Reducer*** Word1: " + word1 + " Word2: " + value.getWord());
context.write(new Text(decade + "\t" + value.getWord()), new Text(word1 + " " + value.getCount() + "\t" + word1count));
}

}

首先,我正在使用缓存,正如我看到的那样 here为了对值进行两次迭代。

我的问题是在第二个循环中,所有值都保持不变。例如,如果我有一个包含单词 one two three 的列表。假设 key 是 1900 test,因此 word1 = "test"

第一个 记录器输出将是:

***Reducer*** Word1: test  Word2: one
***Reducer*** Word1: test Word2: two
***Reducer*** Word1: test Word2: three

但是第二个记录器输出将是:

***Reducer*** Word1: test  Word2: one
***Reducer*** Word1: test Word2: one
***Reducer*** Word1: test Word2: one

由于某些原因,该值保持不变。
我在这里做错了什么?跟hadoop有关系吗?

最佳答案

由于 GC 开销,Hadoop 在反序列化期间缓存了相同的对象。您必须克隆或深度复制您的 TextLongWritable 才能将其放入集合中。

关于java - 第二次迭代 - 值保持不变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38313844/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com