【问题标题】:NoSuchElementException in mapreducemapreduce 中的 NoSuchElementException
【发布时间】:2017-04-30 13:11:09
【问题描述】:

我是 map reduce 的新手,遇到 NoSuchElementException,请帮忙。

在文本下方输入文件容器:

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

映射器类:

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

【问题讨论】:

    标签: hadoop mapreduce stringtokenizer


    【解决方案1】:

    每次迭代都在循环中调用 wordList.nextToken() 三次。每次调用StringTokenizer都会返回下一个token,当你的程序在你的文本中碰到单词first时会导致异常,因为你先检索first然后time然后尝试检索下一个单词, 不存在,导致异常。

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

    【讨论】:

      猜你喜欢
      • 2014-10-11
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 2020-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多