How to create and List Table in HBase shell

Last updated on May 30 2022
Sonali Singh

Table of Contents

How to create and List Table in HBase shell

HBase – Create Table

Creating a Table using HBase Shell

You can create a table using the create command, here you must specify the table name and the Column Family name. The syntax to create a table in HBase shell is shown below.

create ‘<table name>’,’<column family>’

Example

Given below is a sample schema of a table named emp. It has two column families: “personal data” and “professional data”.

Row key personal data professional data

You can create this table in HBase shell as shown below.

hbase(main):002:0> create ’emp’, ‘personal data’, ‘professional data’

And it will give you the following output.

0 row(s) in 1.1300 seconds

=> Hbase::Table – emp

Verification

You can verify whether the table is created using the list command as shown below. Here you can observe the created emp table.

hbase(main):002:0> list

TABLE

emp

2 row(s) in 0.0340 seconds

Creating a Table Using java API

You can create a table in HBase using the createTable() method of HBaseAdmin class. This class belongs to the org.apache.hadoop.hbase.client package. Given below are the steps to create a table in HBase using java API.

Step1: Instantiate HBaseAdmin

This class requires the Configuration object as a parameter, therefore initially instantiate the Configuration class and pass this instance to HBaseAdmin.

Configuration conf = HBaseConfiguration.create();

HBaseAdmin admin = new HBaseAdmin(conf);

Step2: Create TableDescriptor

HTableDescriptor is a class that belongs to the org.apache.hadoop.hbase class. This class is like a container of table names and column families.

//creating table descriptor

HTableDescriptor table = new HTableDescriptor(toBytes(“Table name”));

 

//creating column family descriptor

HColumnDescriptor family = new HColumnDescriptor(toBytes(“column family”));

 

//adding coloumn family to HTable

table.addFamily(family);

Step 3: Execute through Admin

Using the createTable() method of HBaseAdmin class, you can execute the created table in Admin mode.

admin.createTable(table);

Given below is the complete program to create a table via admin.

import java.io.IOException;

 

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;

import org.apache.hadoop.hbase.TableName;

 

import org.apache.hadoop.conf.Configuration;

 

public class CreateTable {

 

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

 

// Instantiating configuration class

Configuration con = HBaseConfiguration.create();

 

// Instantiating HbaseAdmin class

HBaseAdmin admin = new HBaseAdmin(con);

 

// Instantiating table descriptor class

HTableDescriptor tableDescriptor = new

HTableDescriptor(TableName.valueOf(“emp”));

 

// Adding column families to table descriptor

tableDescriptor.addFamily(new HColumnDescriptor(“personal”));

tableDescriptor.addFamily(new HColumnDescriptor(“professional”));

 

// Execute the table through admin

admin.createTable(tableDescriptor);

System.out.println(” Table created “);

}

}

Compile and execute the above program as shown below.

$javac CreateTable.java

$java CreateTable

The following should be the output:

Table created

 

HBase – Listing Table

Listing a Table using HBase Shell

list is the command that is used to list all the tables in HBase. Given below is the syntax of the list command.

hbase(main):001:0 > list

When you type this command and execute in HBase prompt, it will display the list of all the tables in HBase as shown below.

hbase(main):001:0> list

TABLE

emp

Here you can observe a table named emp.

Listing Tables Using Java API

Follow the steps given below to get the list of tables from HBase using java API.

Step 1

You have a method called listTables() in the class HBaseAdmin to get the list of all the tables in HBase. This method returns an array of HTableDescriptor objects.

//creating a configuration object

Configuration conf = HBaseConfiguration.create();

 

//Creating HBaseAdmin object

HBaseAdmin admin = new HBaseAdmin(conf);

 

//Getting all the list of tables using HBaseAdmin object

HTableDescriptor[] tableDescriptor = admin.listTables();

Step 2

You can get the length of the HTableDescriptor[] array using the length variable of the HTableDescriptor class. Get the name of the tables from this object using getNameAsString() method. Run the ‘for’ loop using these and get the list of the tables in HBase.

Given below is the program to list all the tables in HBase using Java API.

import java.io.IOException;

 

import org.apache.hadoop.conf.Configuration;

 

import org.apache.hadoop.hbase.HBaseConfiguration;

import org.apache.hadoop.hbase.HTableDescriptor;

import org.apache.hadoop.hbase.MasterNotRunningException;

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

 

public class ListTables {

 

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

 

// Instantiating a configuration class

Configuration conf = HBaseConfiguration.create();

 

// Instantiating HBaseAdmin class

HBaseAdmin admin = new HBaseAdmin(conf);

 

// Getting all the list of tables using HBaseAdmin object

HTableDescriptor[] tableDescriptor = admin.listTables();

 

// printing all the table names.

for (int i=0; i<tableDescriptor.length;i++ ){

System.out.println(tableDescriptor[i].getNameAsString());

}

 

}

}

Compile and execute the above program as shown below.

$javac ListTables.java

$java ListTables

The following should be the output:

User

emp

 

So, this brings us to the end of blog. This Tecklearn ‘How to create a Table in HBase shell’ 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 and List Table in HBase shell"

Leave a Message

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