运行WordCount.java时显示错误

时间:2013-03-14 06:03:55

标签: java hadoop word-count

我试过这段代码:

javac -verbose -classpath /var/root/hadoop-1.0.4/hadoop-1.0.4-core.jar WordCount.java -d /Users/amrita/desktop/hadoop/javatrail/wordcount_classes

我刚收到以下错误:

parsing started WordCount.java]
[parsing completed 12ms]
[search path for source files: /var/root/hadoop-1.0.4/hadoop-1.0.4-core.jar]
[search path for class files: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/jsfd.jar,/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/classes.jar,/System/Library/Frameworks/JavaVM.framework/Frameworks/JavaRuntimeSupport.framework/Resources/Java/JavaRuntimeSupport.jar,/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/ui.jar,/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/laf.jar,/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/sunrsasign.jar,/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/jsse.jar,/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/jce.jar,/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/charsets.jar,/Library/Java/Extensions/RXTXcomm.jar,/System/Library/Java/Extensions/AppleScriptEngine.jar,/System/Library/Java/Extensions/dns_sd.jar,/System/Library/Java/Extensions/j3daudio.jar,/System/Library/Java/Extensions/j3dcore.jar,/System/Library/Java/Extensions/j3dutils.jar,/System/Library/Java/Extensions/jai_codec.jar,/System/Library/Java/Extensions/jai_core.jar,/System/Library/Java/Extensions/mlibwrapper_jai.jar,/System/Library/Java/Extensions/MRJToolkit.jar,/System/Library/Java/Extensions/QTJava.zip,/System/Library/Java/Extensions/vecmath.jar,/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/ext/apple_provider.jar,/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/ext/dnsns.jar,/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/ext/localedata.jar,/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/ext/sunjce_provider.jar,/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/ext/sunpkcs11.jar,/var/root/hadoop-1.0.4/hadoop-1.0.4-core.jar]
[loading java/io/IOException.class(java/io:IOException.class)]
WordCount.java:5: package org.apache.hadoop.fs does not exist
import org.apache.hadoop.fs.Path;
                           ^
WordCount.java:6: package org.apache.hadoop.conf does not exist
import org.apache.hadoop.conf.*;
^
WordCount.java:7: package org.apache.hadoop.io does not exist
import org.apache.hadoop.io.*;
^
WordCount.java:8: package org.apache.hadoop.mapred does not exist
import org.apache.hadoop.mapred.*;
^
WordCount.java:9: package org.apache.hadoop.util does not exist
import org.apache.hadoop.util.*;
^
[loading java/lang/Object.class(java/lang:Object.class)]
[loading java/lang/String.class(java/lang:String.class)]
[loading java/lang/Exception.class(java/lang:Exception.class)]
WordCount.java:11: cannot find symbol
symbol  : class MapReduceBase
location: class org.myorg.WordCount
    public static class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> {
                                    ^
WordCount.java:11: cannot find symbol
symbol  : class Mapper
location: class org.myorg.WordCount
    public static class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> {
                                                             ^
WordCount.java:11: cannot find symbol
symbol  : class LongWritable
location: class org.myorg.WordCount
    public static class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> {
                                                                    ^
   [total 406ms]
48 errors

我的类路径有问题吗? 如果是这样,我怎样才能获得正确的类路径? 我的WordCount.java在/ Users / amrita / ....和root中的hadoop,有任何特权问题吗? 我无法生成WordCount.class

源代码:WordCount.java

package org.myorg;
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.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();
            StringTokenizer tokenizer = new StringTokenizer(line);
            while (tokenizer.hasMoreTokens()) {
                word.set(tokenizer.nextToken());
                output.collect(word, one);
            }
        }
    }
    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();
             }
             output.collect(key, new IntWritable(sum));
         }
    }
    public static void main(String[] args) throws Exception {
        JobConf conf = new JobConf(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);
    }
}

4 个答案:

答案 0 :(得分:1)

我创建了导入hadoop类的简单程序:

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;
import org.apache.hadoop.util.*;
public class WordCount {
    public static void main(String args[]) {
        System.out.println("test");
    }
}

并使用此命令javac -classpath "C:\Users\user\Desktop\hadd2\*" WordCount.java对其进行编译。在目录C:\Users\user\Desktop\hadd2\*中,我从hadoop页面(http://ftp.ps.pl/pub/apache/hadoop/common/stable/hadoop-1.0.4-bin.tar.gz)下载了* .jars。应用程序编译成功。

答案 1 :(得分:0)

这里的编译器抱怨MapReduce类。 如果不在你的hadoop库中,请检查它的classpath或import语句。

答案 2 :(得分:0)

hduser @ Linux:〜/ hadoop $ javac -classpath /home/hduser/hadoop/hadoop-core-1.1.2.jar -d wordcount_classes WordCount.java

hduser @ Linux:〜/ hadoop $

答案 3 :(得分:-1)

[已解决]

我使用new hadoop api而pgm使用的是old api