我是新来的Map减少得到nosuchelementexception,请帮助。
在文本下面输入文件容器:
this is a hadoop program
i am writing it for first time
Map器类:
public class Mappers extends MapReduceBase implements Mapper<LongWritable, Text, IntWritable, IntWritable>{
private Text word = new Text();
private IntWritable singleWordCount = new IntWritable();
private IntWritable one = new IntWritable(1);
@Override
public void map(LongWritable key, Text value, OutputCollector<IntWritable, IntWritable> output, Reporter reporter) throws IOException {
StringTokenizer wordList = new StringTokenizer(value.toString());
while (wordList.hasMoreTokens()) {
int wordSize = wordList.nextToken().length();
singleWordCount.set(wordSize);
if(word != null && wordList != null && wordList.nextToken() != null){
word.set(wordList.nextToken());
output.collect(singleWordCount, one);
}
}
}
}
这就是我犯的错误
1条答案
按热度按时间cyej8jka1#
你在打电话吗
wordList.nextToken()
每次迭代在循环中执行三次。每次你叫它StringTokenizer
将返回下一个标记,这将在程序命中单词时导致异常first
在你的文本中,因为你检索first
那么time
然后尝试检索下一个不存在的单词,导致异常。您需要做的是在每次迭代中检索一次,并将其存储在变量中。或者如果您真的需要在一次迭代中检索两个单词,请始终调用
hasMoreTokens()
在你打电话之前检查是否还有其他单词需要处理nextToken()
.