HBase shell commands

HBase shell is a JRuby-based shell that provides an interface to HBase to perform operations such as creating tables and other operations. We can go to HBase shell using the following command:

hbase shell

Alternatively, we can use the following command, depending on the directory you are in or whether the environment variable is set:

bin/hbase shell

Once we type in one of the previous commands, we will get a prompt:

hbase(main):026:0>

At this prompt, we can type in the commands. We can always type help to get the list of available commands, and help command_name to get help on the particular command, as follows:

hbase(main):026:0> help

Similarly, with a particular command, it is as follows:

hbase(main):026:0> help 'scan'

Let's look at the commands and their descriptions.

Commands

Description

General Commands

status

This shows the server status, for example:

5 servers, 0 dead, 25.0000 average load

This has three switches, as follows:

hbase> status 'simple'
hbase> status 'summary'
hbase> status 'detailed'

whoami

This shows the current HBase user. The following is the example:

hbase> whoami

version

This shows the version of HBase.

list

This shell command will list out the existing tables in HBase.

The syntax is as follows:

list
list 'stud.*'

count

This counts the number of records in a specified table. The syntax is as follows:

count 'tableName'

The count shows every 1,000 rows by default. The count interval might be optionally specified. Scan caching is enabled on count scans by default. The default cache size is 10 rows. If our rows are small in size, we increase this parameter.

Have a look at the following example:

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

This default HBase counter takes a lot of time to count if there is a lot of records. So, we have the HBase MapReduce JAR file to fasten this operation, which can be called as follows:

hadoop jar hbase.jar rowcount tablenametocount

This counter runs the MapReduce task much faster than the count in HBase.

describe

This describes the table given as parameter; information about the table structure will be displayed.

The syntax is as follows:

describe 'NameOfThetableToDescribe'

exist

If we have thousands of tables, and we need to check whether a table exists in HBase, we can use this command. If there are fewer tables, we can easily verify using a list, and if there are lots of tables, we might find it difficult to scroll through the list of tables, so we can use this command to check.

The syntax is as follows:

exists 'tableToCheck'

is_enabled

This checks whether a table is enabled. The syntax is as follows:

is_enabled 'tableToCheck'

is_disabled

This checks whether a table is disabled.

The syntax is as follows:

is_disabled 'tableToCheck'

show_filters

This displays a list of filters available in HBase.

Have a look at the following example:

hbase> show_filters

Data-manipulation commands

alter

Using this command, we can alter the table and column family schema. Here, we pass the table name and a dictionary specifying a new column family schema.

The following command adds a new column family colFam to table:

alter 'table', {NAME => 'colFam', VERSIONS => 1}

Alternatively, to keep a maximum of two cell versions, use the following command:

hbase> alter 'table', NAME => 'fam1', VERSIONS => 2

To delete the f1 column family in table t1, use the following command:

hbase> alter ''table'', NAME => 'fam1', METHOD => 'delete'

A shorter version of the previous command is:

hbase> alter 'table', 'delete' => 'fam1'

We can also change table-scope attributes such as MAX_FILESIZE, MEMSTORE_FLUSHSIZE, READONLY, and DEFERRED_LOG_FLUSH.

For example, to change the maximum size of a family to 128 MB, we use:

hbase> alter ''table'', METHOD => 'table_att', MAX_FILESIZE => '134217728'

There can be more than one alteration in a single line of command:

hbase> alter ''table'', {NAME => 'fam1'}, {NAME => 'fam2', METHOD => 'delete'}

alter_status

This gives the status of the alter command.

Have a look at the following example:

hbase> alter_status 'talblebingaltered'

alter_async

This command does not wait for all regions to receive the schema changes, whereas alter does.

Have a look at the following example:

hbase> alter_async 't1', NAME => 'f1', METHOD => 'delete'

disable

This disables a table for dropping or modification. The syntax is as follows:

disable 'tableToDisable'

disable_all

This disables all the tables matching the given regex.

Have a look at the following example:

hbase> disable_all 'tab*'

drop

This deletes a table from HBase. So, before dropping the table, it must be disabled, otherwise it will throw an exception that the table is not disabled.

Have a look at the following examples:

disable 'tableToDrop'
drop 'tableToDrop'

drop_all

This drops all of the tables matching the given regex.

Have a look at the following example:

hbase> drop_all 'tab*'

enable

This enables the table after modification.

The syntax is as follows:

enable 'NameOfDisabledTableToEnable'

enable_all

This enables all of the tables matching the given regex.

Have a look at the following example:

hbase> enable_all 'tab*'

delete

This deletes a cell value in a row.

The syntax is as follows:

Delete 'table','row1','colFam1:name'

So, this will delete a name value in row1.

deleteall

This deletes an entire row of a table or a column specified.

The syntax is as follows:

deleteall 'table','row1'

This will delete the entire row1:

deleteall 'table','row1','colFam:name'

This will also delete column names from row1.

