Getting a record from HBase

Getting a record from HBase is similar to getting a record from SQL. You still need basic details like database name, table name and a row key. Where it differs is the result needs to be translated from a byte array to a String value. I try to set out here a basic example, setting out what’s required and how to go about it.

There are a few things we need to know first:

  • The Record Key: The record key is set upon data entry. This is something that you generate yourself and should be unique. Much like SQL, if the record key is a duplicate, the data stored there will be overwritten. So each time you write a new unique record, you need to have a unique record key.
  • Table name: As in SQL, we need to know the table name.
  • Family Name and Column Name: Data in HBase is categorised according to Family Name and Column Name. Families have any number of Columns and columns are somewhat similar to a cell in SQL. It is not necessary that every record in HBase has the same number of column cells. For example, you might have “personalData” as the Family Name. For column data you might have “firstName”, and “secondName”. At some stage, an entry (i.e. a row) might require a “formerName”. There is no requirement for all entries (rows) to have “formerName”. This is where HBase allows flexibility in the format of the data you are storing. (It is also not essential to know all the column names, they can be retrieved using the family map. However, with good database design you should know what each column is for).

Consider a HBase table with the following format:

PersonalDataDetails
firstNamesecondnameagedateOfBirthAddress

It has two Families: PersonalData and Details.

PersonalData holds columns firstName and secondName. The Details family holds columns age, dateOfBirth and Address.

Using a rowkey, we can access the Details family map, without accessing the personalData family. And vice-versa. We can also access all Family maps.

Below is sample code to retrieve a record from HBase. It requires you to enter the recordKey, tableName and family names. The Column Names are automatically retrieved using “entries.getKey();” in the loop over the family maps.

public class Main {
    public static void main(String[] args) {

try{



            //rowKey - insert your row key here
            String rowKey = "aRowKey";
            //table name - insert your table name here
            String tName = "aTableName";
            //Family Names - insert your family names here
            String familyNameOne = "aFamilyNameOne";
            String familyNameTwo = "aFamilyNameTwo";

            Configuration conf = HBaseConfiguration.create(new Configuration());
            Connection connection = ConnectionFactory.createConnection(conf);

            TableName tableName = TableName.valueOf(tName);
            Table table = connection.getTable(tableName);
            Get g = new Get(toBytes(rowKey));
            Result result = table.get(g);

            NavigableMap<byte[], byte[]> byteMapDataOne = result.getFamilyMap(Bytes.toBytes(familyNameOne));
            NavigableMap<byte[], byte[]> byteMapDataTwo = result.getFamilyMap(Bytes.toBytes(familyNameTwo));

            //FamilyNameOne data:
            for (Map.Entry<byte[], byte[]> entries : byteMapDataOne.entrySet()) {
                byte[] temp = entries.getKey();
                byte[] value = entries.getValue();
                String thisColumn = Bytes.toString(temp);
                String thisValue = Bytes.toString(value);

                System.out.println("FamilyName: "+familyNameOne+"   ::   Column Name: "+thisColumn+"   and value is: "+thisValue);

            }//end for

            //familynameTwo data:
            for (Map.Entry<byte[], byte[]> entries : byteMapDataTwo.entrySet()) {
                byte[] temp = entries.getKey();
                byte[] value = entries.getValue();
                String thisColumn = Bytes.toString(temp);
                String thisValue = Bytes.toString(value);

                System.out.println("FamilyName: "+familyNameTwo+"   ::  Column Name: "+thisColumn+"   and value is: "+thisValue);

            }//end for


            //tidy up
            connection.close();

        }
        catch (Exception E){
            System.out.println(E);
            E.printStackTrace();
        }

}//end main
}//end class main

Related Posts

Java HashMap – Get highest Key or highest value

Java HashMaps are a key : value data object. They are widely used in Java and are based on the Map interface. Their flexibility lies in their ability to span…

The problem with frameworks …

A framework is a program or tool that provides ready made solutions and components, saving on the need to write boiler plate code. For example, a framework can come with…

You Missed

Global IT Outage : All eyes on CrowdStrike

  • By aCoder
  • July 19, 2024
  • 1301 views
Global IT Outage : All eyes on CrowdStrike

Java HashMap – Get highest Key or highest value

  • By aCoder
  • July 17, 2024
  • 1691 views
Java HashMap – Get highest Key or highest value

The problem with frameworks …

  • By aCoder
  • July 12, 2024
  • 1400 views
The problem with frameworks …

A.I. stocks are in bubble territory

  • By aCoder
  • July 10, 2024
  • 1252 views
A.I. stocks are in bubble territory

The AI Boyfriend

  • By aCoder
  • July 9, 2024
  • 1308 views
The AI Boyfriend

WordPress – only index page showing

  • By aCoder
  • July 2, 2024
  • 1244 views
WordPress – only index page showing