5

我正在尝试 Hadoop 的基本 MapReduce 程序,其教程位于http://java.dzone.com/articles/hadoop-basics-creating

该类的完整代码是(代码在上面的网址上的网上)

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

但是在eclipse中运行之后;我得到了错误,

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
4

5 回答 5

10

请注意,异常是 NoClassDefFoundError 而不是 ClassNotFoundException。

注意:当类在运行时不可见但在编译时可见时抛出 NoClassDefFoundError。这可能是在 JAR 文件的分发或生产中可能发生的事情,其中​​并未包含所有必需的类文件。

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

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

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

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

于 2012-12-08T11:17:41.350 回答
6

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

因此,请尝试添加您可以从http://commons.apache.org/logging/download_logging.cgi获取的类路径 commons-logging-1.1.1 jar

于 2012-12-10T06:26:26.400 回答
4

NoClassDefFoundError 发生在命名类成功定位到类路径中,但由于某种原因无法加载和验证。最常见的问题是验证命名类所需的另一个类要么丢失,要么版本错误。

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

于 2012-12-08T14:17:05.883 回答
2

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

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

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

于 2012-12-09T14:12:17.010 回答
0

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

于 2016-02-16T02:15:04.563 回答