HBase - Drop a Table


Advertisements


Dropping a Table using HBase Shell

Using the drop command, you can delete a table. Before dropping a table, you have to disable it.

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

Verify whether the table is deleted using the exists command.

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

drop_all

This command is used to drop the tables matching the “regex” given in the command. Its syntax is as follows:

hbase> drop_all ‘t.*’ 

Note: Before dropping a table, you must disable it.

Example

Assume there are tables named raja, rajani, rajendra, rajesh, and raju.

hbase(main):017:0> list
TABLE
raja
rajani
rajendra 
rajesh
raju
9 row(s) in 0.0270 seconds

All these tables start with the letters raj. First of all, let us disable all these tables using the disable_all command as shown below.

hbase(main):002:0> disable_all 'raj.*'
raja
rajani
rajendra
rajesh
raju
Disable the above 5 tables (y/n)?
y
5 tables successfully disabled

Now you can delete all of them using the drop_all command as given below.

hbase(main):018:0> drop_all 'raj.*'
raja
rajani
rajendra
rajesh
raju
Drop the above 5 tables (y/n)?
y
5 tables successfully dropped

Deleting a Table Using Java API

You can delete a table using the deleteTable() method in the HBaseAdmin class. Follow the steps given below to delete a table using java API.

Step 1

Instantiate the HBaseAdmin class.

// creating a configuration object
Configuration conf = HBaseConfiguration.create();

// Creating HBaseAdmin object
HBaseAdmin admin = new HBaseAdmin(conf); 

Step 2

Disable the table using the disableTable() method of the HBaseAdmin class.

admin.disableTable("emp1");

Step 3

Now delete the table using the deleteTable() method of the HBaseAdmin class.

admin.deleteTable("emp12");

Given below is the complete java program to delete a table in HBase.

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");
   }
}

Compile and execute the above program as shown below.

$javac DeleteTable.java
$java DeleteTable

The following should be the output:

Table deleted


Advertisements
E-Books Store