Data Manipulation HBase Command – Create, Truncate, Scan

1. Objective

In our last HBase tutorial, we have discussed Table Management HBase Commands in detail. Today, in this Hbase Command tutorial, we will see Data Manipulation HBase Command. Moreover, we will learn all commands in HBase which we use to create, update, read, delete, scan, count and truncate Data.
So, let’s start Data Manipulation HBase Commands.

HBase Commands

Data Manipulation HBase Command – Create, Truncate, Scan, Read, Update, Delete

Do you know about HBase Performance Tuning

Hadoop Quiz

2. Data Manipulation HBase Command

Below we are discussing major Data Manipulation HBase Commands:

i. Create Data

Generally, we use following Data Manipulation Commands and methods to create data in an HBase table:

  • put in command.
  • add() method of Put class.
  • put() method of HTable class.

Example of Create Data Manipulation HBase Command:
Here, we are going to create the following table in HBase, we can insert rows into a table, using put command.
Let’s revise HBase Architecture – Regions, Hmaster, Zookeeper

Row KeyPersonal DataPersonal DataProfessional dataProfessional data
Employee no. namecitydesignationsalary
1MonikaPunemanager45000
2MehulHyderabad Sr. Engineer 35000
3PrernaChennaiJr. Engineer15000

A syntax of Create Data Manipulation HBase Command:

put ’<table name>’,’row1’,’<colfamily:colname>’,’<value>’

Inserting the first Row
Now, we are inserting the first row values into the “DataTable” table.

Let’s discuss Career Scope in HBase

hbase(main):005:0> put 'DataTable','1','personal data:name','Monika'
0 row(s) in 0.6600 seconds
hbase(main):006:0> put 'DataTable','1','personal data:city','Pune'
0 row(s) in 0.0410 seconds
hbase(main):007:0> put 'DataTable','1','professional
data:designation','manager'
0 row(s) in 0.0240 seconds
hbase(main):007:0> put 'DataTable','1','professional data:salary','45000'
0 row(s) in 0.0240 seconds

Now, by using the put command in the same way, insert the remaining rows. However, if we insert the whole table,

hbase(main):022:0> scan 'DataTable'
  ROW                         COLUMN+CELL
1 column=personal data:city, timestamp=1417524216501, value=Pune
1 column=personal data:name, timestamp=1417524185058, value=monika
1 column=professional data:designation, timestamp=1417524232601,
value=manager
1 column=professional data:salary, timestamp=1417524244109, value=45000
2 column=personal data:city, timestamp=1417524574905, value=Hyderabad
2 column=personal data:name, timestamp=1417524556125, value=mehul
2 column=professional data:designation, timestamp=1417524592204,
value=sr:engg
2 column=professional data:salary, timestamp=1417524604221, value=35000
3 column=personal data:city, timestamp=1417524681780, value=Chennai
3 column=personal data:name, timestamp=1417524672067, value=Prerna
3 column=professional data:designation, timestamp=1417524693187,
value=jr:engg
3 column=professional data:salary, timestamp=1417524702514,
value=15000

ii. Updating Data Using HBase Shell

Also, using the put command, we can update an existing cell value.
Explore HBase Shell & Commands – Usage & Starting HBase Shell
A Syntax of Update Data Manipulation HBase Command:

put ‘table name’,’row ’,'Column family:column name',’new value’

Here, given value replaces the existing value,i.e. updating the row.
Example for Update Data Manipulation HBase Command:
So, let’s assume with the following data, there is a table in HBase called “DataTable”.

hbase(main):003:0> scan 'DataTable'
ROW               COLUMN + CELL
row1 column = personal:name, timestamp = 1418051555, value = Monika
row1 column = personal:city, timestamp = 1418275907, value = Pune
row1 column = professional:designation, timestamp = 14180555,value = manager
row1 column = professional:salary, timestamp = 1418035791555,value = 45000
1 row(s) in 0.0100 seconds

Now, this command will update the city value of the Employee named ‘Monika’ to Mangalore.

hbase(main):002:0> put 'DataTable','row1','personal:city','Mangalore'
0 row(s) in 0.0400 seconds

Further, we can observe the city of Monika has been changed to ‘Mangalore’.

Let’s discuss HBase Operations

hbase(main):003:0> scan 'DataTable'
 ROW          COLUMN + CELL
row1 column = personal:name, timestamp = 1418035791555, value = Monika
row1 column = personal:city, timestamp = 1418274645907, value = Mangalore
row1 column = professional:designation, timestamp = 141857555,value = manager
row1 column = professional:salary, timestamp = 1418039555, value = 45000
1 row(s) in 0.0100 seconds

iii. Reading Data using HBase Shell

In order to read data from a table in HBase, we use the get command and the get() method of HTable class. We can get a single row of data at a time, using get command.
Read HBase Admin API – Class Descriptor & Class HBaseAdmin
A Syntax for Read Data Manipulaton HBase Command:

