如何使用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 Browser Web UI中看不到多个版本。我该怎么做以修复版本,以便为0-9而不是10给出单独的结果)数字9的一项结果?
您应该在Result上使用getColumnCells来获取所有版本(取决于您在Get中设置的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 ) ) ); }