Kunnal Bhardwaj
Kunnal Bhardwaj

Reputation: 11

How to insert Double[] into HBase?

For Example, if i want to insert a double[] like, Double[] dArr={10.23,25.1,30.5,45.3}; into HBase table. Could you plzz tell me how to insert it into hbase??

Upvotes: 0

Views: 781

Answers (1)

Rubén Moraleda
Rubén Moraleda

Reputation: 3067

You can store anything you want, you just have to serialize it to a byte[]:

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));
...

You'll have to unserialize the data when you read the value (convert from byte[] to Double[])

Another thing you can do is store each Double (as byte[]) in its own column (d0 to 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++;
}
...

Upvotes: 1

Related Questions