Table Management Commands in HBase

1. Commands in HBase

Here, in this HBase article, we will learn all the Table Management commands in HBase. These HBase Commands are Create table, List table, Disable table, Enable table, Describe & Alter table, Exist table and Drop table. Also, we will see how to use these several Shell Commands in HBase using Java API. Moreover, we will discuss syntax & example of Table Commands in HBase
So, let’s explore Commands in HBase.

HBase Commands

Table Management Commands in HBase

You must read about HBase Operations

2. Creating a Table Using HBase Shell

i. HBase Create Table

By using the create command in HBase, we can create a table. However, it is must to specify the table name and the Column Family name here.
A syntax for HBase Create Table commands in HBase

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

Example of HBase Create Table 
Here is an example of sample schema of a table named emp1. There are two column families in HBase of it:

  1. personal data
  2. professional data
Row keypersonal dataprofessional data

in HBase shell, we can create this table:

Have a look at General HBase Commands

hbase(main):002:0> create 'emp1', 'personal data', 'professional data'
  • Output

0 row(s) in 1.1300 seconds
=> Hbase::Table – emp1

  • Verification

By using the HBase list command, we can verify whether the table is created.
Below see the created emp1 table.
hbase(main):002:0> list
TABLE
emp1
2 row(s) in 0.0340 seconds

ii. Creating a Table Using Java API

Also, by using the createTable() method of HBaseAdmin class, we can create a table in HBase. Basically, it belongs to the org.apache.hadoop.hbase.client package. However,  to create a table in HBase using Java API, follow these steps.

Do you know about HBase Admin API
Step1: Instantiate HBaseAdmin
At first, instantiate the Configuration class and pass this instance to HBaseAdmin, because this class requires the Configuration object as a parameter:
Configuration conf = HBaseConfiguration.create();
HBaseAdmin admin = new HBaseAdmin(conf);
Step2: Create TableDescriptor
This class belongs to the org.apache.hadoop.hbase class. Basically, this 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
We can execute the created table in Admin mode, by using the createTable() method of HBaseAdmin class.
admin.createTable(table);
To create a table via admin, here is the complete program.

Have a look at HBase Client API

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("emp1"));
     // 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 ");
  }
}
  • To Compile and execute:
$javac CreateTable.java
$java CreateTable
  • Output:

Table created

If these professionals can make a switch to Big Data, so can you:
Rahul Doddamani Story - DataFlair
Rahul Doddamani
Java → Big Data Consultant, JDA
Follow on
Mritunjay Singh Success Story - DataFlair
Mritunjay Singh
PeopleSoft → Big Data Architect, Hexaware
Follow on
Rahul Doddamani Success Story - DataFlair
Rahul Doddamani
Big Data Consultant, JDA
Follow on
I got placed, scored 100% hike, and transformed my career with DataFlair
Enroll now
Deepika Khadri Success Story - DataFlair
Deepika Khadri
SQL → Big Data Engineer, IBM
Follow on
DataFlair Web Services
You could be next!
Enroll now

3. Listing a Table Using HBase Shell

i. HBase List Table

Basically, to list all the tables, we use the list commands in HBase.
A Syntax for List HBase :

Let’s revise HBase Use Cases and Real-time

hbase(main):001:0 > list

This command will display the list of all the tables in HBase while we type this command and execute in HBase prompt:
hbase(main):001:0> list
TABLE
emp1
Hence we can see a table named emp1.

ii. Listing Tables Using Java API

By using Java API, get the list of tables from HBase, for that follow these steps:
Step 1
To get the list of all the tables in HBase, in the class HBaseAdmin we have a method called listTables().
Basically, 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
Moreover, using the length variable of the HTableDescriptor class, we can get the length of the HTableDescriptor[] array. Also, using getNameAsString() method,  get the name of the tables from this object. So, using these run the ‘for’ loop to get the list of the tables in HBase.

Test your HBase Knowledge and check where you are in HBase
By using Java API, here is the program to list all the tables in HBase.

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());
     }
  }
}
  • To Compile and execute:

$javac ListTables.java
$java ListTables

  • Output:

User
emp1

4. Disabling a Table Using HBase Shell

Basically, using the disable commands in HBase we need to first disable the table, to delete a table or change its settings.

Do you know about HBase MapReduce Integration

Commands in HBase

Disabling a Table Using HBase Shell

i. HBase Disable Table

A Syntax for Disabling a Table

disable ‘emp1’

