How to filter records according to `timestamp` in Spring Data Hadoop?

416 Views Asked by At

I have a hbase table with a sample record as follows:

  03af639717ae10eb743253433147e133                 column=u:a, timestamp=1434300763147, value=apple
  10f3d7f8fe8f25d5bdf52343a2601227                 column=u:a, timestamp=1434300763148, value=mapple
  20164b1aff21bc14e94623423a9d645d                 column=u:a, timestamp=1534300763142, value=papple
  44d1cb38271362d20911a723410b2c67                 column=u:a, timestamp=1634300763141, value=scapple

I am lost as I was trying to pull out the row values according to the timestamp. I am using spring data hadoop. I was only able to fetch all the records using below code:

 private static final byte[] CF_INFO = Bytes.toBytes("u");
 private static final byte[] baseUrl = Bytes.toBytes("a");

 List<Model> allNewsList
            = hbaseTemplate.find(tableName, columnFamily, new RowMapper<News>()
            {
                @Override
                public Model mapRow(Result result, int rowNum)
                throws Exception 
                {
                    String dateString = TextUtils.getTimeStampInLong(result.toString());
                    String rowKey = Bytes.toString(result.getRow());
                    return new Model(                                
                        rowKey,
                        Bytes.toString(result.getValue(CF_INFO, col_a)
                    );
                }
            });

How can I apply filter such that I would be able to get records within timestamp [1434300763147,1534300763142].

2

There are 2 best solutions below

1
On

The problem was solved using Scanner object from Hbase Client.

0
On

Hopefully this would help someone someday.

final org.apache.hadoop.hbase.client.Scan scan = new Scan();
scan.setTimeRange(1434300763147,1534300763142);
final List<Model> yourObjects = hbaseTemplate.find(tableName, scan, mapper);

Also, worth a mention, the max value of the timerange is exclusive, so if you want records with that timestamp to be returned, make sure to increment the max value of timerange by 1.