1. 程式人生 > >java.lang.ClassNotFoundException: Class WordCount$TokenizerMapper not found

java.lang.ClassNotFoundException: Class WordCount$TokenizerMapper not found

長時間沒學習,重新除錯mapreduce程式WordCount的時候,以前寫好並除錯通過的程式出問題了,報錯如下:

Error: java.lang.RuntimeException: java.lang.ClassNotFoundException: Class hadoopProj.wordcount.WordCount$TokenizerMapper not found

at org.apache.hadoop.conf.Configuration.getClass(Configuration.java:2195)

at org.apache.hadoop.mapreduce.task.JobContextImpl.getMapperClass(JobContextImpl.java:186)

at org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:745)

at org.apache.hadoop.mapred.MapTask.run(MapTask.java:341)

at org.apache.hadoop.mapred.YarnChild$2.run(YarnChild.java:163)

at java.security.AccessController.doPrivileged(Native Method)

at javax.security.auth.Subject.doAs(Subject.java:415)

死活不明白,以前明明除錯沒問題的,這次並沒有重新編譯,還是用的以前編好的jar包,也重啟hadoop,還是一樣的,於是詳查報錯的說明,在開頭處看到了這樣一段:

start main...

start job wait...

17/01/10 15:05:33 INFO client.RMProxy: Connecting to ResourceManager at /0.0.0.0:8032

17/01/10 15:05:33 WARN mapreduce.JobResourceUploader: No job jar file set.  User classes may not be found. See Job or

Job#setJar(String)

"No job jar file set."是什麼意思?要沒找到jar檔案還能執行嗎,試著在命令列中改了個不存在的jar檔名替代,報出的錯是不一樣的,說明jar檔案還是找到了,既然找到了為什麼又這麼提示?

上網搜有說Eclipse版本問題的,有說沒有新增依賴的,有說沒有上傳到hdfs的,好像都不沾邊,我用的是控制檯,以前執行也不需要新增什麼上傳什麼,於是想是不是跟jar檔案路徑的擺放有關係(因為之前沒少在這方面出錯),幸好我以前除錯偷懶寫了個shell用來執行編譯和除錯,可以從裡頭看出來之前的路徑結構以及所有的指令,對比之後發現跟我現在不同的就一處:我之前的WordCount.jar檔案放在/hadoopProj下,但是現在是放在/hadoopProj/wordcount下。於是我將jar檔案拷貝到/hadoopProj下再試(當然對應的指令中的路徑也要改),通過了!然後我就想是不是跟環境變數什麼的有關係,我檢查了沒有新增/hadoopProj,然後又試著拷貝WordCount.jar到任意路徑都可以執行通過,唯獨/hadoopProj/wordcount。於是我又開始審視程式碼,這裡也貼一下吧

package hadoopProj.wordcount;
import java.io.IOException;
import java.util.StringTokenizer;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class WordCount {

  public static class TokenizerMapper
       extends Mapper<Object, Text, Text, IntWritable>{

    private final static IntWritable one = new IntWritable(1);
    private Text word = new Text();

    public void map(Object key, Text value, Context context
                    ) throws IOException, InterruptedException {
      StringTokenizer itr = new StringTokenizer(value.toString());
      while (itr.hasMoreTokens()) {
        word.set(itr.nextToken());
        context.write(word, one);
      }
    }
  }

  public static class IntSumReducer
       extends Reducer<Text,IntWritable,Text,IntWritable> {
    private IntWritable result = new IntWritable();

    public void reduce(Text key, Iterable<IntWritable> values,
                       Context context
                       ) throws IOException, InterruptedException {
      int sum = 0;
      for (IntWritable val : values) {
        sum += val.get();
      }
      result.set(sum);
      context.write(key, result);
    }
  }

  public static void main(String[] args) throws Exception {
    Configuration conf = new Configuration();
    Job job = Job.getInstance(conf, "word count");
    job.setJarByClass(WordCount.class);
    job.setMapperClass(TokenizerMapper.class);
    job.setCombinerClass(IntSumReducer.class);
    job.setReducerClass(IntSumReducer.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);
    FileInputFormat.addInputPath(job, new Path(args[0]));
    FileOutputFormat.setOutputPath(job, new Path(args[1]));
    System.exit(job.waitForCompletion(true) ? 0 : 1);
  }
}
終於結合錯誤提示和程式碼我想通了一點:在WordCount.jar檔案放在/hadoopProj/wordcount下的時候,上述程式碼中橙色高亮的一句

job.setJarByClass(WordCount.class);

沒有成功(至於為什麼程式都已經從該jar檔案執行了還要通過這個方法來查詢這個jar檔案,我想應該是有時候需要查詢的類所在位置並非在當前jar檔案中,也就是說當前任務的實現未必包含在當前jar中,比如這個例子,main函式如果打包在另一個jar檔案中呢?)。那是為什麼沒有成功呢,應該是因為/hadoopProj/wordcount的目錄結構如下

hadoopProj

   -wordcount

-WordCount.jar

-WordCount.java

-WordCount.class

-...

在job.setJarByClass(WordCount.class)執行的時候,程式從當前任務的main函式所在的檔案路徑開始查詢WordCount.class,剛好就查到了hadoopProj/wordcount,這是一個資料夾,而不是一個jar檔案,所以失敗了,提示"No job jar file set.",而如果jar檔案不跟WordClass.class檔案放在同一目錄下,自然程式查詢當前目錄找不到就會去jar檔案裡找,就能返回該jar檔案。為了驗證這個想法,我把/hadoopProj/wordcount下的WordCount.class檔案刪除掉,果然沒問題,再添加回去,又是同樣出錯。

所以結論是:我不應該把WordCount.jar檔案放在/hadoopProj/wordcount下。本來以前jar檔案打好包的時候是在hadoopProj同級目錄下的,做完實驗後為了歸類我把它放在了wordcount下,才有了這次莫名其妙的錯誤。