Retrieving timestamp from hbase row
Asked Answered
G

4

7

Using Hbase API (Get/Put) or HBQL API, is it possible to retrieve timestamp of a particular column?

Gymnasiast answered 30/11, 2011 at 5:58 Comment(0)
B
13

Assuming your client is configured and you have a table setup. Doing a get returns a Result

Get get = new Get(Bytes.toBytes("row_key"));
Result result_foo = table.get(get);

A Result is backed by a KeyValue. KeyValues contain the timestamps. You can get either a list of KeyValues with list() or get an array with raw(). A KeyValue has a get timestamp method.

result_foo.raw()[0].getTimestamp()
Boyceboycey answered 30/11, 2011 at 18:30 Comment(2)
does [0] indicate the first column in hbase? is there a way to the timestamp for a particular specified column name?Mcdaniel
It has been long enough that I dont recall any details from those docs. Things have likely changed since then. Best thing is to fire up a console for you hbase setup and poke around.Boyceboycey
A
1

@codingFoo's answer assumes all timestamps are the same for all cells, but op's question was for a specific column. In that respect, similar to @peibin wang's answer, I would propose the following if you would like the last timestamp for your column:

Use the getColumnLatestCell method on your Result object, and then call the getTimestamp method like so:

Result res = ...
res.getColumnLatestCell(Bytes.toBytes("column_family"), Bytes.toBytes("column_qualifier")).getTimestamp();

If you want access to a specific timestamp you could use the getColumnCells which returns all cells for a specified column, but then you will have to choose between the cells with a get(int index) and then call getTimestamp()

Allergic answered 16/9, 2021 at 16:58 Comment(0)
P
0
result_foo.rawCells()(0).getTimestamp

is a good style

Plosion answered 16/11, 2015 at 11:2 Comment(1)
still need square braces around the index, e.g. rawCells()[0]Alysiaalyson
N
0

I think the follow will be better:

KeyValue kv = result.getColumnLatest(family, qualifier);
String status = Bytes.toString(kv.getValue());
Long timestamp = kv.getTimestamp();

since Result#getValue(family, qualifier) is implemented as

public byte[] getValue(byte[] family, byte[] qualifier) {
        KeyValue kv = this.getColumnLatest(family, qualifier);
        return kv == null ? null : kv.getValue();
    }
Niddering answered 14/3, 2019 at 2:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.