Domanda

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.

È stato utile?

Soluzione

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[])
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top