HBase Shell

更新时间:
复制 MD 格式

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

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 shell
  • Run the following command to exit the Shell environment.

    quit
  • Use the help command to view a list of basic commands and their usage.

    help

Basic operations

  • Create a table

    Use the create command 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 list command 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 put command 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'
    Note

    In these commands, test is the table name, row1 is the rowkey, cf:a specifies the column by its column family (cf) and column qualifier (a), and value1 is the value to insert.

  • Scan data in a table

    scan is a flexible way to access data in HBase. You can use a scan operation to scan an entire table or query a specific range. It is slightly slower than retrieving a single row by using get. 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 seconds
  • Get a single row from a table

    Use the get command 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 seconds
  • Disable and enable a table

    To delete a table or change its settings, you must first disable it with the disable command. After completing the operation, use the enable command to make the table available again.

    disable 'test'
    enable 'test'
  • Delete a table

    To delete a table, use the drop command. 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 DIFF for the cf column family in the test table.

    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 => 2592000
  • Set pre-partitions for a table. For more information about how to pre-partition a table in ApsaraDB for HBase, see Set pre-partitions.