HBase Shell Commands with Examples

After successful installation of HBase on top of Hadoop, we get an interactive shell to execute various commands and perform several operations. Using these commands, we can perform multiple operations on data-tables that can give better data storage efficiencies and flexible interaction by the client.

We can interact with HBase in two ways,

In HBase, interactive shell mode is used to interact with HBase for table operations, table management, and data modeling. By using Java API model, we can perform all type of table and data operations in HBase. We can interact with HBase using this both methods.

The only difference between these two is Java API use java code to connect with HBase and shell mode use shell commands to connect with HBase.

Quick overcap of HBase before we proceed-

For examples,

In this tutorial- you will learn,

General commands

In Hbase, general commands are categorized into following commands

To get enter into HBase shell command, first of all, we have to execute the code as mentioned below

HBase Shell and General Commands

hbase Shell

Once we get to enter into HBase shell, we can execute all shell commands mentioned below. With the help of these commands, we can perform all type of table operations in the HBase shell mode.

Let us look into all of these commands and their usage one by one with an example.

Status

Syntax:status

This command will give details about the system status like a number of servers present in the cluster, active server count, and average load value. You can also pass any particular parameters depending on how detailed status you want to know about the system. The parameters can be 'summary', 'simple', or 'detailed', the default parameter provided is "summary".

Below we have shown how you can pass different parameters to the status command.

If we observe the below screen shot, we will get a better idea.

HBase Shell and General Commands

hbase(main):001:0>status
hbase(main):002:0>status 'simple'
hbase(main):003:0>status 'summary'
hbase(main):004:0> status 'detailed'

When we execute this command status, it will give information about number of server's present, dead servers and average load of server, here in screenshot it shows the information like- 1 live server, 1 dead servers, and 7.0000 average load.

Version

Syntax: version

HBase Shell and General Commands

Table help

Syntax:table_help

HBase Shell and General Commands

This command guides

whoami

Syntax:

Syntax: Whoami

HBase Shell and General Commands

This command "whoami" is used to return the current HBase user information from the HBase cluster.

It will provide information like

TTL(Time To Live) - Attribute

In HBase, Column families can be set to time values in seconds using TTL. HBase will automatically delete rows once the expiration time is reached. This attribute applies to all versions of a row – even the current version too.

The TTL time encoded in the HBase for the row is specified in UTC. This attribute used with table management commands.

Important differences between TTL handling and Column family TTLs are below

Tables Managements commands

These commands will allow programmers to create tables and table schemas with rows and column families.

The following are Table Management commands

Let us look into various command usage in HBase with an example.

Create

Syntax: create <tablename>, <columnfamilyname>

HBase Shell and General Commands

Example:-

hbase(main):001:0> create 'education' ,'gtupapers'
0 rows(s) in 0.312 seconds
=>Hbase::Table – education

The above example explains how to create a table in HBase with the specified name given according to the dictionary or specifications as per column family. In addition to this we can also pass some table-scope attributes as well into it.

In order to check whether the table 'education' is created or not, we have to use the "list" command as mentioned below.

List

Syntax:list

HBase Shell and General Commands

Describe

Syntax:describe <table name>

HBase Shell and General Commands

hbase(main):010:0>describe 'education'

This command describes the named table.

disable

Syntax: disable <tablename>

HBase Shell and General Commands

hbase(main):011:0>disable 'education'

Here, in the above screenshot we are disabling table education

disable_all

 Syntax: disable_all<"matching regex"

Enable

Syntax: enable <tablename>

HBase Shell and General Commands

hbase(main):012:0>enable 'education'

show_filters

Syntax: show_filters

HBase Shell and General Commands

This command displays all the filters present in HBase like ColumnPrefix Filter, TimestampsFilter, PageFilter, FamilyFilter, etc.

drop

Syntax:drop <table name>

HBase Shell and General Commands

hbase(main):017:0>drop 'education'

We have to observe below points for drop command

