Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将字节数组写入和读取到DataInput和DataOutput流_Java_Hadoop_Bytearray_Hbase_Datainputstream - Fatal编程技术网

Java 如何将字节数组写入和读取到DataInput和DataOutput流

Java 如何将字节数组写入和读取到DataInput和DataOutput流,java,hadoop,bytearray,hbase,datainputstream,Java,Hadoop,Bytearray,Hbase,Datainputstream,Hbase充当Map reduce作业的源和汇。我已经编写了名为(vector writable)的自定义可写类,其中有两个字段 private DoubleVector vector; // It is a Double Array private byte[] rowKey; // The row key of the Hbase 我的映射器将其作为其值发出,因此我在vectorWritable类中实现了写和读方法 @Override public final vo

Hbase充当Map reduce作业的源和汇。我已经编写了名为(vector writable)的自定义可写类,其中有两个字段

private DoubleVector vector; // It is a Double Array
private byte[] rowKey;       // The row key of the Hbase 
我的映射器将其作为其值发出,因此我在vectorWritable类中实现了写和读方法

  @Override
   public final void write(DataOutput out) throws IOException {
   writeVectorCluster(this.vector, this.rowKey, out);       
   }

   @Override
   public final void readFields(DataInput in) throws IOException {
   this.vector = readVector(in);
   this.rowKey = readRowKey(in);
   } 

public static void writeVectorCluster(DoubleVector vector, byte[] rowkey, DataOutput out)
            throws IOException {
        out.writeInt(vector.getLength());
            for (int i = 0; i < vector.getDimension(); i++) {
                out.writeDouble(vector.get(i));
            }
            int length = rowkey.length;
            out.writeInt(length);

           //Is this the right way ?
            out.write(rowkey);
        }


public static DoubleVector readVector(DataInput in) throws IOException {
        int length = in.readInt();
        DoubleVector vector = null;
            vector = new DenseDoubleVector(length);
            for (int i = 0; i < length; i++) {
                vector.set(i, in.readDouble());
            }
        return vector;
    }

   @SuppressWarnings("null")
    public static byte[] readRowKey(DataInput in) throws IOException {
        int length = in.readInt();
        byte [] test = null;
            for (int i = 0; i < length; i++) {
                // getting null pointer exception here
                test[i] = in.readByte();
            }
        return test;
    }

您没有初始化字节数组:

byte [] test = null;
应该是:

byte [] test = new byte[length];
byte [] test = new byte[length];