5
回答
Eclipse中运行Wordcount程序报错

Wordcount程序如下:

import java.io.IOException;
import java.util.Iterator;
import java.util.StringTokenizer;

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.mapred.FileInputFormat;
import org.apache.hadoop.mapred.FileOutputFormat;
import org.apache.hadoop.mapred.JobClient;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reducer;
import org.apache.hadoop.mapred.Reporter;
import org.apache.hadoop.mapred.TextInputFormat;
import org.apache.hadoop.mapred.TextOutputFormat;

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("hdfs://192.168.1.181:9000/user/root/bbs_post2"));
        FileOutputFormat.setOutputPath(conf, new Path("hdfs://192.168.1.181:9000/data/test-out10"));
//        FileInputFormat.setInputPaths(conf, new Path(args[0]));
//        FileOutputFormat.setOutputPath(conf, new Path(args[1]));
        JobClient.runJob(conf);
    }
}
报错信息如下:

log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" java.lang.NullPointerException
    at java.lang.ProcessBuilder.start(Unknown Source)
    at org.apache.hadoop.util.Shell.runCommand(Shell.java:445)
    at org.apache.hadoop.util.Shell.run(Shell.java:418)
    at org.apache.hadoop.util.Shell$ShellCommandExecutor.execute(Shell.java:650)
    at org.apache.hadoop.util.Shell.execCommand(Shell.java:739)
    at org.apache.hadoop.util.Shell.execCommand(Shell.java:722)
    at org.apache.hadoop.fs.RawLocalFileSystem.setPermission(RawLocalFileSystem.java:631)
    at org.apache.hadoop.fs.RawLocalFileSystem.mkdirs(RawLocalFileSystem.java:421)
    at org.apache.hadoop.fs.FilterFileSystem.mkdirs(FilterFileSystem.java:277)
    at org.apache.hadoop.mapreduce.JobSubmissionFiles.getStagingDir(JobSubmissionFiles.java:125)
    at org.apache.hadoop.mapreduce.JobSubmitter.submitJobInternal(JobSubmitter.java:348)
    at org.apache.hadoop.mapreduce.Job$10.run(Job.java:1285)
    at org.apache.hadoop.mapreduce.Job$10.run(Job.java:1282)
    at java.security.AccessController.doPrivileged(Native Method)
    at javax.security.auth.Subject.doAs(Unknown Source)
    at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1548)
    at org.apache.hadoop.mapreduce.Job.submit(Job.java:1282)
    at org.apache.hadoop.mapred.JobClient$1.run(JobClient.java:562)
    at org.apache.hadoop.mapred.JobClient$1.run(JobClient.java:557)
    at java.security.AccessController.doPrivileged(Native Method)
    at javax.security.auth.Subject.doAs(Unknown Source)
    at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1548)
    at org.apache.hadoop.mapred.JobClient.submitJobInternal(JobClient.java:557)
    at org.apache.hadoop.mapred.JobClient.submitJob(JobClient.java:548)
    at org.apache.hadoop.mapred.JobClient.runJob(JobClient.java:833)
    at WordCount.main(WordCount.java:71)

麻烦大家帮我看一下是怎么回事,谢谢!

举报
闵开慧
发帖于4年前 5回/2K+阅
共有5个答案 最后回答: 3年前


1.classpath下面是否放置hadoop conf下面的*-site.xml文件

2.执行程序的主机是否配置了hadoop集群的hosts

3.是否把该程序打包放在Hadoop lib 下并且重启了集群,如果不想打包,可以设置conf.setJarClass(Map.class) 该包会被分发到集群。


import java.io.IOException;
   4 import java.util.*;
   5         
   6 import org.apache.hadoop.fs.Path;
   7 import org.apache.hadoop.conf.*;
   8 import org.apache.hadoop.io.*;
   9 import org.apache.hadoop.mapreduce.*;
  10 import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  11 import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
  12 import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  13 import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
  14         
  15 public class WordCount {
  16         
  17  public static class Map extends Mapper<LongWritable, Text, Text, IntWritable> {
  18     private final static IntWritable one = new IntWritable(1);
  19     private Text word = new Text();
  20         
  21     public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
  22         String line = value.toString();
  23         StringTokenizer tokenizer = new StringTokenizer(line);
  24         while (tokenizer.hasMoreTokens()) {
  25             word.set(tokenizer.nextToken());
  26             context.write(word, one);
  27         }
  28     }
  29  } 
  30         
  31  public static class Reduce extends Reducer<Text, IntWritable, Text, IntWritable> {
  32 
  33     public void reduce(Text key, Iterable<IntWritable> values, Context context) 
  34       throws IOException, InterruptedException {
  35         int sum = 0;
  36         for (IntWritable val : values) {
  37             sum += val.get();
  38         }
  39         context.write(key, new IntWritable(sum));
  40     }
  41  }
  42         
  43  public static void main(String[] args) throws Exception {
  44     Configuration conf = new Configuration();
  45         
  46         Job job = new Job(conf, "wordcount");
  47     
  48     job.setOutputKeyClass(Text.class);
  49     job.setOutputValueClass(IntWritable.class);
  50         
  51     job.setMapperClass(Map.class);
  52     job.setReducerClass(Reduce.class);
  53         
  54     job.setInputFormatClass(TextInputFormat.class);
  55     job.setOutputFormatClass(TextOutputFormat.class);
  56         
  57     FileInputFormat.addInputPath(job, new Path(args[0]));
  58     FileOutputFormat.setOutputPath(job, new Path(args[1]));
  59         
  60     job.waitForCompletion(true);
  61  }
  62         
  63 }



引用来自“张东昊”的评论


1.classpath下面是否放置hadoop conf下面的*-site.xml文件

2.执行程序的主机是否配置了hadoop集群的hosts

3.是否把该程序打包放在Hadoop lib 下并且重启了集群,如果不想打包,可以设置conf.setJarClass(Map.class) 该包会被分发到集群。


1.classpath下面是否放置hadoop conf下面的*-site.xml文件(hadoop集群是在centos系统中,在windows中的eclipse里面运行wordcount程序。请问你说的classpath是在hadoop集群里面配置吗?)
2.执行程序的主机是否配置了hadoop集群的hosts(这个在centos和windows的hosts里面都配置了)
3.是否把该程序打包放在Hadoop lib 下并且重启了集群,如果不想打包,可以设置conf.setJarClass(Map.class) 该包会被分发到集群。(这一项没有明白)
请问你那有可以运行的wordcount程序吗,我对比一下是不是程序的问题

引用来自“Nob”的评论

楼主怎么解决的?
Hadoop2以上需要在conf中添加集群配置信息,否则提交任务时找不到集群。
顶部