Use the HBase Shell command-line tool to perform data management tasks in ApsaraDB for HBase, such as creating and dropping tables, and inserting and deleting data. This topic describes the basic commands and their usage.
Access configuration
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 Connect to a performance-enhanced cluster with HBase Shell.
Common commands
For a complete list of HBase Shell commands, see The Apache HBase Shell.
Shell data definition language (DDL) commands
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: Describes a table. alter: Alters a table. exists: Verifies whether a table exists. drop: Drops a table from HBase.Shell data manipulation language (DML) commands
put: Puts a cell value into a specific row and column of a 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: Removes all data from a table by disabling, dropping, and recreating it. This process discards the original regions. truncate_preserve: Removes all data from a table by disabling, dropping, and recreating it, but preserves the original regions.
Start and exit the Shell
Run the following command to start the Shell environment.
bin/hbase shellRun the following command to exit the Shell environment.
quitUse the
helpcommand to view a list of basic commands and their usage.help
Basic operations
Create a table
Use the
createcommand to create a table. You must specify the table name and at least one column family.// Create a table named test with a column family named cf. create 'test', 'cf'List all tables
Use the
listcommand to list all tables in the HBase database. You can also use a regular expression to filter the table list.list list 'abc.*' list 'test'Insert data
Use the
putcommand to insert data into a table. The following commands insert three rows of data.put 'test', 'row1', 'cf:a', 'value1' put 'test', 'row2', 'cf:b', 'value2' put 'test', 'row3', 'cf:c', 'value3'NoteIn these commands,
testis the table name,row1is the rowkey,cf:aspecifies the column by its column family (cf) and column qualifier (a), andvalue1is the value to insert.Scan data in a table
scanis a flexible way to access data in HBase. You can use ascanoperation to scan an entire table or query a specific range. It is slightly slower than retrieving a single row by usingget. In this demo, we retrieve all data because the database contains only a small amount of data. Run the following statement to query data from the test table.scan 'test'The output is similar to the following:
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 secondsGet a single row from a table
Use the
getcommand to retrieve a single row from a table.get 'test', 'row1'The output is similar to the following:
COLUMN CELL cf:a timestamp=1421762485768, value=value1 1 row(s) in 0.0350 secondsDisable and enable a table
To delete a table or change its settings, you must first disable it with the
disablecommand. After completing the operation, use theenablecommand to make the table available again.disable 'test' enable 'test'Delete a table
To delete a table, use the
dropcommand. This is a destructive operation. Use it with caution.drop 'test'
Common configurations
Set the major compaction period for a table. Change this setting only when necessary.
The major compaction period is measured in milliseconds (ms). The default value is 7 days. Setting the value to 0 disables periodic major compactions.
alter 'test', CONFIGURATION => {'hbase.hregion.majorcompaction' => 300000}Set the data compression algorithm for a column family.
For more information about data compression in ApsaraDB for HBase, see Data compression and encoding.
alter 'test', NAME => 'cf', COMPRESSION => 'SNAPPY'Set the block encoding type for a column family.
The following command sets the block encoding type to
DIFFfor thecfcolumn family in thetesttable.alter 'test', NAME => 'cf', DATA_BLOCK_ENCODING => 'DIFF'Set the time to live (TTL) for a column family.
The TTL is measured in seconds (s). For example, a value of 2592000 is equivalent to 30 days.
alter 'test', NAME => 'cf', TTL => 2592000Set pre-partitions for a table. For more information about how to pre-partition a table in ApsaraDB for HBase, see Set pre-partitions.