Tuesday, 24 July 2018

Table Management Commands in HBase

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.
Commands in HBase
Table Management Commands in HBase

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
  1. 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:
  1. 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.
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.
  1. //creating table descriptor
  2. HTableDescriptor table = new HTableDescriptor(toBytes("Table name"));
  3. //creating column family descriptor
  4. HColumnDescriptor family = new HColumnDescriptor(toBytes("column family"));
  5. //adding coloumn family to HTable
  6. 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.
  1. import java.io.IOException;
  2. import org.apache.hadoop.hbase.HBaseConfiguration;
  3. import org.apache.hadoop.hbase.HColumnDescriptor;
  4. import org.apache.hadoop.hbase.HTableDescriptor;
  5. import org.apache.hadoop.hbase.client.HBaseAdmin;
  6. import org.apache.hadoop.hbase.TableName;
  7. import org.apache.hadoop.conf.Configuration;
  8. public class CreateTable {
  9. public static void main(String[] args) throws IOException {
  10. // Instantiating configuration class
  11. Configuration con = HBaseConfiguration.create();
  12. // Instantiating HbaseAdmin class
  13. HBaseAdmin admin = new HBaseAdmin(con);
  14. // Instantiating table descriptor class
  15. HTableDescriptor tableDescriptor = new
  16. HTableDescriptor(TableName.valueOf("emp1"));
  17. // Adding column families to table descriptor
  18. tableDescriptor.addFamily(new HColumnDescriptor("personal"));
  19. tableDescriptor.addFamily(new HColumnDescriptor("professional"));
  20. // Execute the table through admin
  21. admin.createTable(tableDescriptor);
  22. System.out.println(" Table created ");
  23. }
  24. }
  • To Compile and execute:
  1. $javac CreateTable.java
  2. $java CreateTable
  • Output:
Table created

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 :
  1. 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.
  1. //creating a configuration object
  2. Configuration conf = HBaseConfiguration.create();
  3. //Creating HBaseAdmin object
  4. HBaseAdmin admin = new HBaseAdmin(conf);
  5. //Getting all the list of tables using HBaseAdmin object
  6. 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.
By using Java API, here is the program to list all the tables in HBase.
  1. import java.io.IOException;
  2. import org.apache.hadoop.conf.Configuration;
  3. import org.apache.hadoop.hbase.HBaseConfiguration;
  4. import org.apache.hadoop.hbase.HTableDescriptor;
  5. import org.apache.hadoop.hbase.MasterNotRunningException;
  6. import org.apache.hadoop.hbase.client.HBaseAdmin;
  7. public class ListTables {
  8. public static void main(String args[])throws MasterNotRunningException, IOException{
  9. // Instantiating a configuration class
  10. Configuration conf = HBaseConfiguration.create();
  11. // Instantiating HBaseAdmin class
  12. HBaseAdmin admin = new HBaseAdmin(conf);
  13. // Getting all the list of tables using HBaseAdmin object
  14. HTableDescriptor[] tableDescriptor = admin.listTables();
  15. // printing all the table names.
  16. for (int i=0; i<tableDescriptor.length;i++ ){
  17. System.out.println(tableDescriptor[i].getNameAsString());
  18. }
  19. }
  20. }
  • 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.
Commands in HBase
Disabling a Table Using HBase Shell

i. HBase Disable Table

A Syntax for Disabling a Table
  1. 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.

ii. is_disabled

To find whether a table is disabled, we can use this command in HBase.
A Syntax for is_disabled
  1. 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:
  1. hbase> disable_all 'r.*'

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:
  1. // Creating configuration object
  2. Configuration conf = HBaseConfiguration.create();
  3. // Creating HBaseAdmin object
  4. 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:
  1. if(!b){
  2. admin.disableTable("emp1");
  3. System.out.println("Table disabled");
  4. }
To verify whether the table is disabled; if not, how to disable it,  is the complete program.
  1. import java.io.IOException;
  2. import org.apache.hadoop.conf.Configuration;
  3. import org.apache.hadoop.hbase.HBaseConfiguration;
  4. import org.apache.hadoop.hbase.MasterNotRunningException;
  5. import org.apache.hadoop.hbase.client.HBaseAdmin;
  6. public class DisableTable{
  7. public static void main(String args[]) throws MasterNotRunningException, IOException{
  8. // Instantiating configuration class
  9. Configuration conf = HBaseConfiguration.create();
  10. // Instantiating HBaseAdmin class
  11. HBaseAdmin admin = new HBaseAdmin(conf);
  12. // Verifying weather the table is disabled
  13. Boolean bool = admin.isTableDisabled("emp1");
  14. System.out.println(bool);
  15. // Disabling the table using HBaseAdmin object
  16. if(!bool){
  17. admin.disableTable("emp1");
  18. System.out.println("Table disabled");
  19. }
  20. }
  21. }
  • To compile and execute:
  1. $javac DisableTable.java
  2. $java DsiableTable
  • Output:
