gpt4 book ai didi

java - Hadoop Java 字数统计调整不起作用 - 尝试总结所有

转载 作者:可可西里 更新时间:2023-11-01 14:45:44 26 4
gpt4 key购买 nike

我正在尝试调整此处的字数统计示例:http://wiki.apache.org/hadoop/WordCount因此它将求和并返回输入文件中的单词数,而不是计算每个单词的出现次数。

我尝试更改映射器类,而不是在当前迭代中写入单词,而是为所有单词写入“Sum:”。

即替换

 word.set(tokenizer.nextToken());

@class " map "与

 word.set("Sum: ");

文件的其余部分保持不变。

这样一来,我认为所有映射器的输出都会到达同一个 reducer,该 reducer 最终将对“sum:”的数量求和,最终将成为文件中的单词数。

意思是:

 word  1
other 1
other 1

产生:

word  1
other 2

我期待的是:

 Sum:  1
Sum: 1
Sum: 1

产生:

 Sum: 3

相反,当我尝试运行代码时,我得到了一个非常长的映射操作,最终以抛出异常结束:

RuntimeException: java.io.IOException: 溢出失败

无论输入文件多小。

期待您的帮助。谢谢

最佳答案

你有一个无限循环。在您的代码中,您需要调用

tokenizer.nextToken()

实际将 StringTokenizer 从行中的一个词前移。否则您的映射操作将永远不会取得进展。

所以你需要这样的东西:

public static class Map extends Mapper<LongWritable, Text, Text, IntWritable> {
private final static IntWritable one = new IntWritable(1);
private Text sumText = new Text("Sum: ");
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line);
while (tokenizer.hasMoreTokens()) {
tokenizer.nextToken(); //go to next word
context.write(sumText, one);
}
}
}

不过,还有一个没有循环的更好的解决方案。您可以使用 ẗhe countTokens() StringTokenizer 方法:

public static class Map extends Mapper<LongWritable, Text, Text, IntWritable> {
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line);
context.write(new Text("Sum: "), new IntWritable(tokenizer.countTokens()));
}
}

关于java - Hadoop Java 字数统计调整不起作用 - 尝试总结所有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25227879/

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