无法找到Hadoop Java类

时间:2018-05-16 22:03:06

标签: java hadoop mapreduce

线程中的异常" main"抛出java.lang.ClassNotFoundException:WordCount->这么多的答案与这个问题有关,似乎我肯定错过了一个小点,这花了我几个小时来计算 我将尝试尽可能清楚地了解路径,代码本身以及我尝试过的其他可能的解决方案 我确信我正确配置了Hadoop,因为一切正常,直到最后一个阶段 但仍然发布细节:

  1. 环境变量和路径
  2. >

    HADOOP VARIABLES START
    export HADOOP_HOME=/usr/local/hadoop
    export HADOOP_CONF_DIR=/usr/local/hadoop/etc/hadoop
    export HADOOP_INSTALL=/usr/local/hadoop
    export HADOOP_CLASSPATH=/usr/lib/jvm/java-8-oracle/lib/tools.jar
    export PATH=$PATH:$HADOOP_INSTALL/bin
    export PATH=$PATH:$HADOOP_INSTALL/sbin
    export HADOOP_MAPRED_HOME=$HADOOP_HOME
    export HADOOP_COMMON_HOME=$HADOOP_HOME
    export HADOOP_HDFS_HOME=$HADOOP_HOME
    export YARN_HOME=$HADOOP_HOME
    #export HADOOP_COMMON_LIB_NATIVE_DIR=$HADOOP_INSTALL/lib/native
    #export HADOOP_OPTS="-Djava.library.path=$HADOOP_INSTALL/lib"
    export JAVA_HOME=/usr/lib/jvm/java-8-oracle
    export PATH=$PATH:$HADOOP_HOME/bin
    export PATH=$PATH:$JAVA_HOME/bin
    
    #HADOOP VARIABLES END
    

    班级本身:

    package com.cloud.hw03;
    
    /**
     * Hello world!
     *
     */
    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, "wordcount");
        job.setMapperClass(TokenizerMapper.class);
        job.setCombinerClass(IntSumReducer.class);
        job.setReducerClass(IntSumReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        job.setJarByClass(WordCount.class);
        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
        System.exit(job.waitForCompletion(true) ? 0 : 1);
      }
    }
    

    我为编译和运行所做的工作:

    1. 使用我的WordCount maven项目(eclipse-workspace)在同一文件夹中创建jar文件

      $ hadoop com.sun.tools.javac.Main WordCount.java

      $ jar cf WordCount.jar WordCount * .class

    2. 运行程序:(我已经在hdfs中创建了一个目录并复制了输入输出文件)

    3. hadoop jar WordCount.jar WordCount / input / inputfile01 / input / outputfile01

      结果是:线程中的异常&#34; main&#34; java.lang.ClassNotFoundException:WordCount

      由于我与WordCount.class位于同一目录,并且我在同一目录中创建了我的jar文件,我没有指定WordCount的完整路径,所以我在这个目录中运行上面的第二个命令: enter image description here

      我已经添加了job.setJarByClass(WordCount.class);到代码所以没有帮助。我很感激你花时间回答! 我确信我再做一些意想不到的事情,并且无法弄清楚4小时

1 个答案:

答案 0 :(得分:0)

Hadoop网站上的Wordcount示例代码不使用包

既然你有一个,你就可以运行完全限定的类。与常规Java应用程序完全相同

hadoop jar WordCount.jar com.cloud.hw03.WordCount

另外,如果你实际上有一个Maven项目,那么hadoop com.sun.tools.javac.Main是不正确的。您实际上将使用Maven来编译和创建包含所有类的JAR,而不仅仅是WordCount*个文件

例如,从包含pom.xml的文件夹

mvn package 

否则,您需要位于父目录

hadoop com.sun.tools.javac.Main ./com/cloud/hw03/WordCount.java

并从该目录运行jar cf命令