找不到Hadoop Job类

时间:2015-06-11 00:49:55

标签: java eclipse hadoop mapreduce classpath

嗨,我遇到了麻烦,而且我还没能从类似的线程中获得帮助。我正在做一个hadoop工作的例子,我现在正试图从IDE运行它。这是我的源代码

package org.myorg;

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

import org.apache.hadoop.io.*;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.mapred.*;

import org.apache.hadoop.util.*;


public class WordCount 
{

public static class Map extends MapReduceBase implements 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, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException
    {
       String line = value.toString().toLowerCase().replaceAll("\\p{Punct}|\\d","");
       StringTokenizer tokenizer = new StringTokenizer(line);
       while (tokenizer.hasMoreTokens()) 
       {

           word.set(tokenizer.nextToken());
           output.collect(word,  one);

       }// end while

    }// end public void map

}// end public static class Map


public static class Reduce extends MapReduceBase implements Reducer<Text, IntWritable, Text, IntWritable>
{
    public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException
    {
        int sum = 0;
        while (values.hasNext())
        {
            sum+= values.next().get();
        }// end while

        output.collect(key, new IntWritable(sum));

    }// end public void reduce

}// end public static class Reduce

public static void main(String[] args) throws Exception
{
    JobConf conf = new JobConf(org.myorg.WordCount.class);
    conf.setJobName("wordcount");

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

    conf.setMapperClass(Map.class);
    conf.setCombinerClass(Reduce.class);
    conf.setReducerClass(Reduce.class);

    conf.setInputFormat(TextInputFormat.class);
    conf.setOutputFormat(TextOutputFormat.class);

    FileInputFormat.setInputPaths(conf, new Path(args[0]));
    FileOutputFormat.setOutputPath(conf, new Path(args[1]));

    JobClient.runJob(conf);

    }// end main

}//end public class WordCount

以下是我的例外情况。有没有人有任何想法?

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Level
    at org.apache.hadoop.mapred.JobConf.<clinit>(JobConf.java:362)
    at org.myorg.WordCount.main(WordCount.java:56)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Level
    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:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)

1 个答案:

答案 0 :(得分:1)

显然,缺少log4j的jar文件。你应该将jar添加到类路径中。