27902

How to get timestamped versions of HBase cell

Question:

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 }

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?

Answer1:

You should use <a href="https://hbase.apache.org/apidocs/org/apache/hadoop/hbase/client/Result.html#getColumnCells(byte[],%20byte[])" rel="nofollow">getColumnCells</a> on Result to get all versions (depending on MAX_VERSION_COUNT you have set in Get). <a href="https://hbase.apache.org/apidocs/org/apache/hadoop/hbase/client/Result.html#getValue(byte[],%20byte[])" rel="nofollow">getValue</a> returns the latest value.

Sample Code:

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

Answer2:

This is a deprecated approach which matches the version of HBase I am currently working on.

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);

Then, going one step further, it is important to note the setMaxVersions method must be called at table creation to allow for more than a default three values to be inserted into a cell. Here's the updated table creation:

//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();

Recommend

  • How to get timestamped versions of HBase cell
  • implement mutex in node.js
  • How to achieve fault tolerance(Recovery) with TaskMangers of Apache-Flink?
  • Issue on Spark Streaming data put data into HBase
  • Tutorial or example for zookeeper C binding [closed]
  • How to ensure a kafka cluster is fully up?
  • Override contextPath on solr start from command line
  • Error with zookeeper & storm
  • How change Kafka committed consumer offset with required offset
  • fail to connect to Hbase with java api
  • Error on zookeeper
  • 'Object.ReferenceEquals' is always false because it is called with a value type
  • ArrayList Search .net
  • inserting from erlang into cassandra
  • Bash “diff” utility showing files as different when using a regex Ignore
  • Hide custom navigation buttons if carousel has less items to show - Owl Carousel
  • How to use SpEL to inject result of method call in Spring?
  • Unable to connect to HBase stand alone server from windows remote client
  • accumulo, zookeeper hadoop Installation instructions, downloads and versions for CENTOS 6
  • no difference between [..] and […] for array?
  • SQL Server 2008R2 and creating XML document
  • Uber API: Specify multiple scopes while requesting or issuing a token returns Invalid Request Parame
  • Learn Ruby Hard Way ex. 48
  • When is $scope.$apply necessary for dealing with objects/arrays with Angular?
  • Comma within fields in CSV file -import to DB using SSIS
  • How to get a value (ex: baseURL) in every Karate feature?
  • Invalid access key error using credentials redeemed from an amazon open id token
  • Circular dependency while pushing http interceptor
  • Comma separated Values
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • Error creating VM instance in Google Compute Engine
  • Hits per day in Google Big Query
  • how does django model after text[] in postgresql [duplicate]
  • UserPrincipal.Current returns apppool on IIS