HBase Table using JAVA API

In HBase, we can create table operations in two ways

  • Shell command
  • JAVA API

We will learn to use both to create Tables.

  • HBase Create table with Java API
  • HBase Create table with Shell

HBase create a table with Java API

In this section, we are going to perform some of the operations using Java coding through Java API.

Through Java API, we can create tables in HBase and also load data into tables using Java coding.

  • Establishing a connection with HBase through Java API
  • Using Eclipse for Java coding, debugging and testing

Establishing connection through Java API:

The Following steps guide us to develop Java code to connect HBase through Java API.

Step 1) In this step, we are going to create a Java project in eclipse for HBase connection.

Creation of new project name “HbaseConnection” in eclipse.

For Java related project set up or creation of program

Create, Insert, Read Tables in HBase

If we observe the screenshot above.

  1. Give project name in this box. In our case, we have the project name “HbaseConnection”
  2. Check this box for default location to be saved. In this /home/hduser/work/HbaseConnection is the path
  3. Check the box for Java environment here. In this JavaSE-1.7 is the Java edition
  4. Choose your option where you want to save file. In our case, we have selected option second “Create a separate folder for sources and class files”
  5. Click on the finish button.
  • When you click on Finish button, it’s going to create “HbaseConnection” project in eclipse
  • It will directly come to eclipse home page after clicking the finish button.

Step 2) On eclipse home page follow the following steps

Right click on project -> Select Build Path -> Configure build path

Create, Insert, Read Tables in HBase

From above screenshot

  1. Right click on a project
  2. Select build path
  3. Select configure build path

After clicking Configure Build path, it will open another window as shown in below screenshot

In this step, we will add relevant HBase jars into java project as shown in the screenshot.

  • Important jars to be added hbase-0.94.8.jar, hadoop-core-1.1.2.jar
  • Click on the finish button

Create, Insert, Read Tables in HBase

  1. Come to libraries
  2. Press option – Add External Jars
  3. Select required important jars
  4. Press finish button to add these files to ‘src’ of java project under libraries

After adding these jars, it will show under project “src” location. All the Jar files that fall under the project are now ready for usage with Hadoop ecosystem.

Step 3) In this step by using HBaseConnection.java, the HBase Connection would be established through Java Coding

  • On Eclipse top menu, execute a java program as shown belowRun -> Run As -> Java Application

    Create, Insert, Read Tables in HBase

  1. Select Run
  2. Select Run as Java Application
  • This code will establish a connection with HBase through Java API
  • After Running this code ‘guru99’ table will be created in HBase with two column families named “education” and “projects”. At present, the empty schema is only created in HBase.

Create, Insert, Read Tables in HBase

From the screenshot above we are performing following functions.

  1. Using HTableDescriptor we can able to create “guru99” table in HBase
  2. Using addFamily method, we are going to add “education” and “projects” as column names to “guru99” table.

The below coding is going to

  • Establish a connection with HBase and
  • Create “guru99” table with two columns

Code Placed under HBaseConnection_Java document

// Place this code inside Hbase connection
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;						
import org.apache.hadoop.hbase.HBaseConfiguration;							
import org.apache.hadoop.hbase.HColumnDescriptor;							
import org.apache.hadoop.hbase.HTableDescriptor;		
Import org.apache.hadoop.hbase.client.HBaseAdmin;							

public class HBaseConnection							
{							
    public static void main(String[] args) throws IOException						
    {							
	HBaseConfigurationhc = new HBaseConfiguration(new Configuration());										
	HTableDescriptorht = new HTableDescriptor("guru99"); 										

	ht.addFamily( new HColumnDescriptor("education"));					
	ht.addFamily( new HColumnDescriptor("projects"));										
	System.out.println( "connecting" );										
	HBaseAdminhba = new HBaseAdmin( hc );								

	System.out.println( "Creating Table" );								
	hba.createTable( ht );							
	System.out.println("Done......");										
    }						
}

This is required code you have to place in HBaseConnection.java and have to run a java program

After running this program, it is going to establish a connection with HBase and in turn, it will create a table with column names.

  • The table name is “guru99”
  • Column names are “education” and “projects”

Step 4) We can check whether “guru99” table is created with two columns in HBase or not by using HBase shell mode with “list” command.

The “list” command gives information about all the tables that is created in HBase.

In this screen, we are going to do

  • Code checking in HBase shell by executing “list” command.
  • If we run “list” command, it will display the table created in HBase as below. In our case, we can see table “guru99” is created

Create, Insert, Read Tables in HBase

HBase Create table with Shell

Syntax to Create a table is

Syntax: create <tablename>, <columnfamilyname>

HBase Shell and General Commands

Example:-

hbase(main):001:0> create 'education' ,'guru99'
0 rows(s) in 0.312 seconds
=>Hbase::Table – education

The above example explains how to create a table in HBase with the specified name given according to the dictionary or specifications as per column family. In addition to this, we can also pass some table-scope attributes as well into it.

create 'guru99', {NAME=>'Edu', VERSIONS=>213423443}

Summary:

HBase is a column-oriented NoSQL database for storing a large amount of data on top of Hadoop ecosystem. Handling tables in HBase is a very crucial thing because all important functionalities such as Data operations, Data enhancements and Data modeling we can be performed through only tables in HBase. Tables perform the following functions 1) Creation of tables with column names and rows 2) Inserting values into tables 3) Retrieving values from tables

Categories:

Leave a comment