如何获取带有时间戳的HBase单元版本 [英] How to get timestamped versions of HBase cell

查看:469
本文介绍了如何获取带有时间戳的HBase单元版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Get.setMaxVersions(10)方法返回HBase单元的所有带时间戳的版本,其中10是任意数字(可能是20或5).以下是控制台的主要方法,该方法创建一个表,插入10个随机整数,然后尝试检索所有整数以打印出来.

How do I return all timestamped versions of an HBase cell with the Get.setMaxVersions(10) method where 10 is an arbitrary number (could be something else like 20 or 5)? The following is a console main method that creates a table, inserts 10 random integers, and tries to retrieve all of them to print out.

public static void main(String[] args)
    throws ZooKeeperConnectionException, MasterNotRunningException, IOException, InterruptedException {

final String HBASE_ZOOKEEPER_QUORUM_IP = "localhost.localdomain"; //set ip in hosts file
final String HBASE_ZOOKEEPER_PROPERTY_CLIENTPORT = "2181";
final String HBASE_MASTER = HBASE_ZOOKEEPER_QUORUM_IP + ":60010";

//identify a data cell with these properties
String tablename = "characters";
String row = "johnsmith";
String family = "capital";
String qualifier = "A"; 

//config
Configuration config = HBaseConfiguration.create();
config.clear();
config.set("hbase.zookeeper.quorum", HBASE_ZOOKEEPER_QUORUM_IP);
config.set("hbase.zookeeper.property.clientPort", HBASE_ZOOKEEPER_PROPERTY_CLIENTPORT);
config.set("hbase.master", HBASE_MASTER);

//admin
HBaseAdmin hba = new HBaseAdmin(config);

//create a table
HTableDescriptor descriptor = new HTableDescriptor(tablename);
descriptor.addFamily(new HColumnDescriptor(family));
hba.createTable(descriptor);
hba.close();

//get the table
HTable htable = new HTable(config, tablename);

//insert 10 different timestamps into 1 record
for(int i = 0; i < 10; i++) {
    String value = Integer.toString(i);
    Put put = new Put(Bytes.toBytes(row));
    put.add(Bytes.toBytes(family), Bytes.toBytes(qualifier), System.currentTimeMillis(), Bytes.toBytes(value));
    htable.put(put);
    Thread.sleep(200); //make sure each timestamp is different
}

//get 10 timestamp versions of 1 record
final int MAX_VERSIONS = 10;
Get get = new Get(Bytes.toBytes(row));
get.setMaxVersions(MAX_VERSIONS);
Result result = htable.get(get);
byte[] value = result.getValue(Bytes.toBytes(family), Bytes.toBytes(qualifier));  // returns MAX_VERSIONS quantity of values
String output = Bytes.toString(value);

//show me what you got
System.out.println(output); //prints 9 instead of 0 through 9
}

输出为9(因为循环结束于i = 9,并且在Hue的HBase Browser浏览器Web UI中看不到多个版本.我该怎么做以修复版本,以便得到0的10个单独结果- 9,而不是仅9的结果?

The output is 9 (because the loop ended at i=9, and I don't see multiple versions in Hue's HBase Browser web UI. What can I do to fix the versions so it gives me 10 individual results for 0 - 9 instead of one result of only the number 9?

推荐答案

您应使用 getValue 返回最新值.

You should use getColumnCells on Result to get all versions (depending on MAX_VERSION_COUNT you have set in Get). getValue returns the latest value.

示例代码:

    List<Cell> values = result.getColumnCells(Bytes.toBytes(family), Bytes.toBytes(qualifier));
    for ( Cell cell : values )
    {
        System.out.println( Bytes.toString( CellUtil.cloneValue( cell ) ) );
    }

这篇关于如何获取带有时间戳的HBase单元版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