Support Questions

Find answers, ask questions, and share your expertise

Get ttl from HBase record

avatar
New Contributor

Hi,

 

I am trying to understand how can I get ttl of HBase record using Java API 1.0.0-cdh5.4.5.

I can add ttl using Put.setTTL() function, which actually is defined in Mutation.

http://archive.cloudera.com/cdh5/cdh/5/hbase/apidocs/org/apache/hadoop/hbase/client/Mutation.html#se...

 

But how can I get back my TTL for this record?

Could you show me a snippet of the code to get a record by the key and get TTL for it.

 

Also I explicitly set TTL for some records and for some records I use table's default TTL.

Not sure if I should get ti the same way or they are different.

 

 

1 ACCEPTED SOLUTION

avatar
Mentor
2 REPLIES 2

avatar
Mentor
The TTL values are stored as Cell-level tags [1].

To retrieve them back from their Cell, fetch the Cell via Get/etc. and then
use the available Tags-relevant APIs on the Cell object:
http://archive.cloudera.com/cdh5/cdh/5/hbase/apidocs/org/apache/hadoop/hbase/Cell.html#getTagsArray(...
and deserialise the array of tags via
http://archive.cloudera.com/cdh5/cdh/5/hbase/apidocs/org/apache/hadoop/hbase/CellUtil.html#tagsItera...

[1] -
https://github.com/cloudera/hbase/blob/cdh5.4.5-release/hbase-server/src/main/java/org/apache/hadoop...

avatar
New Contributor

Hello,

 

So the Cell TTL is stored via Cell TTL tags.When I retrive it fails to get Cell Tags for TTL but the Cell TTL feature does work . Can you point me to what I am doing wrong to retrieve the Cell TTL . We are using hbase-1.0.0-cdh5.4.2.

 

 I fail to get the Cell Tag for TTL when I iterate over the cell Tags.

 

During PUT we do

put.setTTL( );

 

 

Table table..

Get get = new Get(Bytes.toBytes(<key>));

Result rs = table.get(get);

if (rs!=null) {

Cell cell = rs.getColumnCells(..);

Iterator<Tag> i = CellUtil.tagsIterator(cell.getTagsArray(), cell.getTagsOffset(),

cell.getTagsLength());

while (i.hasNext()) {

Tag t = i.next();

if (TagType.TTL_TAG_TYPE == t.getType()) {

long ts = cell.getTimestamp();

assert t.getTagLength() == Bytes.SIZEOF_LONG;

long ttl = Bytes.toLong(t.getBuffer(), t.getTagOffset(), t.getTagLength());

 

System.out.println(ttl);

}

  }

 

Thanks in advance.