nosuchelementexception

vltsax25  于 2021-05-29  发布在  Hadoop
关注(0)|答案(1)|浏览(327)

我是新来的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);
             }
        }
    }

}

这就是我犯的错误

cyej8jka

cyej8jka1#

你在打电话吗 wordList.nextToken() 每次迭代在循环中执行三次。每次你叫它 StringTokenizer 将返回下一个标记,这将在程序命中单词时导致异常 first 在你的文本中,因为你检索 first 那么 time 然后尝试检索下一个不存在的单词,导致异常。
您需要做的是在每次迭代中检索一次,并将其存储在变量中。或者如果您真的需要在一次迭代中检索两个单词,请始终调用 hasMoreTokens() 在你打电话之前检查是否还有其他单词需要处理 nextToken() .

相关问题