truncate

This disables the table, drops the table, and recreates schema. So, when we use drop, we need to disable the table manually, and then drop. If we just want to drop data and not the schema, we can use truncate; it automatically drops and recreates the schema of the dropped table:

hbase> truncate

Data-creation commands

create

This command is used to create a new table with a specified schema.

The syntax is as follows:

create 'tablename','cf1'

This will create a table with a column family cf1, and we can then put data in the table and dynamically add columns.

Have a look at the following examples.

The following command will create a tableToCreate table with the column family colFam1, with five versions of records:

hbase> create 'tableToCreate', {NAME => 'colFam1', VERSIONS => 5}

This will create a tableToCreate table with column families colFam1, colFam2, and colFam3:

hbase> create 'tableToCreate', {NAME => 'colFam1'}, {NAME => 'colFam2'}, {NAME => 'colFam3'}

The shorthand of the previous commands are:

hbase> create 'tableToCreate', 'colFam1', 'colFam2', 'colFam3'
hbase> create 'tableToCreate', {NAME => 'colFam1', VERSIONS => 1, TTL => 2592000, BLOCKCACHE => true}

put

This puts/writes a value at a specified cell in a table or timestamp coordinate. To put a cell value into table t1 at row r1 under column c1 marked with the time ts1, use:

hbase> put 't1', 'r1', 'c1', 'value', ts1

Data-reading commands

scan

This command iterates through the rows in the table and displays on stdout. This lists all the records in the table. A scanner might contain TIMERANGE, FILTER, LIMIT, STARTROW, STOPROW, TIMESTAMP, MAXLENGTH, or COLUMNS. If no columns are specified, all columns will be scanned. To scan all members of a column family, leave the qualifier empty, as in 'col_family:'.

Have a look at the following example:

hbase> scan '.META.'
hbase> scan '.META.', {COLUMNS => 'info:regioninfo'}
hbase> scan 't1', {COLUMNS => ['c1', 'c2'], LIMIT => 10, STARTROW => 'xyz'}
hbase> scan 't1', {FILTER => org.apache.hadoop.hbase.filter.ColumnPaginationFilter.new(1, 0)}
hbase> scan 't1', {COLUMNS => 'c1', TIMERANGE => [1303668804, 1303668904]}

For experts, there is an additional option, -- CACHE_BLOCKS --, which switches block caching for the scanner on (true) or off (false). By default, it is enabled.

Have a look at the following example:

hbase> scan 't1', {COLUMNS => ['c1', 'c2'], CACHE_BLOCKS => false}

get

This gets row or cell contents and passes table names, rows, and optionally, a dictionary of column(s), timestamps, time ranges, and versions.

The following is the example:

hbase> get 'tableName', 'row1'
hbase> get 'tableName', 'row1', {TIMERANGE => [ts1, ts2]}
hbase> get 'tableName', 'row1', {COLUMN => 'c1'}
hbase> get 'tableName', 'row1', {COLUMN => ['c1', 'c2', 'c3']}
hbase> get 'tableName', 'row1', {COLUMN => 'c1', TIMESTAMP => ts1}
hbase> get 'tableName', 'row1', {COLUMN => 'c1', TIMERANGE => [ts1, ts2], VERSIONS => 4}
hbase> get 'tableName', 'row1', {COLUMN => 'c1', TIMESTAMP => ts1, VERSIONS => 4}
hbase> get 'tableName', 'row1', 'c1'
hbase> get 'tableName', 'row1', 'c1', 'c2'
hbase> get 'tableName', 'row1', ['c1', 'c2']

get_counter

This returns a counter cell value at a specified table/row/column location. A cell should be managed with an atomic increment function on HBase, and the data should be binary encoded.

The following is the example:

hbase> get_counter 't1', 'r1', 'c1'

incr

This increments a cell value at a specified table/row/column location.

To increment a cell value in table t1 at row r1 under column c1 by 1 (which can be omitted) or 10, do:

hbase> incr 't1', 'r1', 'c1'
hbase> incr 't1', 'r1', 'c1', 1
hbase> incr 't1', 'r1', 'c1', 10

get_table

Using this, we can assign a table to a variable and perform operations such as put, get, and scan.

Have a look at the following example:

t = get_table 'stud'
t.scan()

Miscellaneous admin commands

close_region

This closes a single region. The close operation is done without the master's involvement (it will not know of the closing operation). Once the region is closed, it will stay closed. Use assign to reopen/reassign. Use unassigned or move to assign the region elsewhere on cluster.

Have a look at the following example:

hbase> close_region 'REGIONNAMEToMove'
hbase> close_region 'REGIONNAME', 'REGIONSERVER_IP:PORT'

assign

This assigns a region and adds true to force the assignment of a region. If a region is already assigned, this will just go ahead and reassign the region.

balance_switch

This enables/disables the balancer and returns the previous balancer state.

Have a look at the following example:

