java - 如何获取 HBase 单元的时间戳版本

标签 java hbase

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

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 浏览器 Web UI 中没有看到多个版本。我可以做些什么来修复版本,以便它为我提供 10 个 0 的单独结果 - 9 而不是只有数字 9 的一个结果?

最佳答案

您应该使用getColumnCells在结果上获取所有版本(取决于您在获取中设置的 MAX_VERSION_COUNT )。 getValue返回最新值。

示例代码:

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

关于java - 如何获取 HBase 单元的时间戳版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24846243/

相关文章:

python - 在Ubuntu 13.10中安装Hue时出现无站点包错误

hadoop - Hbase- 即使删除列族后 Hadoop DFS 大小也没有减少

java - 从 Android Activity 中调用 Fragment 方法

java - Java json-path 库的问题

apache - 在VM上安装时发生HBase错误

hadoop - 如何区分创建 HBase 或任何其他进程的文件或目录?

hadoop - 我应该使用 PIG 从 HIVE 将数据加载到 HBase 还是有更好的方法?

java - 用 Java 处理数百万条数据库记录

java - Xuggler H264 FPS 编码问题

java - 如果参数的值为字符串或数组,则验证 JSON 字符串