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:
| PersonalData | Details | ||||
| firstName | secondname | age | dateOfBirth | Address | |
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






