0
votes

I have been working in HBase for last some weeks. My question is:

I have a HBase table with 100 records and each record having three columns in one column family and there is just one column family. Now I want to retrieve the rows on the basis of timestamp. Means the row which is added at the last should be retrieved first. Its like (LIFO). Now is this functionality available in HBase? If yes then how can I do it? I am using 0.98.3.

NOTE: While inserting data I did not mention timestamps manually.

I am trying to do it in Java language.

Regards

1

1 Answers

0
votes

Rows are naturally sorted lexicographically by the row key (ascending), timestamp is not involved at all when performing full table scans, the first row retrieved will be the lowest one.

This would be the order in case of string row keys:

STRING  ROW
0       \x30
00      \x30\x30
0000    \x30\x30\x30\x30
0001    \x30\x30\x30\x31
0002    \x30\x30\x30\x32
...
0010    \x30\x30\x31\x30
1       \x31
10      \x31\x30
2       \x32
a       \x61
ab      \x61\x62
...
zzzz    \x7A\x7A\x7A\x7A

This would be the order in case of 4 byte signed integer row keys:

INT     ROW
1       \x00\x00\x00\x01
2       \x00\x00\x00\x02
3       \x00\x00\x00\x03
4       \x00\x00\x00\x04
...
100     \x00\x00\x00\x64
...
10000   \x00\x00\x27\x10
...
MAX_INT \x7F\xFF\xFF\xFF

If you need the scan to work as a LIFO, you have to include the inverted timestamp as a prefix for your rowkey (although this design is not recommended for heavy write environments due of hotspotting).

byte[] rowKey = Bytes.add( Byte.toBytes( Long.MAX_VALUE - System.currentTimeMillis() ), "-myRow".getBytes());

If you don't invert the timestamp, it will work as a LILO.

For more information take a look at this section of the HBase Book: https://hbase.apache.org/book/rowkey.design.html