Search code examples
hbase

Is there any way to integer sort in hbase?


As far as I know , HBase supports only the bytes array for write purpose.

Is there any way to receive the scanned results in integer sort order and not as lexicographical order

for example

1

2

10

not as

1

10

2

some people said we can store like

00001

00002

00010

Is there any alternative way?


Solution

  • From org.apache.hadoop.hbase.util.Bytes:

      /**
       * Convert an int value to a byte array.  Big-endian.  Same as what DataOutputStream.writeInt
       * does.
       *
       * @param val value
       * @return the byte array
       */
      public static byte[] toBytes(int val) {
        byte [] b = new byte[4];
        for(int i = 3; i > 0; i--) {
          b[i] = (byte) val;
          val >>>= 8;
        }
        b[0] = (byte) val;
        return b;
      }
    

    So you can clearly see that you can use this utility class to convert your keys just before you save them. When fetched, they will be sorted by their integer value. Effectively this handle the padding you mentioned in your comment.