7

使用 Hbase API (Get/Put) 或 HBQL API,是否可以检索特定列的时间戳?

4

4 回答 4

13

假设您的客户端已配置并且您有一个表设置。执行 get 会返回Result

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

Result 由KeyValue支持。KeyValues 包含时间戳。您可以使用 list() 获取 KeyValue 列表,也可以使用 raw() 获取数组。KeyValue 有一个获取时间戳的方法。

result_foo.raw()[0].getTimestamp()
于 2011-11-30T18:30:12.253 回答
1

我认为以下会更好:

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

因为 Result#getValue(family, qualifier) 被实现为

public byte[] getValue(byte[] family, byte[] qualifier) {
        KeyValue kv = this.getColumnLatest(family, qualifier);
        return kv == null ? null : kv.getValue();
    }
于 2019-03-14T02:59:01.060 回答
1

@codingFoo 的答案假设所有单元格的所有时间戳都相同,但 op 的问题是针对特定列的。在这方面,类似于@peibin wang 的回答,如果您想要列的最后一个时间戳,我会提出以下建议:

在 Result 对象上使用getColumnLatestCell方法,然后getTimestamp像这样调用该方法:

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

如果您想访问特定的时间戳,您可以使用getColumnCells返回指定列的所有单元格,但是您必须在带有 a 的单元格之间进行选择get(int index),然后调用getTimestamp()

于 2021-09-16T16:58:49.000 回答
0
result_foo.rawCells()(0).getTimestamp

是个好风格

于 2015-11-16T11:02:50.957 回答