get ’<table name>’,’row1’

Example of Read Data Manipulation HBase Command
So, let’s scan the first row of the “DataTable” table.

hbase(main):012:0> get 'DataTable', '1'
  COLUMN                     CELL
personal : city timestamp = 1417521848375, value =Pune
personal : name timestamp = 1417521785385, value = Monika
professional: designation timestamp = 1417521885277, value = manager
professional: salary timestamp = 1417521903862, value = 45000
4 row(s) in 0.0270 seconds
  • Reading a Specific Column

Further, we can see that how to read a specific column by using the get method.
Syntax:

Join DataFlair on Telegram
hbase> get 'table name', ‘rowid’, {COLUMN ⇒ ‘column family:column name ’}

For Example
In order to read a specific column in HBase table, see

hbase(main):015:0> get 'DataTable', 'row1', {COLUMN ⇒ 'personal:name'}
 COLUMN                CELL
personal:name timestamp = 1418035791555, value = monika
1 row(s) in 0.0080 seconds

Have a look at HBase Commands

iv. Deleting a Specific Cell in a Table

We can delete a specific cell in a table, by using the delete command.
A Syntax for Delete Data Manipulation HBase Command

delete ‘<table name>’, ‘<row>’, ‘<column name >’, ‘<time stamp>’

Example of Delete Data Manipulation HBase Command
To delete a specific cell, see this example. Here we are deleting the salary:

hbase(main):006:0> delete 'DataTable', '1', 'personal data:city',
1417521848375
0 row(s) in 0.0060 seconds
  • Deleting All Cells in a Table

We can delete all the cells in a row, by using the “deleteall” command.
Read HBase Use Cases and Real-time Applications
Syntax:

deleteall ‘<table name>’, ‘<row>’,

Example of Delete all cells Data Manipulation HBase Command
Here we are deleting all the cells of row1 of “DataTable” table.

hbase(main):007:0> deleteall 'DataTable','1'
0 row(s) in 0.0240 seconds

Now using the scan command, verify the table.

hbase(main):022:0> scan 'DataTable'
ROW                  COLUMN + CELL
2 column = personal data:city, timestamp = 1417524574905, value = Hyderabad
2 column = personal data:name, timestamp = 1417524556125, value = Mehul
2 column = professional data:designation, timestamp = 1417524204, value = sr:engg
2 column = professional data:salary, timestamp = 1417524604221, value = 35000
3 column = personal data:city, timestamp = 1417524681780, value = Chennai
3 column = personal data:name, timestamp = 1417524672067, value = Prerna
3 column = professional data:designation, timestamp = 1417523187, value = jr:engg
3 column = professional data:salary, timestamp = 141752470214, value = 15000

v. Scanning using HBase Shell

In order to view the data in HTable, we can use the scan command. It is possible to get the table data, by using the scan command.
A Syntax for Scan Data Manipulation HBase Command:

scan ‘<table name>’

Example of Scan Data Manipulation HBase Command:
With the help of scan command, this example shows how to read data from a table.

hbase(main):010:0> scan 'DataTable'
ROW                           COLUMN + CELL
1 column = personal data:city, timestamp = 1417521848375, value = Pune
1 column = personal data:name, timestamp = 1417521785385, value = Monika
1 column = professional data:designation, timestamp = 1417585277,value = manager
1 column = professional data:salary, timestamp = 1417521903862, value = 45000
1 row(s) in 0.0370 seconds

vi. Count Table

By using the count command, we can count the number of rows of a table.
Let’s learn HBase Client API – HTable, Put, Get, Delete, Result
A Syntax for Count Data Manipulation HBase Command:

count ‘<table name>’

So, “DataTable” table will have two rows, after deleting the first row.
To verify it-

hbase(main):023:0> count 'DataTable'
2 row(s) in 0.090 seconds
⇒ 2

vii Truncate Table

It disables drops and recreates a table.
A Syntax for Data Manipulation HBase Command:

hbase> truncate 'table name'

Example of Data Manipulation HBase Command:
Here, is an example, we have truncated the “DataTable” table.

hbase(main):011:0> truncate 'DataTable'
Truncating 'one' table (it may take a while):
  - Disabling table...
  - Truncating table...
  0 row(s) in 1.5950 seconds

Now, use the scan command to verify, after truncating the table. We will get a table with zero rows.

Explore best HBase Books

hbase(main):017:0> scan ‘DataTable’
ROW                  COLUMN + CELL
0 row(s) in 0.3110 seconds

So, this was all about HBase commands in Data Manipulation. Hope you like our explanation.

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. Conclusion

Hence, we have seen all the HBase Data Manipulation Commands. Moreover, These HBase commands are create, update, read, delete, scan, count and truncate data manipulation. Still, if any doubt regarding HBase Commands, ask in the comment tab.
Related article – 
General HBase Commands
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.