Basic HBase Java Classes and Methods – Part 6: Scan a Table

In HBase a Scan is similar to a Select in SQL.  Again we return to skeleton code which is very similar to what we have seen before.  I will put comments into the three areas we will be addressing:

So there are three areas we will cover that are new:

  • Define a ResultScanner
  • Code to Scan the table
  • Close scan resources

Define a ResultScanner

First just before our try clause, we define a ResultScanner.  We do this outside of the try clause for the same reason we do the table outside the try clause, because we need to check and close the resources in the finally clause and if we were to define this inside the try clause it would then be out of scope.

 Scan the table

We get a Table object from our Connection object, just as we have done before.  This time we run a getScanner method on the Table object using a Scan object that we instantiate.  The results come back as a list which we iterate though.  Each iteration we use the CellUtil utility class which allows us to access each part of the ResultScanner separately.  You can read more about CellUtil in the JavaDoc.

CellUtil.cloneRow() is used to access the key
CellUtil.cloneFamily() is used to access the column family name
CellUtil.cloneQualifier() is used to access the column name
CellUtil.cloneValue() is used to access the column value

 Close scan resources

Similar to how we close the connection and the table, we also need to close our ResultScanner.

The final code is as follows:

This produces the following output:

In our next part,  we will look at how to delete data from an HBase table.  Next Basic HBase Java Classes and Methods – Part 7: Delete from a Table

This entry was posted in Data Analytics and tagged , . Bookmark the permalink.

Leave a Reply