如何将Double []插入HBase?

时间:2015-02-02 08:33:06

标签: hadoop hbase

例如, 如果我想插入一个double []之类的, Double [] dArr = {10.23,25.1,30.5,45.3}; 进入HBase表。 你能告诉我如何将它插入hbase ??

1 个答案:

答案 0 :(得分:1)

您可以存储您想要的任何内容,只需将其序列化为字节[]:

Double[] dArr={10.23,25.1,30.5,45.3};
byte[] value = new byte[0];
byte[] family = "f".getBytes();
byte[] column = "d".getBytes();
for (Double d:dArr) {
    value = Bytes.add(value, Bytes.toBytes(d));
}
Put put = new Put( rowKey );
put.add(family, column, Bytes.toBytes(value));
...

读取值时必须反序列化数据(从byte []转换为Double [])

你可以做的另一件事是将每个Double(作为byte [])存储在它自己的列中(d0到dX)

Double[] dArr={10.23,25.1,30.5,45.3};
Put put = new Put( rowKey );
byte[] family = "f".getBytes();
byte[] column = "d".getBytes();
int i=0;
for (Double d:dArr) {
    put.add( family, Bytes.add(column, Bytes.toBytes(i)), Bytes.toBytes(d));
    i++;
}
...