drop_all

Syntax: drop_all<"regex">

is_enabled

Syntax: is_enabled 'education'

This command will verify whether the named table is enabled or not. Usually, there is a little confusion between "enable" and "is_enabled" command action, which we clear here

alter

Syntax: alter <tablename>, NAME=><column familyname>, VERSIONS=>5

This command alters the column family schema. To understand what exactly it does, we have explained it here with an example.

Examples:

In these examples, we are going to perform alter command operations on tables and on its columns. We will perform operations like

  1. To change or add the 'gtupapers_1' column family in table 'education' from current value to keep a maximum of 5 cell VERSIONS,

HBase Shell and General Commands

hbase> alter 'education', NAME='gtupapers_1', VERSIONS=>5
  1. You can also operate the alter command on several column families as well. For example, we will define two new column to our existing table "education".
 hbase> alter 'edu', 'gtupapers_1', {NAME => 'gtupapers_2', IN_MEMORY => true}, {NAME => 'gtupapers_3', VERSIONS => 5}

HBase Shell and General Commands

  1. In this step, we will see how to delete column family from the table. To delete the 'f1' column family in table 'education'.

Use one ofthese commands below,

hbase> alter 'education', NAME => 'f1', METHOD => 'delete'
hbase> alter 'education', 'delete' =>' gtupapers_1'    

HBase Shell and General Commands

  1. As shown in the below screen shots, it shows two steps – how to change table scope attribute and how to remove the table scope attribute.
Syntax: alter <'tablename'>, MAX_FILESIZE=>'132545224'

HBase Shell and General Commands

Step 1) You can change table-scope attributes like MAX_FILESIZE, READONLY, MEMSTORE_FLUSHSIZE, DEFERRED_LOG_FLUSH, etc. These can be put at the end;for example, to change the max size of a region to 128MB or any other memory value we use this command.

Usage:

NOTE: MAX_FILESIZE Attribute Table scope will be determined by some attributes present in the HBase. MAX_FILESIZE also come under table scope attributes.

Step 2) You can also remove a table-scope attribute using table_att_unset method. If you see the command

alter 'education', METHOD => 'table_att_unset', NAME => 'MAX_FILESIZE'

alter_status

 Syntax: alter_status 'education'

HBase Shell and General Commands

Data manipulation commands

These commands will work on the table related to data manipulations such as putting data into a table, retrieving data from a table and deleting schema, etc.

The commands come under these are

Let look into these commands usage with an example.

Count

Syntax: count <'tablename'>, CACHE =>1000

Example:

HBase Shell and General Commands

hbase> count 'gtupapers', CACHE=>1000

This example count fetches 1000 rows at a time from "gtupapers" table.

We can make cache to some lower value if the table consists of more rows.

But by default it will fetch one row at a time.

hbase>count 'gtupapers', INTERVAL => 100000
hbase> count 'gtupapers', INTERVAL =>10, CACHE=> 1000

If suppose if the table "gtupapers" having some table reference like say g.

We can run the count command on table reference also like below

hbase>g.count INTERVAL=>100000
hbase>g.count INTERVAL=>10, CACHE=>1000

Put

Syntax:  put <'tablename'>,<'rowname'>,<'columnvalue'>,<'value'>

This command is used for following things

Example:

HBase Shell and General Commands

To check whether the input value is correctly inserted into the table, we use "scan" command. In the below screen shot, we can see the values are inserted correctly

HBase Shell and General Commands

Code Snippet: For Practice

create 'gtupapers', {NAME=>'Edu', VERSIONS=>213423443}
put 'gtupapers', 'r1', 'Edu:c1', 'value', 10
put 'gtupapers', 'r1', 'Edu:c1', 'value', 15
put 'gtupapers', 'r1', 'Edu:c1', 'value', 30

From the code snippet, we are doing these things

Get

Syntax: get <'tablename'>, <'rowname'>, {< Additional parameters>}

