2016-04-22 59 views
0

我map.java类我试图通过这个错误:在地图显示java.lang.NullPointerException错误减少程序

context.write(新文本(stringWord),新文本(文件名))来发送文件名

recude.java

import java.io.IOException; 

import org.apache.hadoop.io.*; 
import org.apache.hadoop.mapreduce.*; 

public class Reduce extends Reducer<Text, Text, Text, Text> { 

@Override 
public void reduce(Text key, Iterable<Text> values, Context context) 
throws IOException, InterruptedException { 
    int sum = 0; 
    MapWritable occurenceInFile = new MapWritable(); 
    for (Text val : values) { 
     if(occurenceInFile.containsKey(val)) { 
      occurenceInFile.put(val, new IntWritable(1)); 
     } else { 
      IntWritable occurence = (IntWritable) occurenceInFile.get(val); 
      occurenceInFile.put(val, new IntWritable(occurence.get() + 1)); 
     } 
     sum += 1; 
    } 
    String result = key.toString() + " ("; 
    boolean flag = false; 
    for (Writable filenameWritable : occurenceInFile.keySet()) { 
     if (flag) { 
      result += ", "; 
     } 
     String filename = ((Text) filenameWritable).toString(); 
     result += filename + "=" + ((IntWritable) occurenceInFile.get(filenameWritable)).toString(); 
     flag = true; 
    } 
    result += ")\nTotal occurence of \"" + key + "\": " + Integer.toString(sum); 

    context.write(key, new Text(result)); 
} 
} 

错误代码此错误代码是显示在标准错误

Error: java.lang.NullPointerException 
at Reduce.reduce(Reduce.java:18) 
at Reduce.reduce(Reduce.java:6) 
at org.apache.hadoop.mapreduce.Reducer.run(Reducer.java:180) 
at org.apache.hadoop.mapred.ReduceTask.runNewReducer(ReduceTask.java:656) 
at org.apache.hadoop.mapred.ReduceTask.run(ReduceTask.java:394) 
at org.apache.hadoop.mapred.YarnChild$2.run(YarnChild.java:172) 
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:1657) 
at org.apache.hadoop.mapred.YarnChild.main(YarnChild.java:166) 

我初学者n来编写一个wordcount程序,但我仍然不知道如何解决这个错误。

+1

查看代码中的第6行和第18行。这是一个字数统计程序?看起来很乱。我确信有可能写一些更清洁的东西。 – duffymo

+0

是的,这不是整个程序,但我认为在我的map.java和wordcount.java(主类) –

+0

没有错误你认为是错误的。有一个错误。 JVM刚刚说清楚了。我相信这可以并且应该写得更清楚。在使用Hadoop进行尝试之前,我建议使用基于JDK8流的版本。如果你不能这么做,那么你很少有机会用大铁锤。先做一些简单的事情。 – duffymo

回答

0

我想行号#18是这一个:

occurenceInFile.put(val, new IntWritable(occurence.get() + 1));

所以有可能的是occurence变量值,这是在以前的行读,是null。另一种变体是occurence.get()的值是null

所以,很难提出什么可能导致它确切的,但你应该调试你的程序并找到,为什么occurenceInFile.get(val);返回null价值。

相关问题