How to read data from Table in HBase

Last updated on May 30 2022
Sonali Singh

Table of Contents

How to read data from Table in HBase

HBase – Read Data

Reading Data using HBase Shell

The get command and therefore the get() method of HTable class are used to read data from a table in HBase. Using get command, you’ll get a single row of data at a time. Its syntax is as follows:

get ’<table name>’,’row1’

Example

The following example shows how to use the get command. Allow us scan the primary row of the emp table.

hbase(main):012:0> get ’emp’, ‘1’

 

COLUMN                     CELL

 

personal : city timestamp = 1417521848375, value = hyderabad

 

personal : name timestamp = 1417521785385, value = ramu

 

professional: designation timestamp = 1417521885277, value = manager

 

professional: salary timestamp = 1417521903862, value = 50000

 

4 row(s) in 0.0270 seconds

Reading a Specific Column

Given below is the syntax to read a specific column using the get method.

hbase> get ‘table name’, ‘rowid’, {COLUMN ⇒ ‘column family:column name ’}

Example

Given below is the example to read a specific column in HBase table.

hbase(main):015:0> get ’emp’, ‘row1’, {COLUMN ⇒ ‘personal:name’}

COLUMN                CELL

personal:name timestamp = 1418035791555, value = raju

1 row(s) in 0.0080 seconds

Reading Data Using Java API

To read data from an HBase table, use the get() method of the HTable class. This method requires an instance of the Get class. Follow the steps given below to retrieve data from the HBase table.

Step 1: Instantiate the Configuration Class

Configuration class adds HBase configuration files to its object. You can create a configuration object using the create() method of the HbaseConfiguration class as shown below.

Configuration conf = HbaseConfiguration.create();

Step 2: Instantiate the HTable Class

You have a class called HTable, an implementation of Table in HBase. This class is used to communicate with a single HBase table. While instantiating this class, it accepts the configuration object and the table name as parameters. You can instantiate the HTable class as shown below.

HTable hTable = new HTable(conf, tableName);

Step 3: Instantiate the Get Class

You can retrieve data from the HBase table using the get() method of the HTable class. This method extracts a cell from a given row. It requires a Get class object as parameter. Create it as shown below.

Get get = new Get(toBytes(“row1”));

Step 4: Read the Data

While retrieving data, you can get a single row by id, or get a set of rows by a set of row ids, or scan an entire table or a subset of rows.

You can retrieve an HBase table data using the add method variants in Get class.

To get a specific column from a specific column family, use the following method.

get.addFamily(personal)

To get all the columns from a specific column family, use the following method.

get.addColumn(personal, name)

Step 5: Get the Result

Get the result by passing your Get class instance to the get method of the HTable class. This method returns the Result class object, which holds the requested result. Given below is the usage of get() method.

Result result = table.get(g);

Step 6: Reading Values from the Result Instance

The Result class provides the getValue() method to read the values from its instance. Use it as shown below to read the values from the Result instance.

byte [] value = result.getValue(Bytes.toBytes(“personal”),Bytes.toBytes(“name”));

byte [] value1 = result.getValue(Bytes.toBytes(“personal”),Bytes.toBytes(“city”));

Given below is the complete program to read values from an HBase table.

import java.io.IOException;

 

import org.apache.hadoop.conf.Configuration;

 

import org.apache.hadoop.hbase.HBaseConfiguration;

import org.apache.hadoop.hbase.client.Get;

import org.apache.hadoop.hbase.client.HTable;

import org.apache.hadoop.hbase.client.Result;

import org.apache.hadoop.hbase.util.Bytes;

 

public class RetriveData{

 

public static void main(String[] args) throws IOException, Exception{

 

// Instantiating Configuration class

Configuration config = HBaseConfiguration.create();

 

// Instantiating HTable class

HTable table = new HTable(config, “emp”);

 

// Instantiating Get class

Get g = new Get(Bytes.toBytes(“row1”));

 

// Reading the data

Result result = table.get(g);

 

// Reading values from Result class object

byte [] value = result.getValue(Bytes.toBytes(“personal”),Bytes.toBytes(“name”));

 

byte [] value1 = result.getValue(Bytes.toBytes(“personal”),Bytes.toBytes(“city”));

 

// Printing the values

String name = Bytes.toString(value);

String city = Bytes.toString(value1);

 

System.out.println(“name: ” + name + ” city: ” + city);

}

}

Compile and execute the above program as shown below.

$javac RetriveData.java

$java RetriveData

The following should be the output:

name: Raju city: Delhi

 

So, this brings us to the end of blog. This Tecklearn ‘How to read data from Table in HBase’ helps you with commonly asked questions if you are looking out for a job in HBase and No-SQL Database Domain.

If you wish to learn HBase and build a career in HBase or No-SQL Database domain, then check out our interactive, Apache HBase Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

https://www.tecklearn.com/course/apache-hbase-training/

Apache HBase Training

About the Course

Tecklearn Apache HBase training will master the powerful NoSQL distributed database. You will learn HBase architecture, data analytics using HBase, integration with Hive, monitoring cluster using ZooKeeper and working on real-life industry projects. Build your career as a certified HBase professional through our hands-on training with real-world examples. Upon completion of this online training, you will hold a solid understanding and hands-on experience with Apache HBase.

Why Should you take Apache HBase Training?

  • HBase is now the largest data-driven service serving top websites including Facebook Messaging Platform.
  • There is Strong demand for HBase qualified professionals and they are paid big bucks for the right skills.
  • According to indeed.com, the average pay of an HBase developer stands at $81,422 per annum.

What you will Learn in this Course?

Introduction to HBase and NoSQL

  • Introduction to HBase
  • Fundamentals of HBase
  • What is NoSQL
  • NoSQL Vs RDBMS
  • Why HBase
  • Where to use HBase

HBase Data Modelling

  • Data Modelling
  • HDFS vs. HBase
  • HBase Use Cases

HBase Architecture and Components

  • HBase Architecture
  • Components of HBase Cluster

HBase Installation

  • Prerequisites for HBase Installation
  • Installation Steps

Programming in HBase

  • Create an Eclipse Project for HBase
  • Simple Table Creation from Java in HBase
  • HBase API
  • HBase Shell
  • Primary operations and advanced operations

Integration of Hive with HBase

  • Create a table and insert data into it
  • Integration of Hive with HBase
  • HBase Mapping

Deep Dive into HBase

  • Input Data into HBase
  • File Loading
  • HDFS File
  • HBase handling files in File System
  • WAL
  • Seek Vs Transfer
  • HBase ACID Properties

Got a question for us? Please mention it in the comments section and we will get back to you.

 

0 responses on "How to read data from Table in HBase"

Leave a Message

Your email address will not be published. Required fields are marked *