2

How can i store and retrieve primitive data types using hbase api.? My task is to save random events on hbase that contains unpredictable data types which are generated randomly. and need to retrieve them after whenever i want? can someone help me out this please. because i'm really new to hbase and this stuff.

Chamika Kasun
  • 96
  • 3
  • 16

1 Answers1

5

This is how you put data into a HBase table :

Configuration conf = HBaseConfiguration.create();
HTable table = new HTable(conf, "TABLE_NAME");
Put p = new Put(rowKey);
p.add(Bytes.toBytes("cf"), Bytes.toBytes("c1"), Bytes.toBytes("VALUE"));
table.put(p);

You don't have to worry about the type of the data. However, you need to keep in mind that anything which goes inside HBase goes as an array of bytes. So, while fetching the data back from HBase you need to convert it back into the suitable type because you will be getting a bytearray everytime. This can be done using various overloaded methods provided by the Bytes class. Like this :

Bytes.toString(byte[])
Bytes.toFloat(byte[])
Bytes.toLong(byte[])
Tariq
  • 34,076
  • 8
  • 57
  • 79
  • Here what i want to do is, for a example lets say i want to store a float number 34.5. so first i want to store this and when i retrieve this value again need to recognize this as a float value. not as a byte array – Chamika Kasun Apr 22 '14 at 12:41
  • Use **Bytes.toFloat(byte[])** while fetching your data. – Tariq Apr 22 '14 at 13:59
  • Can you please help me on this one also : http://stackoverflow.com/questions/23333060/how-to-create-a-column-family-in-a-selected-cluster-in-hbase – Chamika Kasun Apr 28 '14 at 05:25