在Eclipse中运行WordCount示例时出现ClassNotFoundException

时间:2013-11-04 12:43:51

标签: java eclipse hadoop mapreduce

我正在尝试运行WordCount map / reduce作业的示例代码。我在Hadoop 1.2.1上运行它。我正在从Eclipse运行它。这是我尝试运行的代码:

package mypackage;

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.Reducer.Context;
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();
        conf.set("mapred.job.tracker", "maprfs://,y_address");
        conf.set("fs.default.name", "hdfs://my_address");

        Job job = new Job(conf, "wordcount");
        job.setJarByClass(WordCount.class);

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

不幸的是,运行此代码最终会出现以下错误:

  

13/11/04 13:27:53 INFO mapred.JobClient:任务ID:   attempt_201310311611_0005_m_000000_0,状态:未通过   java.lang.RuntimeException:java.lang.ClassNotFoundException:   com.rf.hadoopspikes.WordCount $ Map at   org.apache.hadoop.conf.Configuration.getClass(Configuration.java:857)     在   org.apache.hadoop.mapreduce.JobContext.getMapperClass(JobContext.java:199)     在org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:718)     在org.apache.hadoop.mapred.MapTask.run(MapTask.java:364)at   org.apache.hadoop.mapred.Child $ 4.run(Child.java:255)at   java.security.AccessController.doPrivileged(Native Method)at   javax.security.auth.Subject.doAs(Subject.java:415)at   org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1190)     在org.apache.hadoop.mapred.Child.main(Child.java:249)

我知道无法找到WordClass,但我不知道如何使这项工作。 有什么想法吗?

1 个答案:

答案 0 :(得分:0)

直接从Eclipse运行时,您需要确保将类捆绑到Jar文件中(hadoop然后复制到HDFS)。您的错误很可能与您的Jar尚未构建的事实有关,或者在运行时,类是从输出目录运行而不是捆绑的jar。

尝试将类导出到jar文件中,然后从该Jar文件中运行WordCount类。您还可以考虑使用我认为可以处理所有这些形式的Eclipse Hadoop插件。最后的选择是捆绑jar然后从命令行启动(如各种Hadoop教程中所述)