How to create data in an HBase table

Last updated on May 30 2022
Sonali Singh

Table of Contents

How to create data in an HBase table

HBase – Create Data

Inserting Data using HBase Shell

This blog demonstrates how to create data in an HBase table. To create data in an HBase table, the following commands and methods are used:

  • put command,
  • add() method of Put class, and
  • put() method of HTable class.

As an example, we are going to create the following table in HBase.

image001 25
create

Using put command, you can insert rows into a table. Its syntax is as follows:

put ’<table name>’,’row1’,’<colfamily:colname>’,’<value>’

Inserting the First Row

Let us insert the first row values into the emp table as shown below.

hbase(main):005:0> put ’emp’,’1′,’personal data:name’,’raju’

0 row(s) in 0.6600 seconds

hbase(main):006:0> put ’emp’,’1′,’personal data:city’,’hyderabad’

0 row(s) in 0.0410 seconds

hbase(main):007:0> put ’emp’,’1′,’professional

data:designation’,’manager’

0 row(s) in 0.0240 seconds

hbase(main):007:0> put ’emp’,’1′,’professional data:salary’,’50000′

0 row(s) in 0.0240 seconds

Insert the remaining rows using the put command in the same way. If you insert the whole table, you will get the following output.

hbase(main):022:0> scan ’emp’

 

ROW                        COLUMN+CELL

1 column=personal data:city, timestamp=1417524216501, value=hyderabad

 

1 column=personal data:name, timestamp=1417524185058, value=ramu

 

1 column=professional data:designation, timestamp=1417524232601,

 

value=manager

 

1 column=professional data:salary, timestamp=1417524244109, value=50000

 

2 column=personal data:city, timestamp=1417524574905, value=chennai

 

2 column=personal data:name, timestamp=1417524556125, value=ravi

 

2 column=professional data:designation, timestamp=1417524592204,

 

value=sr:engg

 

2 column=professional data:salary, timestamp=1417524604221, value=30000

 

3 column=personal data:city, timestamp=1417524681780, value=delhi

 

3 column=personal data:name, timestamp=1417524672067, value=rajesh

 

3 column=professional data:designation, timestamp=1417524693187,

 

value=jr:engg

3 column=professional data:salary, timestamp=1417524702514,

 

value=25000

Inserting Data Using Java API

You can insert data into Hbase using the add() method of the Put class. You can save it using the put() method of the HTable class. These classes belong to the org.apache.hadoop.hbase.client package. Below given are the steps to create data in a Table of HBase.

Step 1:Instantiate the Configuration Class

The 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 configuration object and table name as parameters. You can instantiate HTable class as shown below.

HTable hTable = new HTable(conf, tableName);

Step 3: Instantiate the PutClass

To insert data into an HBase table, the add() method and its variants are used. This method belongs to Put, therefore instantiate the put class. This class requires the row name you want to insert the data into, in string format. You can instantiate the Put class as shown below.

Put p = new Put(Bytes.toBytes(“row1”));

Step 4: InsertData

The add() method of Put class is used to insert data. It requires 3 byte arrays representing column family, column qualifier (column name), and the value to be inserted, respectively. Insert data into the HBase table using the add() method as shown below.

p.add(Bytes.toBytes(“coloumn family “), Bytes.toBytes(“column

name”),Bytes.toBytes(“value”));

Step 5: Save the Data in Table

After inserting the required rows, save the changes by adding the put instance to the put() method of HTable class as shown below.

hTable.put(p);

Step 6: Close the HTable Instance

After creating data in the HBase Table, close the HTable instance using the close() method as shown below.

hTable.close();

Given below is the complete program to create data in HBase Table.

import java.io.IOException;

 

import org.apache.hadoop.conf.Configuration;

 

import org.apache.hadoop.hbase.HBaseConfiguration;

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

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

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

 

public class InsertData{

 

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

 

// Instantiating Configuration class

Configuration config = HBaseConfiguration.create();

 

// Instantiating HTable class

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

 

// Instantiating Put class

// accepts a row name.

Put p = new Put(Bytes.toBytes(“row1”));

 

// adding values using add() method

// accepts column family name, qualifier/row name ,value

p.add(Bytes.toBytes(“personal”),

Bytes.toBytes(“name”),Bytes.toBytes(“raju”));

 

p.add(Bytes.toBytes(“personal”),

Bytes.toBytes(“city”),Bytes.toBytes(“hyderabad”));

 

p.add(Bytes.toBytes(“professional”),Bytes.toBytes(“designation”),

Bytes.toBytes(“manager”));

 

p.add(Bytes.toBytes(“professional”),Bytes.toBytes(“salary”),

Bytes.toBytes(“50000”));

 

// Saving the put Instance to the HTable.

hTable.put(p);

System.out.println(“data inserted”);

 

// closing HTable

hTable.close();

}

}

Compile and execute the above program as shown below.

$javac InsertData.java

$java InsertData

The following should be the output:

data inserted

So, this brings us to the end of blog. This Tecklearn ‘How to create data in an HBase table’ 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 create data in an HBase table"

Leave a Message

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