You can use HBase Shell to manage data in ApsaraDB for HBase, including creating tables, inserting data, deleting data, and deleting tables. This topic provides a brief introduction to HBase Shell.
Access configurations
For ApsaraDB for HBase Standard Edition, see Use HBase Shell to access ApsaraDB for HBase Standard Edition clusters.
For ApsaraDB for HBase Performance-enhanced Edition, see Use HBaseue Shell to access an ApsaraDB for HBase Performance-enhanced Edition instance.
Common commands
This section describes common commands supported by HBase Shell. For more information, see The Apache HBase Shell.
Data definition language
create: creates a table. list: lists all tables in HBase. disable: disables a table. is_disabled: verifies whether a table is disabled. enable: enables a table. is_enabled: verifies whether a table is enabled. describe: provides the description of a table. alter: alters a table. exists: verifies whether a table exists. drop: deletes a table from HBase.
Data manipulation language
put: puts a cell value at a specified column in a specified row in a particular table. get: gets the content of a row or cell. delete: deletes a cell value in a table. deleteall: deletes all cells in a given row. scan: scans and returns table data. count: counts and returns the number of rows in a table. truncate: truncates all data in a table. This command actually disables, deletes, and re-creates a specified table. This command causes the loss of regions. truncate_preserve: truncates all data in a table. This command actually disables, deletes, and re-creates a specified table. The regions in the new table are consistent with those in the old table.
Run and exit HBase Shell
Run the following command to run HBase Shell:
bin/hbase shell
Run the following command to exit HBase Shell:
quit
Run the following command to view basic commands and their usage.
help
Basic operations
Create an HBase table.
Use the
create
command to create a table. When creating a table, you must enter the names of the table and column families.// Create a table named test, which has a column family named cf. create 'test', 'cf'
Query information about all tables.
Use the
list
command to query information about all tables in an HBase database. You can also use regular expressions to filter tables.list list 'abc.*' list 'test'
Insert data.
Use the
put
command to insert data into a new table. For example, you can run the following statements to insert three data records into a table:put 'test', 'row1', 'cf:a', 'value1' put 'test', 'row2', 'cf:b', 'value2' put 'test', 'row3', 'cf:c', 'value3'
NoteIn the preceding statements,
test
is the table name,row1
indicates the rowkey of the table,cf:a
indicates the custom column family name and column name, andvalue1
indicates the value.Query data of a specified table.
The
scan
command can be used to query HBase data. You can use this command to scan a table or query data within a specified range. However, this command returns query results more slowly than theget
command, which is used to query a single data record. In this example, the scan command is used because the demo database stores only a small amount of data. Run the following statement to query data in the table named test:scan 'test'
The following data is returned:
ROW COLUMN+CELL row1 column=cf:a, timestamp=1421762485768, value=value1 row2 column=cf:b, timestamp=1421762491785, value=value2 row3 column=cf:c, timestamp=1421762496210, value=value3 3 row(s) in 0.0230 seconds
Query a single data record in a table
Use the
get
command to query a single data record in a table.get 'test', 'row1'
The following data is returned:
COLUMN CELL cf:a timestamp=1421762485768, value=value1 1 row(s) in 0.0350 seconds
Disable and enable a specified table.
If you want to delete a table or modify its settings, you must use the disable command to disable the table first. After you delete the table or modify its settings, use the enable command to enable the table.
disable 'test' enable 'test'
Delete a specified table.
Use the drop command to delete a table. Exercise caution with this command.
drop 'test'
Common configurations
Set the major compaction period for a specified table. We recommend that you do not set this period unless otherwise required.
The major compaction period is measured in milliseconds (ms). The default value is 7 days. If you set the value to 0, the major compaction period is disabled.
alter 'test', CONFIGURATION => {'hbase.hregion.majorcompaction' => 300000}
Set the data compression format for the column families of a specified table.
For more information about data compression of ApsaraDB for HBase, see Data compression and encoding.
alter 'test', NAME => 'cf', COMPRESSION => 'SNAPPY'
Set the block encoding type for the column families of a specified table.
For example, you can run the following statement to set the block encoding type to DATA_BLOCK_ENCODING for the column family named cf of the table named test.
alter 'test', NAME => 'cf', DATA_BLOCK_ENCODING => 'DIFF'
Set the time to live (TTL) for data in the column families of a specified table.
The TTL is measured in seconds. In this example, the TTL value is set to 2592000 seconds, which is equal to 30 days.
alter 'test', NAME => 'cf', TTL => 2592000
Pre-split a specified table into regions. For more information, see Pre-split.