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程序,但我仍然不知道如何解決這個錯誤。
查看代碼中的第6行和第18行。這是一個字數統計程序?看起來很亂。我確信有可能寫一些更清潔的東西。 – duffymo
是的,這不是整個程序,但我認爲在我的map.java和wordcount.java(主類) –
沒有錯誤你認爲是錯誤的。有一個錯誤。 JVM剛剛說清楚了。我相信這可以並且應該寫得更清楚。在使用Hadoop進行嘗試之前,我建議使用基於JDK8流的版本。如果你不能這麼做,那麼你很少有機會用大鐵錘。先做一些簡單的事情。 – duffymo