【问题标题】:Not understanding a MapReduce NPE不了解 MapReduce NPE
【发布时间】:2014-04-03 20:09:54
【问题描述】:

这是我收到的错误:

    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);
    //}
}

这是我的减速机:

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");
        }
    }
}

这是我的 Weather 类的示例:

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 教程修改(@ 987654321@).

【问题讨论】:

  • 能否包含Weather 类的定义?
  • 这是一个真正简单的类,只有 25 个 get/set 方法。构造函数中没有做任何工作。这是从程序周围的单个站点移动 25 个天气变量的最简单方法。代码见上。
  • 我怀疑您需要在 Weather 类中实现 org.apache.Hadoop.io.Writable 才能使其正常工作。如果你想传递不可写的东西,你需要编写更多的自定义代码。
  • 我刚刚删除了 Weather 类的使用,所以我只是在传递 IntWritables,但我仍然得到相同的错误,所以在任何你看到“天气”的地方都用“IntWritable”替换它。跨度>
  • 如果您绝对确定已删除 Weather 的所有用途并用 IntWritables 替换它们(包括您设置工作的位置),那么我没有任何其他想法,抱歉。设置地图输出器时显然会引发错误。作为一般补救措施,我可以建议按照教程从头开始,如果可行,则通过引入自定义代码逐步修改它。您可能能够更轻松地检测到错误正在蔓延的位置。

标签: java generics hadoop mapreduce cloudera


【解决方案1】:

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

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

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

【讨论】:

  • 我的天气类实现了可写接口,并且我有接口需要的 shell 函数,它可以工作!非常感谢。
猜你喜欢
  • 2014-10-05
  • 1970-01-01
  • 1970-01-01
  • 2013-02-25
  • 1970-01-01
  • 1970-01-01
  • 2021-01-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多