【问题标题】:Exception in thread "main" java.lang.ClassNotFoundException: WordCount线程“主”java.lang.ClassNotFoundException 中的异常:WordCount
【发布时间】:2013-09-18 17:44:27
【问题描述】:

我目前想要创建 Hadoop 的单个实例节点。所以我关注这个tutorial。我在终端中运行了以下命令:

hduser@ubuntu:/usr/local/hadoop$ bin/hadoop jar WordCount.jar geekyomega.WordCount /user/hduser/gutenberg /user/hduser/gutenberg-output

在我遇到这个错误之前一切都很顺利:

Exception in thread "main" java.lang.ClassNotFoundException: WordCount
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:270)
    at org.apache.hadoop.util.RunJar.main(RunJar.java:153)

我正在尝试使用从here 获得的以下代码运行此示例。这是我的代码版本:

package geekyomega;

import java.io.IOException;
import java.util.*;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.*;
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);
 }

}

我认为我的问题是工作实例化。于是我做了如下,我改了:

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

转至以下大写版本:

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

但这并没有帮助。有谁知道这里有什么可以帮助我的吗?

谢谢, 极客

PS - 我不想运行 wordcount 的教程版本。我所做的是在 eclipse 中创建项目,将 hadoop jar 添加到其中,并将其导出为 jar 文件。

【问题讨论】:

    标签: java debugging hadoop word-count


    【解决方案1】:

    除了添加包之外,还在程序的作业配置部分添加以下行:

    job.setJarByClass(WordCount.class);

    【讨论】:

    • 这行得通。你能解释一下为什么这是必要的吗?我从教程中复制了代码,但我有点沮丧的是,在我稍微调整了一下之后,示例代码无法开箱即用。所以换个说法,为什么会丢失,为什么我需要它?
    • 识别包含给定类的jar。而为什么不见了最好由作者自己来回答。我总是这样做,你也应该这样做。
    【解决方案2】:

    你的班级名称是 geekyomega.WordCount

    你没有附加包名。在命令行中,在 jar 文件名之后,给出作业类的完全限定名称。

    【讨论】:

    • 这也是事实,也是一个很好的收获。我会给你一些代表并修改我的问题。谢谢!
    • @akhilesh 这真的很有帮助..!!
    • 非常感谢,为什么我无法跑步。非常感谢。赞成(y)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-05
    • 2017-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-03
    相关资源
    最近更新 更多