hbase> balance_switch true
hbase> balance_switch false

balancer

HBase has a built-in feature that is called balancer, which by default runs every 5 minutes, and once started, it will try to equal out the assigned region per RegionServer. This will show if the balancer for HBase is enabled.

compact

This compacts all regions in a specified table.

flush

This flushes all regions in a specified table.

Have a look at the following example:

hbase> flush 'TABLENAMEToFlush'
hbase> flush 'REGIONNAMEToFlush'

major_compact

This command runs a major compaction on a specified table.

move

This moves a region.

Have a look at the following example:

hbase> move 'ENCODED_REGIONNAME'
hbase> move 'ENCODED_REGIONNAME', 'SERVER_NAME'

split

This splits the table or an individual region.

unassign

This command unassigns the RegionServer.

zk_dump

This gives the dump status of an HBase cluster, as seen by ZooKeeper.

HBase is rooted at /hbase. The following is how it shows the dump status:

Master address: shashwat.com:60000
Region server holding ROOT: shashwat.com:60020
Region servers:
shashwat.com:60020
Quorum Server Statistics:
shashwat.com:2181
Zookeeper version: <version number>, built on <date time>
Clients:
/127.0.0.1:50641[1](queued=0,recved=3,sent=65)
/127.0.0.1:50637[1](queued=0,recved=13,sent=226)
/127.0.0.1:50644[1](queued=0,recved=14,sent=198)
/127.0.0.1:50643[1](queued=0,recved=63,sent=65)
/127.0.0.1:51874[0](queued=0,recved=1,sent=0)
/127.0.0.1:50713[1](queued=0,recved=63,sent=63)
Latency min/avg/max: 0/8/210
Received: 53
Sent: 626
Outstanding: 0
Zxid: 0x32f0
Mode: standalone
Node count: 1

hlog_roll

This starts writing log messages to a new file. The name of RegionServer should be given as the parameter.

Have a look at the following example:

hbase> hlog_roll

add_peer

This adds a peer cluster to replicate to. The ID must be short and the cluster key must be composed as: hbase.zookeeper.quorum:hbase.zookeeper.property.clientPort:zookeeper.znode.parent.

This gives a full path for HBase to connect to another cluster.

Have a look at the following example:

hbase> add_peer '1', "server1.cie.com:2181:/hbase"
hbase> add_peer '2', "zk1,zk2,zk3:2182:/hbase-1"

list_peers

This lists all replication peer clusters.

Have a look at the following example:

hbase> list_peers

disable_peer

This stops the replication stream to the specified cluster, but still keeps track of new edits to replicate.

Have a look at the following example:

hbase> disable_peer '1'

enable_peer

This restarts the replication to the specified peer cluster, continuing from where it was disabled.

Have a look at the following example:

hbase> enable_peer '1'

remove_peer

This stops the specified replication stream and deletes all the meta information kept about it. The following is the example:

hbase> remove_peer '1'

start_replication

This restarts all the replication features.

Have a look at the following example:

HBase> start_replication

stop_replication

This stops all the replication features. The state in which each stream stops is undetermined.

Have a look at the following example:

hbase> stop_replication

Security commands

grant

This command is used to grant user-specific rights. Grant permissions are either zero or more letters from the set RWXCA: R for read, W for write, X for execute, C for create, and A for admin.

Have a look at the following example:

hbase> grant 'shashwat', 'RWXCA'
hbase> grant 'shashwat', 'RWC', 'table1', 'colFam', 'sal'

revoke

This takes back/revokes access rights.

Have a look at the following example:

hbase> revoke 'shashwat', 'table', 'ColFam', 'sal'

user_permission

This shows all permissions for a particular user.

Have a look at the following example:

hbase> user_permission
hbase> user_permission 'tabStud'

Namespace-related commands

create_namespace

This command is used to create a namespace.

Have a look at the following example:

hbase>create_namespace 'tableStudgroup'
hbase>create 'snamespace:table', 'colfam'

This will create a table in the snamespace namespace with the column family colfam.

drop_namespace

This is used to drop a namespace. Have a look at the following example:

hbase>drop_namespace 'snamespace'

alter_namespace

This alters an existing namespace.

The syntax is as follows:

hbase>alter_namespace 'snamespace', {METHOD => 'set', 'PROPERTY_NAME' => 'PROPERTY_VALUE'}

list_namespace

This lists out namespaces.

Have a look at the following example:

hbase>list_namespace

list_namespace_tables

This lists tables in the namespace.

Have a look at the following example:

hbase> list_namespace_tables 'namespace'

describe_namespace

This displays the namespace description.

Have a look at the following example:

hbase > describe_namespace 'name'

Note

For internal implementation and source code, visit https://github.com/apache/hbase/blob/master/hbase-shell/src/main/ruby/shell.rb.

Tip

Keep in mind that not all commands will run on all the versions of HBase. However, most of the commands will run on the latest version of HBase shell.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.157.197