HBase Read Data - HBase

How to read data in HBase?

Reading Data using HBase Shell

get command and get() method of HTable class are used to read data from a table in HBase. Using get command, you can get a single row of data at a time. Below is the syntax:

Example

Below example shows how to use the get command. Let’s scan the first row of the emp table.

Reading a Specific Column

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

Example

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

Reading Data Using Java API

get() method of the HTable class can be used to read data from an HBase table. 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. Configuration object can be created using create() method of HbaseConfiguration class as shown below.

Step 2: Instantiate the HTable Class

There is a class called HTable, an implementation of Table in HBase. This class is used for comunicating with a single HBase table. While instantiating this class, it accepts the configuration object and the table name as parameters. HTable class can be instantiates as shown below.

Step 3: Instantiate the Get Class

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

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.

HBase table data can be retrieved using add method variants in Get class.

To get a specific column from a specific column family, use below method.

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

Step 5: Get the Result

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

Step 6: Reading Values from the Result Instance

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

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

Compile and execute the above program as shown below.

Output:

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

HBase Topics