执行mapreduce程序时发生nullpointer异常

xtfmy6hx  于 2021-06-02  发布在  Hadoop
关注(0)|答案(1)|浏览(372)

**结束。**此问题需要详细的调试信息。它目前不接受答案。
**想改进这个问题吗?**更新问题,使其成为堆栈溢出的主题。

四年前关门了。
改进这个问题
运行map reduce程序时出现nullpointer异常。请帮助理解为什么会出现此错误。

public class AvgDriver extends Configured implements Tool{

    @Override
    public int run(String[] arg0) throws Exception {

        Job job=Job.getInstance();
        job.setJar("AvgSalary.jar");

        job.setMapperClass(AvgMapper.class);
        job.setMapOutputKeyClass(NullWritable.class);
        job.setMapOutputValueClass(DoubleWritable.class);

        //job.setInputFormatClass(TextInputFormat.class);

        job.setReducerClass(AvgReducer.class);
        job.setOutputKeyClass(NullWritable.class);
        job.setOutputValueClass(DoubleWritable.class);

        FileInputFormat.setInputPaths(job, new Path(arg0[0]));
        FileOutputFormat.setOutputPath(job, new Path(arg0[1]));

        return job.waitForCompletion(true)?0:1;
    }

    public void main(String [] args) throws Exception
    {

        System.exit(ToolRunner.run(new AvgDriver(), args));
    }
}

public class AvgMapper extends Mapper<LongWritable, Text, NullWritable, DoubleWritable> {

    public void map(LongWritable key , Text value , Context context) throws IOException, InterruptedException
    {
        String values=value.toString();
        String [] val=values.split("\t");

        double convertVal=Double.parseDouble(val[2]);

        context.write(NullWritable.get(), new DoubleWritable(convertVal));
    }

} 

public class AvgReducer extends Reducer<NullWritable, DoubleWritable, NullWritable, DoubleWritable> {

    double total=0.0;
    int count=0;

    public void Reduce(NullWritable key , Iterator<DoubleWritable> value , Context context) throws IOException, InterruptedException
    {
        while (value.hasNext()) {
            total = total+ ((DoubleWritable) value.next()).get();
            count++;
        }

        total=total/count;

        context.write(key, new DoubleWritable(total));
    }
}
anhgbhbe

anhgbhbe1#

main方法中缺少static。更新如下。

public static void main(String [] args) throws Exception

相关问题