0

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.

Community
  • 1
  • 1
mvera
  • 904
  • 12
  • 23

1 Answers1

1

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.

nilsmagnus
  • 2,210
  • 23
  • 33
  • Thanks Nils. If I use this transformaton for i1 and i2 I will obtain byte[] b1 and b2. If i1 < i2 then I will have b1 < b2 if I compare them lexicographically as Hbase will do to comapre keys during scan ? – mvera Oct 28 '16 at 10:28
  • I guess so. (the size of b1==b2, but the contents will be lexicographical comparable in the order you want) But hbase will maintain the lexicographical order for you on new inserts/deletions, so you do not have to worry about this when scanning. – nilsmagnus Oct 28 '16 at 11:29