mapreduce中的NoSuchElementException

时间:2017-04-30 13:11:10

标签: hadoop mapreduce stringtokenizer

我是新来的地图减少获取NoSuchElementException,请帮忙。

文本下面的输入文件容器:

this is a hadoop program
i am writing it for first time

Mapper类:

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);
             }
        }
    }

}

This is the error I am getting

1 个答案:

答案 0 :(得分:1)

每次迭代都会在循环中调用wordList.nextToken()三次。每次调用它时StringTokenizer都会返回下一个令牌,当您的程序点击文字中的单词first时会导致异常,因为您检索first然后time然后尝试检索不存在的下一个单词,从而导致异常。

您需要做的是在每次迭代中检索一次并将其存储在变量中。或者,如果您确实需要在一次迭代中检索两个单词,请在实际调用hasMoreTokens()之前调用nextToken()以检查是否确实存在另一个要处理的单词。