Java 如何获取带时间戳的HBase单元版本

Java 如何获取带时间戳的HBase单元版本,java,hbase,Java,Hbase,如何使用Get.setMaxVersions(10)方法返回HBase单元的所有时间戳版本,其中10是任意数字(可能是20或5之类的其他数字)?下面是一个控制台main方法,它创建一个表,插入10个随机整数,并尝试检索所有整数以打印输出 public static void main(String[] args) throws ZooKeeperConnectionException, MasterNotRunningException, IOException, Interrupted

如何使用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
}
publicstaticvoidmain(字符串[]args)
抛出ZookePerConnectionException、MasterNotRunningException、IOException、InterruptedException{
最后一个字符串HBASE\u ZOOKEEPER\u QUORUM\u IP=“localhost.localdomain”//在主机文件中设置IP
最终字符串HBASE\u ZOOKEEPER\u PROPERTY\u CLIENTPORT=“2181”;
最终字符串HBASE\u MASTER=HBASE\u ZOOKEEPER\u QUORUM\u IP+“:60010”;
//使用这些属性标识数据单元
字符串tablename=“characters”;
String row=“johnsmith”;
String family=“capital”;
字符串限定符=“A”;
//配置
Configuration config=HBaseConfiguration.create();
config.clear();
config.set(“hbase.zookeeper.quorum”,hbase\u zookeeper\u quorum\u IP);
config.set(“hbase.zookeeper.property.clientPort”,hbase\u zookeeper\u property\u clientPort);
config.set(“hbase.master”,hbase\u master);
//管理员
HBaseAdmin hba=新的HBaseAdmin(配置);
//创建一个表
HTableDescriptor描述符=新的HTableDescriptor(tablename);
descriptor.addFamily(新的HColumDescriptor(family));
createTable(描述符);
hba.close();
//收拾桌子
HTable HTable=新的HTable(配置,表名);
//在一条记录中插入10个不同的时间戳
对于(int i=0;i<10;i++){
字符串值=整数。toString(i);
Put Put=新的Put(字节数。toBytes(行));
put.add(Bytes.toBytes(family)、Bytes.toBytes(qualifier)、System.currentTimeMillis()、Bytes.toBytes(value));
htable.put(put);
Thread.sleep(200);//确保每个时间戳都不同
}
//获取1条记录的10个时间戳版本
最终int MAX_版本=10;
Get Get=新的Get(字节数.toBytes(行));
get.setMaxVersions(最大版本);
结果=htable.get(get);
byte[]value=result.getValue(Bytes.toBytes(family),Bytes.toBytes(qualifier));//返回值的最大数量
字符串输出=字节。toString(值);
//让我看看你有什么
System.out.println(输出);//打印9而不是0到9
}
输出为9(因为循环结束于i=9,并且我在Hue的HBase浏览器web UI中看不到多个版本。我可以做些什么来修复版本,以便它为0-9提供10个单独的结果,而不是仅一个数字9的结果?

您应该使用“结果”来获取所有版本(取决于您在get中设置的最大版本数)。返回最新的值

示例代码:

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

这是一种不推荐使用的方法,与我目前正在使用的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);

谢谢,Kapil。你为我指明了正确的方向。我的HBase版本使用了旧的不推荐的方法。我将包括基于你与我共享的API工作的代码。
//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();