java - wordcount 程序中的 NoClassDefFoundError

标签 java hadoop

我正在运行 hadoop wordcount 程序。但它给了我像“NoClassDefFoundError”这样的错误

运行命令:

 hadoop -jar /home/user/Pradeep/sample.jar hdp_java.WordCount /user/hduser/ana.txt /user/hduser/prout
  Exception in thread "main" java.lang.NoClassDefFoundError: WordCount
   Caused by: java.lang.ClassNotFoundException: WordCount
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    Could not find the main class: WordCount. Program will exit.

我在eclipse中创建了程序,然后导出为jar文件

eclipse 代码:

package hdp_java;

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.LongWritable;
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.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

public class WordCount {

public static class Map extends Mapper<LongWritable, Text, Text, IntWritable> {
   private final static IntWritable one = new IntWritable(1);
  private Text word = new Text();

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

  public static class Reduce extends Reducer<Text, IntWritable, Text, IntWritable> {

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

    public static void main(String[] args) throws Exception {
       Configuration conf = new Configuration();

    Job job = new Job(conf, "wordcount");

job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);

job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);

job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);

FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));

job.waitForCompletion(true);
}

}

谁能告诉我哪里错了?

最佳答案

您需要像这样告诉 hadoop 作业使用哪个 jar:

job.setJarByClass(WordCount.class);

还请务必在提交作业时向 HADOOP_CLASSPATH-libjars 添加任何依赖项,如下例所示:

使用以下命令从(例如)当前目录和 lib 目录添加所有 jar 依赖项:

export HADOOP_CLASSPATH=$HADOOP_CLASSPATH:`echo *.jar`:`echo lib/*.jar | sed 's/ /:/g'`

请记住,当通过 hadoop jar 启 Action 业时,您还需要通过使用 -libjars 将任何依赖项的 jar 传递给它。我喜欢使用:

hadoop jar <jar> <class> -libjars `echo ./lib/*.jar | sed 's/ /,/g'` [args...]

注意 sed 命令需要不同的分隔符; HADOOP_CLASSPATH: 分隔的,-libjars 需要 , 分隔。

关于java - wordcount 程序中的 NoClassDefFoundError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17046744/

相关文章:

hadoop - 将复制因子设置为 0 会发生什么

java - getHeight 在 main 中调用时返回真实高度,但在从另一个类调用时返回 0

hadoop - map 侧连接的理论/程序是什么

hadoop - 处理 PIG 脚本中的 fs(hadoop shell)命令错误

java - Android Studio 构建失败

hadoop - Hive CLI 无法从另一个表创建表

hadoop - 将流利的时间包括到json帖子数据中

java - Java中如何检查输入的数字是否有效

java - mvn 验证抛出依赖收敛错误,但 mvn 依赖 :tree does not show the dependency

java - 在不阻塞主线程的情况下检索 HTTP 响应