java.lang.NoClassDefFoundError in Hadoop Basics' MapReduce Program



我正在尝试Hadoop的Basic MapReduce程序,该程序的教程在http://java.dzone.com/articles/hadoop-basics-creating

该类的完整代码是(代码出现在上面url的网络上)

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.input.KeyValueTextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
public class Dictionary {
public static class WordMapper extends Mapper<Text, Text, Text, Text> {
private Text word = new Text();
public void map(Text key, Text value, Context context) throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString(), ",");
while (itr.hasMoreTokens()) {
word.set(itr.nextToken());
context.write(key, word);
}
}
}
public static class AllTranslationsReducer extends Reducer<Text, Text, Text, Text> {
private Text result = new Text();
public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
String translations = "";
for (Text val : values) {
translations += "|" + val.toString();
}
result.set(translations);
context.write(key, result);
}
}
public static void main(String[] args) throws Exception {
System.out.println("welcome to Java 1");
Configuration conf = new Configuration();
System.out.println("welcome to Java 2");
Job job = new Job(conf, "dictionary");
job.setJarByClass(Dictionary.class);
job.setMapperClass(WordMapper.class);
job.setReducerClass(AllTranslationsReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
job.setInputFormatClass(KeyValueTextInputFormat.class);
FileInputFormat.addInputPath(job, new Path("/tmp/hadoop-cscarioni/dfs/name/file"));
FileOutputFormat.setOutputPath(job, new Path("output"));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}

但在日食中奔跑之后;我得到错误,

welcome to Java 1
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at org.apache.hadoop.conf.Configuration.<clinit>(Configuration.java:73)
at Dictionary.main(Dictionary.java:43)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 2 more
请注意,异常是NoClassDefFoundError,而不是ClassNotFoundException。

注意:当类在运行时不可见,但在编译时可见时,会引发NoClassDefFoundError。这可能发生在JAR文件的分发或生产过程中,其中并没有包括所有必需的类文件。

要修复:请检查构建时和运行时类路径中的差异。

NoClassDefFoundError和ClassNotFoundException不同。一个是错误,另一个是异常。

NoClassDefFoundError:源于JVM在查找期望查找的类时遇到问题。由于找不到类文件,在编译时正在运行的程序无法运行。

ClassNotFoundException:此异常表示类路径中未找到该类,即我们正在尝试加载类定义,而类路径中不存在包含该类的class/jar。

NoClassDefFoundError在类在运行时不可见但在编译时出现。这可能与JAR文件有关,因为没有包括所有必需的类文件。

所以试着在你的类路径中添加commons-logging-1.1jar,你可以从中获得http://commons.apache.org/logging/download_logging.cgi

NoClassDefFoundError发生在命名类在类路径中成功定位,但由于某些原因无法加载和验证时。大多数情况下,问题是验证命名类所需的另一个类丢失或版本错误。

一般来说,这个错误意味着"仔细检查类路径中是否有所有正确的JAR文件(正确版本)"。

在本地IDE(Eclipse)中运行Hadoop Map/Reduce程序时,这是一个非常常见的错误。

您应该已经在构建路径中添加了hadoop-core.jar,这样就不会在程序中检测到编译错误。但是在运行它时会出现错误,因为hadoop核心依赖于commons-logging.jar(以及其他一些jar)。您可能需要将/lib下的jar添加到您的构建路径中。

我建议您使用Maven或其他依赖关系管理工具来管理依赖关系。

请阅读一篇文章:http://kishorer.in/2014/10/22/running-a-wordcount-mapreduce-example-in-hadoop-2-4-1-single-node-cluster-in-ubuntu-14-04-64-bit/.它解释了如何在没有Marven的情况下在Eclipse中引用依赖项。然而,据我所知,马文是首选方式。

最新更新