如何使用TwoDArrayWritable从mapper中发出2D双数组

时间:2013-11-01 06:23:35

标签: hadoop mapreduce

我想使用TwoDArrayWritable作为值发出2D双数组。

如何撰写context.write(key , )

修改

Reducer如何get他们在二维双数组和print值。

Wrote

中的Mapper
row = E.length;
col = E[0].length;
                     TwoDArrayWritable array = new TwoDArrayWritable (DoubleWritable.class);
                     DoubleWritable[][] myInnerArray = new DoubleWritable[row][col];
                     // set values in myInnerArray
                     for (int k1 = 0; k1 < row; k1++) {
                        for(int j1=0;j1< col;j1++){
                            myInnerArray[k1][j1] = new DoubleWritable(E[k1][j1]);

                    }
                 array.set(myInnerArray);
                 context.write(clusterNumber, array);

但显示Nullpointer exception

13/11/01 16:34:07 INFO mapred.LocalJobRunner: Map task executor complete.
13/11/01 16:34:07 WARN mapred.LocalJobRunner: job_local724758890_0001
java.lang.Exception: java.lang.NullPointerException
    at org.apache.hadoop.mapred.LocalJobRunner$Job.run(LocalJobRunner.java:404)
Caused by: java.lang.NullPointerException
    at org.apache.hadoop.io.TwoDArrayWritable.write(TwoDArrayWritable.java:91)
    at org.apache.hadoop.io.serializer.WritableSerialization$WritableSerializer.serialize(WritableSerialization.java:100)
    at org.apache.hadoop.io.serializer.WritableSerialization$WritableSerializer.serialize(WritableSerialization.java:84)
    at org.apache.hadoop.mapred.MapTask$MapOutputBuffer.collect(MapTask.java:945)
    at org.apache.hadoop.mapred.MapTask$NewOutputCollector.write(MapTask.java:601)
    at org.apache.hadoop.mapreduce.task.TaskInputOutputContextImpl.write(TaskInputOutputContextImpl.java:85)
    at org.apache.hadoop.mapreduce.lib.map.WrappedMapper$Context.write(WrappedMapper.java:106)
    at edu.Mapper.map(Mapper.java:277)


Mapper.java:277 : context.write(clusterNumber, array);

2 个答案:

答案 0 :(得分:0)

 TwoDArrayWritable array = new TwoDArrayWritable (DoubleWritable.class);
 DoubleWritable[][] myInnerArray = new DoubleWritable[10][];
 // set values in myInnerArray
 array.set(myInnerArray);
 context.write(key, array);

我相信NPE是因为数组的某些元素为null。从您发布的代码我想知道ecol等于col

答案 1 :(得分:0)

伙计这个NPE异常在你的数组中发生露水到某个空值。如果数组中没有任何值,那么就会将null露水置于物体中。您可以在调试模式下进行检查。所以你的任务是检查你的数组中没有空值。