Hadoop WordCount教程java.lang.ClassNotFoundException

时间:2016-12-02 03:17:26

标签: hadoop

我对hadoop相对较新,我正在努力理解我在尝试运行这项工作时得到的ClassNotFoundException。我使用标准教程here,这是我的WordCount类(在ubuntu 16.04 hadoop 2.7.3分布式集群模式下运行):

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

为了尝试并保持井井有条,我在〜/ .bashrc文件中添加了几条路径:

hduser@mynode:~$ cd $HADOOP_CODE
hduser@mynode:/usr/local/hadoop/code$

这是$ HADOOP_HOME目录下的一个目录。为了编译WordCount.JAVA文件,我跑了:

hduser@mynode:/usr/local/hadoop$ hadoop com.sun.tools.javac.Main $HADOOP_CODE/WordCount.java
hduser@mynode:/usr/local/hadoop$ jar cf wc.jar $HADOOP_CODE/WordCount*.class

然后我尝试了:

hduser@mynode:/usr/local/hadoop$ hadoop jar $HADOOP_CODE/wc.jar $HADOOP_CODE/WordCount /home/hduser/input /home/hduser/output/wordcount

轰炸了以下错误:

Exception in thread "main" java.lang.ClassNotFoundException: /usr/local/hadoop/code/WordCount

修改 这给了我同样的错误:

hduser@mynode:/usr/local/hadoop/code$ hadoop jar $HADOOP_CODE/wc.jar WordCount /home/hduser/input /home/hduser/output/wordcount

为了让它无误地运行,我将WordCount.Java文件向上移动到默认的hadoop($ HADOOP_HOME)文件夹。我也从herehere知道解决方案是将文件包添加到文件中。

我想要了解的是为什么这就是解决方案。没有软件包名称,hadoop在哪里查找指定的软件包,为什么我不能将它传递给它以使其正确运行?这可能是一个基本的java问题(道歉 - 我来自python世界),但是在编译过程中包名称是什么使得它可以在没有路径名的情况下运行,但是不用包名意味着它必须在默认目录中?我不想在我运行的每个作业中都添加包名。非常感谢您的解释!

0 个答案:

没有答案