b. Example for Disabling a Table
Below example explains to disable a table:
hbase(main):025:0> disable ’emp1′
0 row(s) in 1.2760 seconds
c. Verification
However, we can still sense its existence through the list and exists commands, after disabling the table. Although, we cannot scan it. It will display error:
hbase(main):028:0> scan ’emp1′
ROW         COLUMN + CELL
ERROR: emp1 is disabled.

Have a look at HBase Troubleshooting

ii. is_disabled

To find whether a table is disabled, we can use this command in HBase.
A Syntax for is_disabled

hbase> is_disabled 'table name'

For Example
It verifies whether the table named emp1 is disabled. So, to check that it is disabled, make sure it will return true and it will return false,  if not.
hbase(main):031:0> is_disabled ’emp1′
true
0 row(s) in 0.0440 seconds

iii. disable_all

To disable all the tables matching the given regex, we can use this commands in HBase.
A Syntax for disable_all:

hbase> disable_all 'r.*'

You must know about HBase Compaction

iv. Disable a Table Using Java API

Here are some commands belong to the HBaseAdmin class, like isTableDisabled() method – to verify whether a table is disabled, disableTable() method- to disable a table.  To disable a table, follow these steps:
Step 1
Instantiate HBaseAdmin class:

// Creating configuration object
Configuration conf = HBaseConfiguration.create();
// Creating HBaseAdmin object
HBaseAdmin admin = new HBaseAdmin(conf);

Step 2
Then using isTableDisabled() method , verify whether the table is disabled:
Boolean b = admin.isTableDisabled(“emp1”);
Step 3
disable it, if the table is not disabled:

if(!b){
  admin.disableTable("emp1");
  System.out.println("Table disabled");
}

To verify whether the table is disabled; if not, how to disable it,  is the complete program.

Let’s discuss Data manipulation Command in HBase

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.client.HBaseAdmin;
public class DisableTable{
  public static void main(String args[]) throws MasterNotRunningException, IOException{
     // Instantiating configuration class
     Configuration conf = HBaseConfiguration.create();
     // Instantiating HBaseAdmin class
     HBaseAdmin admin = new HBaseAdmin(conf);
     // Verifying weather the table is disabled
     Boolean bool = admin.isTableDisabled("emp1");
     System.out.println(bool);
     // Disabling the table using HBaseAdmin object
     if(!bool){
        admin.disableTable("emp1");
        System.out.println("Table disabled");
     }
  }
}
  • To compile and execute:
$javac DisableTable.java
$java DsiableTable
  • Output:

false
Table disabled

5. Enabling a Table Using HBase Shell

Commands in HBase

Enabling a Table

Let’s revise HBase Operations

i. HBase Enable Table

A Syntax for HBase Enable Table:

enable ‘emp1’

Example
To enable a table, here is an example.

hbase(main):005:0> enable 'emp1'
0 row(s) in 0.4580 seconds

ii. is_enabled

In order to find whether a table is enabled, follow this HBase command.
A Syntax for is_enabled:

hbase> is_enabled 'table name'

Whether the table named emp1 is enabled, use this following code to verify. Basically,  it will return true, if it is enabled, and it will return false, if not.

hbase(main):031:0> is_enabled 'emp1'
true
0 row(s) in 0.0440 seconds

Do you know about HBase Security

iii. Enable a Table Using Java API

To verify whether a table is enabled, isTableEnabled() method is used; and to enable a table, enableTable() method is used. These methods belong to HBaseAdmin class. Follow the steps given below to enable a table.
Step1
Instantiate HBaseAdmin:

// Creating configuration object
Configuration conf = HBaseConfiguration.create();
// Creating HBaseAdmin object
HBaseAdmin admin = new HBaseAdmin(conf);

Step 2
Now, by using isTableEnabled() method, verify whether the table is enabled:
Boolean bool = admin.isTableEnabled(“emp1”);
Step 3
Disable it, if the table is not disabled:

if(!bool){
  admin.enableTable("emp1");
  System.out.println("Table enabled");
}

To verify whether the table is enabled and if it is not, then how to enable it, here is the program.

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.client.HBaseAdmin;
public class EnableTable{
  public static void main(String args[]) throws MasterNotRunningException, IOException{
     // Instantiating configuration class
     Configuration conf = HBaseConfiguration.create();
     // Instantiating HBaseAdmin class
     HBaseAdmin admin = new HBaseAdmin(conf);
     // Here, verifying whether the table is disabled
     Boolean bool = admin.isTableEnabled("emp1");
     System.out.println(bool);
     // Using HBaseAdmin object Enabling the table
     if(!bool){
        admin.enableTable("emp1");
        System.out.println("Table Enabled");
     }
  }
}
  • To Compile and execute:
