如何获取hbase cell的时间戳版本

jucafojl  于 2021-06-09  发布在  Hbase
关注(0)|答案(2)|浏览(372)

如何使用get.setmaxversions(10)方法返回hbase单元格的所有时间戳版本,其中10是任意数字(可以是20或5之类的其他值)?下面是一个控制台main方法,它创建一个表,插入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浏览器webui中没有看到多个版本)。我能做些什么来修正版本,让它给我10个0-9的单独结果,而不是一个数字9的结果?

jjjwad0x

jjjwad0x1#

这是一种不推荐使用的方法,它与我目前正在使用的hbase版本相匹配。

List<KeyValue> kvpairs = result.getColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
String line = "";
for(KeyValue kv : kvpairs) {
    line += Bytes.toString(kv.getValue()) + "\n";
}
System.out.println(line);

然后,更进一步,需要注意的是,在创建表时必须调用setmaxversions方法,以允许在单元格中插入三个以上的默认值。以下是更新的表创建:

//create a table based on variables from question above
HTableDescriptor tableDescriptor = new HTableDescriptor(tablename);
HColumnDescriptor columnDescriptor = new HColumnDescriptor(columnFamily);
columnDescriptor.setMaxVersions(MAX_VERSIONS);
tableDescriptor.addFamily(columnDescriptor);
hba.createTable(tableDescriptor);
hba.close();
q5iwbnjs

q5iwbnjs2#

您应该在result中使用getcolumncells来获取所有版本(取决于在get中设置的最大版本计数)。getvalue返回最新的值。
示例代码:

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

相关问题