Here <Additional Parameters> include TIMERANGE, TIMESTAMP, VERSIONS and FILTERS.

By using this command, you will get a row or cell contents present in the table. In addition to that you can also add additional parameters to it like TIMESTAMP, TIMERANGE,VERSIONS, FILTERS, etc. to get a particular row or cell content.

HBase Shell and General Commands

Examples:-

hbase> get 'gtupapers', 'r1', {COLUMN => 'c1'}

For table "gtupapers' row r1 and column c1 values will display using this command as shown in the above screen shot

hbase> get 'gtupapers', 'r1'

For table "gtupapers"row r1 values will be displayed using this command

hbase> get 'gtupapers', 'r1', {TIMERANGE => [ts1, ts2]}

For table "gtupapers"row 1 values in the time range ts1 and ts2 will be displayed using this command

hbase> get 'gtupapers', 'r1', {COLUMN => ['c1', 'c2', 'c3']}

For table "gtupapers" row r1 and column families' c1, c2, c3 values will be displayed using this command

Delete

Syntax:delete <'tablename'>,<'row name'>,<'column name'>

HBase Shell and General Commands

Example:

hbase(main):)020:0> delete 'gtupapers', 'r1', 'c1''. 

deleteall

Syntax: deleteall <'tablename'>, <'rowname'>

HBase Shell and General Commands

Example:-

hbase>deleteall 'gtupapers', 'r1', 'c1'

This will delete all the rows and columns present in the table. Optionally we can mention column names in that.

Truncate

Syntax:  truncate <tablename>

HBase Shell and General Commands

After truncate of an hbase table, the schema will present but not the records. This command performs 3 functions; those are listed below

Scan

Syntax: scan <'tablename'>, {Optional parameters}

This command scans entire table and displays the table contents.

scan 'gtupapers' 

The output as below shown in screen shot

HBase Shell and General Commands

In the above screen shot

Examples:-

The different usages of scan command

Command

Usage

scan '.META.', {COLUMNS => 'info:regioninfo'}

It display all the meta data information related to columns that are present in the tables in HBase

scan 'gtupapers', {COLUMNS => ['c1', 'c2'], LIMIT => 10, STARTROW => 'xyz'}

It display contents of table gtupapers with their column families c1 and c2 limiting the values to 10

scan 'gtupapers', {COLUMNS => 'c1', TIMERANGE => [1303668804, 1303668904]}

It display contents of gtupapers with its column name c1 with the values present in between the mentioned time range attribute value

scan 'gtupapers', {RAW => true, VERSIONS =>10}

In this command RAW=> true provides advanced feature like to display all the cell values present in the table gtupapers

Code Example:

First create table and place values into table

create 'gtupapers', {NAME=>'e', VERSIONS=>2147483647}
put 'gtupapers', 'r1', 'e:c1', 'value', 10
put 'gtupapers', 'r1', 'e:c1', 'value', 12
put 'gtupapers', 'r1', 'e:c1', 'value', 14
delete 'gtupapers', 'r1', 'e:c1', 11

Input Screenshot:

HBase Shell and General Commands

If we run scan command

Query: scan 'gtupapers', {RAW=>true, VERSIONS=>1000}

It will display output shown in below.

Output screen shot:

HBase Shell and General Commands

The output shown in above screen shot gives the following information

Cluster Replication Commands

Command

Functionality

add_peer

Add peers to cluster to replicate

hbase> add_peer '3', zk1,zk2,zk3:2182:/hbase-prod

remove_peer

Stops the defined replication stream.

Deletes all the metadata information about the peer

hbase> remove_peer '1'

start_replication

Restarts all the replication features

hbase> start_replication

stop_replication

Stops all the replication features

hbase>stop_replication

Summary:

HBase shell and general commands give complete information about different type of data manipulation, table management, and cluster replication commands. We can perform various functions using these commands on tables present in HBase.

 

YOU MIGHT LIKE: