Hadoop运行命令java.lang.ClassNotFoundException

时间:2018-04-24 07:42:26

标签: java hadoop

我已成功安装hadoop 3.0.0独立在Ubuntu 16.04上运行。 我使用Apache hadoop教程中的以下代码创建了一个jar。

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 WDCount {

    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(WDCount.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);
      }
    }

创建WDCount.jar成功且没有错误

然后我创建了输入和输出文件夹,并创建了带有短语的文本文件,并将其保存为输入文件夹中的fileo1.txt。

我创建了这个文本,以便在WDCount.jar上运行hadoop

/usr/local/hadoop/bin/hadoop jar /usr/local/hadoop/share/hadoop/mapreduce/Wordcount/WDCount.jar /usr/local/hadoop/share/hadoop/mapreduce/Wordcount/Input /usr/local/hadoop/share/hadoop/mapreduce/Wordcount/Output

当我运行代码时,我收到此消息;

  

线程“main”中的异常java.lang.ClassNotFoundException:/ usr / local / hadoop / share / hadoop / mapreduce / Wordcount / Input
          at java.lang.Class.forName0(Native Method)
          在java.lang.Class.forName(Class.java:348)
          在org.apache.hadoop.util.RunJar.run(RunJar.java:232)
          在org.apache.hadoop.util.RunJar.main(RunJar.java:153)

谁能告诉我出了什么问题?

1 个答案:

答案 0 :(得分:1)

在jar名称

之后包含包含main方法的类文件的名称

usr / local / hadoop / bin / hadoop jar /usr/local/hadoop/share/hadoop/mapreduce/Wordcount/WDCount.jar WDCount / usr / local / hadoop / share / hadoop / mapreduce / Wordcount / Input / usr /本地/ hadoop的/共享/ hadoop的/映射精简/ WORDCOUNT /输出