i map.java 类我尝试通过这个发送文件名
context.write(new Text(stringWord), new Text(fileName))
回避.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));
}
}
错误代码此错误代码显示在 STDERR 中
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)
我开始编写字数统计程序,但我仍然不知道如何修复此错误。
我想行号#18是这个:
occurenceInFile.put(val, new IntWritable(occurence.get() + 1));
因此,在上一行读取occurence
变量值可能是null
。另一种变体是occurence.get()
值为 null
.
因此,很难确切地建议导致它的原因,但您应该调试程序并找到为什么occurenceInFile.get(val);
可以返回null
值。