mvera mvera - 1 year ago 167
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

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
) in
so that a scan will return the expected values if I use the same transformation for startrow and endrow ?


Nils gave the anwser and I found a confirmation here:

Java Comparator for byte array (lexicographic)

Hbase extracts
and compare them.

Answer Source

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

From byte-array to int:


From int to byte-array:


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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download