false
Table disabled

5. Enabling a Table Using HBase Shell

Commands in HBase
Enabling a Table

i. HBase Enable Table

A Syntax for HBase Enable Table:
  1. enable ‘emp1’

Example
To enable a table, here is an example.
  1. hbase(main):005:0> enable 'emp1'
  2. 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:
  1. 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.
  1. hbase(main):031:0> is_enabled 'emp1'
  2. true
  3. 0 row(s) in 0.0440 seconds

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:
  1. // Creating configuration object
  2. Configuration conf = HBaseConfiguration.create();
  3. // Creating HBaseAdmin object
  4. 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:
  1. if(!bool){
  2. admin.enableTable("emp1");
  3. System.out.println("Table enabled");
  4. }
To verify whether the table is enabled and if it is not, then how to enable it, here is the program.
  1. import java.io.IOException;
  2. import org.apache.hadoop.conf.Configuration;
  3. import org.apache.hadoop.hbase.HBaseConfiguration;
  4. import org.apache.hadoop.hbase.MasterNotRunningException;
  5. import org.apache.hadoop.hbase.client.HBaseAdmin;
  6. public class EnableTable{
  7. public static void main(String args[]) throws MasterNotRunningException, IOException{
  8. // Instantiating configuration class
  9. Configuration conf = HBaseConfiguration.create();
  10. // Instantiating HBaseAdmin class
  11. HBaseAdmin admin = new HBaseAdmin(conf);
  12. // Here, verifying whether the table is disabled
  13. Boolean bool = admin.isTableEnabled("emp1");
  14. System.out.println(bool);
  15. // Using HBaseAdmin object Enabling the table
  16. if(!bool){
  17. admin.enableTable("emp1");
  18. System.out.println("Table Enabled");
  19. }
  20. }
  21. }
  • To Compile and execute:
  1. $javac EnableTable.java
  2. $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:
  1. 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
  1. Instantiate the HBaseAdimn class
  2. // Instantiating configuration object
  3. Configuration conf = HBaseConfiguration.create();
  4. // Instantiating HBaseAdmin class
  5. 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.
  1. import java.io.IOException;
  2. import org.apache.hadoop.hbase.HBaseConfiguration;
  3. import org.apache.hadoop.conf.Configuration;
  4. import org.apache.hadoop.hbase.client.HBaseAdmin;
  5. public class TableExists{
  6. public static void main(String args[])throws IOException{
  7. // Instantiating configuration class
  8. Configuration conf = HBaseConfiguration.create();
  9. // Instantiating HBaseAdmin class
  10. HBaseAdmin admin = new HBaseAdmin(conf);
  11. // Verifying the existance of the table
  12. boolean bool = admin.tableExists("emp");
  13. System.out.println( bool);
  14. }
  15. }
  • To compile and execute:
  1. $javac TableExists.java
  2. $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.
  1. hbase(main):018:0> disable 'emp'
  2. 0 row(s) in 1.4580 seconds
  3. hbase(main):019:0> drop 'emp'
  4. 0 row(s) in 0.3060 seconds
Now, using the exists HBase command, verify whether the table is deleted.
  1. hbase(main):020:07gt; exists 'emp'
  2. Table emp does not exist
  3. 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:
  1. hbase> drop_all ‘t.*’

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.
  1. // creating a configuration object
  2. Configuration conf = HBaseConfiguration.create();
  3. // Creating HBaseAdmin object
  4. 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.
  1. import java.io.IOException;
  2. import org.apache.hadoop.hbase.HBaseConfiguration;
  3. import org.apache.hadoop.conf.Configuration;
  4. import org.apache.hadoop.hbase.client.HBaseAdmin;
  5. public class DeleteTable {
  6. public static void main(String[] args) throws IOException {
  7. // Instantiating configuration class
  8. Configuration conf = HBaseConfiguration.create();
  9. // Instantiating HBaseAdmin class
  10. HBaseAdmin admin = new HBaseAdmin(conf);
  11. // disabling table named emp
  12. admin.disableTable("emp12");
  13. // Deleting emp
  14. admin.deleteTable("emp12");
  15. System.out.println("Table deleted");
  16. }
  17. }
  • To Compile and execute:
  1. $javac DeleteTable.java
  2. $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.

0 comments:

Post a Comment