$javac EnableTable.java
$java EnableTable
  • Output:

false
Table Enabled

6. HBase Describe a Table Using Shell 

To get the description of the table in return, follow this HBase command.
A Syntax for Describe Table:

Let’s revise HBase Features

hbase> describe 'table name'

7. HBase Alter Table

To make changes to an existing table, we use this Alter command. Basically, there are several operations like the set and delete table scope operators, change the maximum number of cells of a column family, and delete a column family from a table are possible with this command in HBase.

8. Existence of Table using HBase Shell

By using the exists HBase command, we can verify the existence of a table.

i. HBase Exist

Example of existence:
hbase(main):024:0> exists ’emp’
Table emp does exist
0 row(s) in 0.0750 seconds
==================================================================
hbase(main):015:0> exists ‘student’
Table student does not exist
0 row(s) in 0.0480 seconds

ii. Verifying the Existence of Table Using Java API

Also, using one of the HBaseAdmin class method the tableExists(), we can verify the existence of a table in HBase. So, to verify the existence of a table in HBase, follow the steps:
Step 1

Instantiate the HBaseAdimn class
// Instantiating configuration object
Configuration conf = HBaseConfiguration.create();
// Instantiating HBaseAdmin class
HBaseAdmin admin = new HBaseAdmin(conf);

Step 2
Then using the tableExists( ) method verify the existence of the table.
To test the existence of a table in HBase using Java API here is the java program.

Have a look at HBase Pros and Cons

import java.io.IOException;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.HBaseAdmin;
public class TableExists{
  public static void main(String args[])throws IOException{
     // Instantiating configuration class
     Configuration conf = HBaseConfiguration.create();
     // Instantiating HBaseAdmin class
     HBaseAdmin admin = new HBaseAdmin(conf);
     // Verifying the existance of the table
     boolean bool = admin.tableExists("emp");
     System.out.println( bool);
  }
}
  • To compile and execute:
$javac TableExists.java
$java TableExists
  • Output:

True

9. Dropping a Table using HBase Shell

We can delete a table, by using the drop commands in HBase. Make sure, we have to disable it, before dropping a table.

hbase(main):018:0> disable 'emp'
0 row(s) in 1.4580 seconds
hbase(main):019:0> drop 'emp'
0 row(s) in 0.3060 seconds

Now, using the exists HBase command, verify whether the table is deleted.

hbase(main):020:07gt; exists 'emp'
Table emp does not exist
0 row(s) in 0.0730 seconds

i. drop_all

Basically, in order to drop the tables which are matching the “regex” that is given in the command, we use this commands in HBase.
A Syntax for drop_all:

hbase> drop_all ‘t.*’
Hadoop Quiz

10. Deleting a Table Using Java API

Also, using one of the HBaseAdmin class method “the deleteTable() method”, we can delete a table. So,  to delete a table using Java API, follow these steps:
Step 1
Instantiate the HBaseAdmin class.

Let’s discuss Hbase Architecture

// creating a configuration object
Configuration conf = HBaseConfiguration.create();
// Creating HBaseAdmin object
HBaseAdmin admin = new HBaseAdmin(conf);

Step 2
By using the disableTable() method of the HBaseAdmin class, disable the table.
admin.disableTable(“emp1”);
Step 3
Then using the deleteTable() method of the HBaseAdmin class,  delete the table.
admin.deleteTable(“emp12”);
So, to delete a table in HBase here is the complete Java program.

import java.io.IOException;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.HBaseAdmin;
public class DeleteTable {
  public static void main(String[] args) throws IOException {
     // Instantiating configuration class
     Configuration conf = HBaseConfiguration.create();
     // Instantiating HBaseAdmin class
     HBaseAdmin admin = new HBaseAdmin(conf);
     // disabling table named emp
     admin.disableTable("emp12");
     // Deleting emp
     admin.deleteTable("emp12");
     System.out.println("Table deleted");
  }
}
  • To Compile and execute:
$javac DeleteTable.java
$java DeleteTable
  • Output:

Table deleted
So, this was all about Table Commands in HBase. Hope you like our explanation.

11. Conclusion: HBase Commands

Hence, in this HBase Commands tutorial, we have seen all the commands in HBase like Create, List, Disable, enable, describe & Alter, exist, drop table in detail. Moreover, we discussed syntax and example of HBase Commands. However, if any doubt occurs, regarding these Commands in HBase, feel free to ask.
See also –

HBase Interview Questions

For reference

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.