HBase可作为MapReduce作业的数据源和数据汇。我编写了自定义的Writable类,叫做“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;
}
当我尝试从输入流中读取rowKey时,出现了一个NullPointerException。虽然readVector方法工作良好,我也得到了正确的值。
我应该如何将字节数组写入DataInput Stream,以便在我的Output streams中检索它
更新:已解决 这是我的rowKey方法的更新版,它工作得很好。感谢@Perception
public static byte[] readRowKey(DataInput in) throws IOException {
int length = in.readInt();
byte[] theBytes = new byte[length];
in.readFully(theBytes);
return theBytes;
}