java—如何将字节数组写入和读取到datainput和dataoutput流

0ejtzxu1  于 2021-06-03  发布在  Hadoop
关注(0)|答案(1)|浏览(430)

hbase充当map reduce作业的源和汇。我已经编写了我的自定义可写类(vector writable),它有两个字段。

private DoubleVector vector; // It is a Double Array
private byte[] rowKey;       // The row key of the Hbase

我的Map器将此作为其值发出,因此我在我的vectorwritable类中实现了write和read方法

@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;
    }

当我试图从输入流中读取rowkey时,会出现nullpointerexception。readvector方法工作得很好,我得到了正确的值。
如何在datainput流中写入字节数组,以便在输出流中检索它
更新:这是我的rowkey方法的更新,运行良好。谢谢@perception

public static byte[] readRowKey(DataInput in) throws IOException {  
        int length = in.readInt();
        byte[] theBytes = new byte[length];
        in.readFully(theBytes); 
        return theBytes;
    }
wixjitnu

wixjitnu1#

您没有初始化字节数组:

byte [] test = null;

应该是:

byte [] test = new byte[length];

相关问题