mvera mvera - 1 month ago 19
Java Question

Hbase use integer as row key and lexicographical order

I'm designing an Hbase schema where the row key should be an integer. I intend to use scan API from java with startrow and endrow with integer values.

I guess I can transform my integers in a String with

'0'
padding to respect the lexicographical order, but my keys will be much bigger than if I use the binary representation of an integer.

How can I transform my integer (let's say an
int
) in
byte[]
so that a scan will return the expected values if I use the same transformation for startrow and endrow ?

Answer:

Nils gave the anwser and I found a confirmation here:

Java Comparator for byte array (lexicographic)

Hbase extracts
int
from
byte[]
and compare them.

Answer

You can do this by using org.apache.hadoop.hbase.util.Bytes from the hbase-client library.

From byte-array to int:

Bytes.toInt(byteArray)

From int to byte-array:

Bytes.toBytes(intvalue)

I have actually made an online tool to generate the hex values I need to query row-ids in hbase shell right here.

Comments