【发布时间】:2016-08-16 11:53:57
【问题描述】:
我 map.java 类我尝试通过这个发送文件名
context.write(new Text(stringWord), new Text(fileName))
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)
我开始写一个wordcount程序,但我仍然不知道如何解决这个错误。
【问题讨论】:
-
查看代码中的第 6 行和第 18 行。这是一个字数统计程序?看起来一团糟。我确信可以写出更干净的东西。
-
是的,这不是整个程序,但我认为我的 map.java 和 wordcount.java(主类)没有错误
-
你想错了。有一个错误。 JVM刚刚说清楚了。我相信这可以而且应该写得更清楚。在尝试使用 Hadoop 之前,我会推荐一个基于 JDK8 流的版本。如果你不能做到这一点,那么你用大锤子的机会就很小。先做一些简单的事情。
-
我喜欢...我的代码没有错误,为什么会崩溃?
标签: java hadoop mapreduce stderr