我有一项任务需要将多维数组写入 HDFS。由于双数组是可序列化的,我认为我可以通过使用以下代码来实现:
FileSystem fs=FileSystem.get(context.getConfiguration());
FSDataOutputStream dos = fs.create(new Path(directory+"/Predictions"));
ObjectWritable obj=new ObjectWritable(double.class,labelPredictions);
obj.write(dos);
dos.close();
其中 directory 是 hdfs 中的路径,labelPredictions 是多维数组。但是当我这样做时,我得到了这个错误:
java.lang.ClassCastException: [[D cannot be cast to java.lang.Double
at org.apache.hadoop.io.ObjectWritable.writeObject(ObjectWritable.java:150)
at org.apache.hadoop.io.ObjectWritable.write(ObjectWritable.java:70)
at MapReduce.MapReduce$Reduce.cleanup(MapReduce.java:181)
at org.apache.hadoop.mapreduce.Reducer.run(Reducer.java:178)
at org.apache.hadoop.mapred.ReduceTask.runNewReducer(ReduceTask.java:648)
at org.apache.hadoop.mapred.ReduceTask.run(ReduceTask.java:416)
at org.apache.hadoop.mapred.Child$4.run(Child.java:259)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:416)
at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1059)
at org.apache.hadoop.mapred.Child.main(Child.java:253)
我的 double 组是否需要实现 Writable 才能执行此操作,还是有其他方法?
最佳答案
也许您应该使用 double[][].class
而不是 double.class
...? (取决于您使用的数组的维数——您说的是“多维”,但不清楚实际维数是多少。)
关于java - Hadoop:如何将多维数组写入 HDFS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9959113/