gpt4 book ai didi

java - 不了解 MapReduce NPE

转载 作者:可可西里 更新时间:2023-11-01 14:25:04 25 4
gpt4 key购买 nike

这是我收到的错误:

    14/02/28 02:52:43 INFO mapred.JobClient: Task Id : attempt_201402271927_0020_m_000001_2, Status : FAILED
java.lang.NullPointerException
at org.apache.hadoop.mapred.MapTask$MapOutputBuffer.init(MapTask.java:843)
at org.apache.hadoop.mapred.MapTask.createSortingCollector(MapTask.java:376)
at org.apache.hadoop.mapred.MapTask.access$100(MapTask.java:85)
at org.apache.hadoop.mapred.MapTask$NewOutputCollector.<init>(MapTask.java:584)
at org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:656)
at org.apache.hadoop.mapred.MapTask.run(MapTask.java:330)
at org.apache.hadoop.mapred.Child$4.run(Child.java:268)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:396)
at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1408)
at org.apache.hadoop.mapred.Child.main(Child.java:262)

我已经将我的代码注释掉以基本上接受典型的 LongWritable 和 Text,然后我只输出一个常量 IntWritable 1 和一个空的天气类(自定义类):

这是我的映射器类:

public class Map extends Mapper<LongWritable, Text, IntWritable, Weather> {

private IntWritable id = new IntWritable(1);
private Weather we = new Weather();

public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
//String s;
//String line = value.toString();

//int start[] = {0,18,31,42,53,64,74,84,88,103};
//int end[] = {6,22,33,44,55,66,76,86,93,108};

//if(line.length() > 108) {
// create the object to hold our data
// getStuff()
// parse the string

// push the object onto our data structure
context.write(id, we);
//}
}

这是我的 reducer :

public class Reduce extends Reducer<IntWritable, Weather, IntWritable, Text> {
private Text text = new Text("one");
private IntWritable one = new IntWritable(1);
public void reduce(IntWritable key, Iterable<Weather> weather, Context context)
throws IOException, InterruptedException {
//for(Weather w : weather) {
// text.set(w.toString());
context.write(one, text);
}
}

这是我的主要内容:

public class Skyline {

public static void main(String[] args) throws IOException{
//String s = args[0].length() > 0 ? args[0] : "skyline.in";
Path input, output;
Configuration conf = new Configuration();

conf.set("io.serializations", "org.apache.hadoop.io.serializer.JavaSerialization,"
+ "org.apache.hadoop.io.serializer.WritableSerialization");
try {
input = new Path(args[0]);
} catch(ArrayIndexOutOfBoundsException e) {
input = new Path("hdfs://localhost/user/cloudera/in/skyline.in");
}
try {
output = new Path(args[1]);
//FileSystem.getLocal(conf).delete(output, true);
} catch(ArrayIndexOutOfBoundsException e) {
output = new Path("hdfs://localhost/user/cloudera/out/");
//FileSystem.getLocal(conf).delete(output, true);
}

Job job = new Job(conf, "skyline");

job.setJarByClass(Skyline.class);

job.setOutputKeyClass(IntWritable.class);
job.setOutputValueClass(Weather.class);

job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);

job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);

FileInputFormat.addInputPath(job, input);
FileOutputFormat.setOutputPath(job, output);
try {
job.waitForCompletion(true);
} catch(InterruptedException e) {
System.out.println("Interrupted Exception");
} catch(ClassNotFoundException e) {
System.out.println("ClassNotFoundException");
}
}
}

这是我的天气类的示例:

public class Weather {

private in stationId;

public Weather(){}

public int getStation(){return this.stationID;}
public void setStation(int r){this.stationID = r}
//...24 additional things of ints, doubles and strings
}

我已经无计可施了。在这一点上,我有一个程序的外壳,它什么都不做,但仍然收到错误。我已经阅读了 Java 泛型,以确保我正确地使用了它们(我想我是),我对 MapReduce 范例非常陌生,但这个程序只是一个外壳,从 MapReduce 教程( https://hadoop.apache.org/docs/r1.2.1/mapred_tutorial.html#Walk-through ).

最佳答案

问题是您用于 map() 输出/reduce() 输入的类 Weather 没有实现可写。这将阻止默认的 SerializationFactory 处理您的值。

潜在的概念性问题是 Hadoop 不知道如何将您的数据类型序列化到磁盘并将其读回。这是一个强制性步骤,因为在将数据从 map 任务移动到 reducer 之前必须持久化数据(通常这两者可以在不同的节点上运行)。

所以您要做的是实现 Writable 并在您的自定义数据类型中添加序列化例程。

关于java - 不了解 MapReduce NPE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22135566/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com