In this section we will teach you how to create HBase table and insert data into it.
HBase Create Table Example
In this section you will learn to connect to HBase shell and the create table. We will explains you the commands and steps for creating table, inserting data, viewing data stored in the table and finally learn to delete the table.
What you will learn?
- Connecting to HBase through hbase shell
- Creating table
- Adding data into table
- List all data from table
- Disable table
- Drop table
What is HBase shell?
Hbase shell is command line utility which is used to connect to HBase server from command line. This tool allows you to create, update and delete tables. This tool is also used for inserting, updating, selecting and deleting database from the HBase table. To access the HBase shell you have to type hbase shell on the command prompt on the HBase server. It connects to the HBase server and provides command prompt to run queries.
In this tutorial we are going to create a table called 'Employee' with two column families 'present address', 'permanent address' for the Employee table.
|Row key||present address||permanent address|
First of all connect to hbase shell by typing following command on command prompt on Ubuntu server running HBase:
hbase shell This will open the hbase shell and then you can interact with the HBase databse. root@hadoop1:/home/deepak# hbase shell SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/usr/hdp/220.127.116.11-169/hadoop/lib/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/usr/hdp/18.104.22.168-169/zookeeper/lib/slf4j-log4j12-1.6.1.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory] HBase Shell; enter 'help<RETURN>' for list of supported commands. Type "exit<RETURN>" to leave the HBase Shell Version 22.214.171.124.4.0.0-169, r61dfb2b344f424a11f93b3f086eab815c1eb0b6a, Wed Feb 10 06:35:13 UTC 2016 hbase(main):001:0>
You can view the existing table by running following command on the console:
To Create Employee table run the following command:
create 'Employee', 'present address', 'permanent address'
It creates the 'Employee' table with two column families 'present address' and 'permanent address'.
Adding data to HBase Database
To add the data into HBase table put command is used. Put command table <table name>, <row key>, <column family:key>, <data> to insert the data into HBase table. We are the following command add data:
put 'Employee' ,'row1','present address:city','Delhi';
Here first parameter is table name 'Employee', second parameter is <row key> eg. 'row1' and then the column family and data.
Run the following command to add data into 'present address' column family of 'Employee' table:
put 'Employee' ,'row1','present address:city','Delhi'; put 'Employee' ,'row1','present address:colony','Rohini'; put 'Employee' ,'row1','present address:sector','Sector 3';
Now run the following commands to add data into 'permanent address' column family:
put 'Employee' ,'row1','permanent address:city','Varanasi,UP'; put 'Employee' ,'row1','permanent address:colony','Dashashwamedh Ghat Road'; put 'Employee' ,'row1','permanent address:Area','Sakarkand gali Meerghat';
To view the data run the following command:
scan 'Employee'Above command will display all the data from 'Employee' table.
To drop table you have first disable the 'Employee' table with following command:
After that you can run following command to drop 'Employee' table:
HBase shell will display the success message after deleting 'Employee' table. You can verify this by running the list command on the HBase shell.
In this tutorial you learned how to create table with column families, then add the data into table, display all the data from database and finally drop the table. With the completion of this tutorial you are now familiar with the HBase database and the process of using HBase shell.