🖥️ HBase Shell and General Commands

🖥️ HBase Shell 和通用命令

Overview of HBase Shell

HBase Shell 概述

“HBase shell is an application software which encapsulates Java client API, allowing us to interact with HBase through the command line.”
“HBase shell 是一个封装了 Java 客户端 API 的应用软件,允许我们通过命令行与 HBase 进行交互。”

Using HBase Shell

使用 HBase Shell

  • HBase shell operates mainly in ==interactive mode== for accessing HBase.
  • HBase shell 主要以 ==交互模式== 运行以访问 HBase。
  • It allows performing multiple operations on data-tables for better data storage efficiency and flexible interaction.
  • 它允许对数据表执行多种操作,以提高数据存储效率和实现灵活的交互。

Types of HBase Commands

HBase 命令的类型

HBase commands are categorized into the following types:
HBase 命令分为以下几种类型:

  • General Commands
  • 通用命令
  • Data Definition Commands (DDL)
  • 数据定义命令 (DDL)
  • Data Manipulation Commands (DML)
  • 数据操作命令 (DML)
  • Security Commands
  • 安全命令

General Commands

通用命令

Entering HBase Command Line

进入 HBase 命令行

  • To enter the HBase shell command line, navigate to the HBase bin directory and execute:
  • 要进入 HBase shell 命令行,请导航到 HBase 的 bin 目录并执行:
    1
    ./hbase shell
  • To quit the shell, use:
  • 要退出 shell,请使用:
    1
    exit
  • To display help information, use:
  • 要显示帮助信息,请使用:
    1
    help
  • To check the status of the HBase cluster, use:
  • 要检查 HBase 集群的状态,请使用:
    1
    status
  • To view the version information of HBase, use:
  • 要查看 HBase 的版本信息,请使用:
    1
    version

Namespace Commands

命名空间命令

  • Create Namespace

  • 创建命名空间

    • Syntax: create_namespace <namespace_name>
    • 语法: create_namespace <namespace_name>
    • Example:
    • 示例:
      1
      create_namespace 'my_space'
  • Delete Namespace

  • 删除命名空间

    • Syntax: drop_namespace <namespace_name>
    • 语法: drop_namespace <namespace_name>
    • Example:
    • 示例:
      1
      drop_namespace 'my_space'

Table Commands

表命令

  • List Tables

  • 列出表

    • Syntax: list
    • 语法: list
      • Lists all current tables.
      • 列出所有当前的表。
  • Create Table

  • 创建表

    • Syntax: create <namespace>:<table>, {NAME => <family>, VERSIONS => <VERSIONS>}
    • 语法: create <namespace>:<table>, {NAME => <family>, VERSIONS => <VERSIONS>}
    • Example:
    • 示例:
      1
      create 'my_namespace:t12', {NAME => 'f1', VERSIONS => 2}, {NAME => 'f2', VERSIONS => 2}
  • Delete Table

  • 删除表

    • Steps:
    • 步骤:
      1. Disable the table.
      2. 禁用表。
      3. Drop the table.
      4. 删除表。
    • Example:
    • 示例:
      1
      2
      disable 't1'
      drop 't1'
  • View Table Structure

  • 查看表结构

    • Syntax: describe <table>
    • 语法: describe <table>
    • Example:
    • 示例:
      1
      describe 't1'
  • Modify Table Structure

  • 修改表结构

    • Syntax: alter <table>, {NAME => <family>}, {NAME => <family>, METHOD => 'delete'}
    • 语法: alter <table>, {NAME => <family>}, {NAME => <family>, METHOD => 'delete'}
    • Example:
    • 示例:
      1
      2
      disable 't1'
      alter 't1', {NAME => 'body', TTL => '15552000'}, {NAME => 'meta', TTL => '15552000'}

Data Manipulation Commands (DML)

数据操作命令 (DML)

Adding and Querying Data

添加和查询数据

  • Add Data

  • 添加数据

    • Syntax: put <table>, <rowkey>, <family:column>, <value>, <timestamp>
    • 语法: put <table>, <rowkey>, <family:column>, <value>, <timestamp>
    • Example:
    • 示例:
      1
      put 't1', 'rowkey001', 'f1:col1', 'value01'
  • Query Data

  • 查询数据

    • Syntax: get <table>, <rowkey>, [<family:column> ...]
    • 语法: get <table>, <rowkey>, [<family:column> ...]
    • Examples:
    • 示例:
      • Specific column:
      • 特定列:
        1
        get 't1', 'rowkey001', 'f1:col1'
      • All columns in a family:
      • 一个列族中的所有列:
        1
        get 't1', 'rowkey002', 'f1'
  • Scan Table

  • 扫描表

    • Syntax: scan <table>, {COLUMNS => [<family:column>], LIMIT => num}
    • 语法: scan <table>, {COLUMNS => [<family:column>], LIMIT => num}
    • Example:
    • 示例:
      1
      scan 't1', {LIMIT => 5}
  • Count Rows

  • 计算行数

    • Syntax: count <table>, {INTERVAL => intervalNum, CACHE => cacheNum}
    • 语法: count <table>, {INTERVAL => intervalNum, CACHE => cacheNum}
    • Parameters:
    • 参数:
      • INTERVAL: Number of lines displayed at once (default is 1000).
      • INTERVAL: 一次显示的行数(默认为 1000)。
      • CACHE: Size of buffer (default is 10).
      • CACHE: 缓冲区大小(默认为 10)。
  • Delete Column Value

  • 删除列值

    • Syntax: delete <table>, <rowkey>, <family:column>, <timestamp>
    • 语法: delete <table>, <rowkey>, <family:column>, <timestamp>
    • Example:
    • 示例:
      1
      delete 't1', 'rowkey001', 'f1:col1'
  • Delete Row

  • 删除行

    • Syntax: deleteall <table>, <rowkey>, <family:column>, <timestamp>
    • 语法: deleteall <table>, <rowkey>, <family:column>, <timestamp>
    • Example:
    • 示例:
      1
      deleteall 't1', 'rowkey001'
  • Delete All Data in a Table

  • 删除表中的所有数据

    • Syntax: truncate <table>
    • 语法: truncate <table>
    • Steps:
    • 步骤:
      1. Disable the table.
      2. 禁用表。
      3. Drop the table.
      4. 删除表。
      5. Create the table again.
      6. 再次创建表。
    • Example:
    • 示例:
      1
      truncate 't1'

Security Commands

安全命令

Granting and Revoking Permissions

授予和撤销权限

  • Grant Permissions

  • 授予权限

    • Syntax: grant <user> <permissions> [<table> [<column family> [<column qualifier>]]]
    • 语法: grant <user> <permissions> [<table> [<column family> [<column qualifier>]]]
    • Example:
    • 示例:
      1
      grant 'MyPrivilege', 'RWXCA'
    • Permission Codes:
    • 权限代码:
      • R: Read
      • R: 读
      • W: Write
      • W: 写
      • X: Execute
      • X: 执行
      • C: Create
      • C: 创建
      • A: Admin
      • A: 管理
  • Revoke Permissions

  • 撤销权限

    • Syntax: revoke <user>
    • 语法: revoke <user>
    • Example:
    • 示例:
      1
      revoke 'MyPrivilege'
  • User Permission Listing

  • 用户权限列表

    • Syntax: user_permission '<tablename>'
    • 语法: user_permission '<tablename>'
    • Example:
    • 示例:
      1
      user_permission 'emp'

HBase API Operations

HBase API 操作

Connecting to HBase

连接到 HBase

  • Configuration for Client Connection
  • 客户端连接配置
    1
    2
    3
    Configuration conf = HBaseConfiguration.create();
    conf.set("hbase.zookeeper.quorum", "s1, s2, s3");
    conf.set("hbase.zookeeper.property.clientPort", "2181");

Using HBase Client API

使用 HBase 客户端 API

  • Inserting Data

  • 插入数据

    1
    2
    3
    Put put = new Put(Bytes.toBytes(rowKey));
    put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(column), Bytes.toBytes(value));
    table.put(put);
  • Deleting Data

  • 删除数据

    1
    2
    3
    Delete deleteColumn = new Delete(Bytes.toBytes(rowKey));
    deleteColumn.deleteColumns(Bytes.toBytes(familyName), Bytes.toBytes(columnName));
    table.delete(deleteColumn);
  • Querying Data

  • 查询数据

    1
    2
    3
    4
    5
    6
    7
    Get get = new Get(Bytes.toBytes(rowKey));
    Result res = table.get(get);
    for (Cell cell : res.rawCells()) {
    System.out.println("CF: " + Bytes.toString(CellUtil.cloneFamily(cell)) +
    ", CN: " + Bytes.toString(CellUtil.cloneQualifier(cell)) +
    ", Value: " + Bytes.toString(CellUtil.cloneValue(cell)));
    }

🛠️ HBase Shell and General Commands

🛠️ HBase Shell 和通用命令

HBase Query Data Scanning

HBase 查询数据扫描

Scanning for Results

扫描结果

In HBase, we can scan a large amount of query data using the following method:
在 HBase 中,我们可以使用以下方法扫描大量查询数据:

1
2
3
4
public ResultScanner getScanner(Scan scan) {
ResultScanner resultScanner = table.getScanner(new Scan());
Iterator<Result> results = resultScanner.iterator();
}

Iterating Over Results

遍历结果

To process the results of a scan, we can use a loop as shown below:
要处理扫描结果,我们可以使用如下所示的循环:

1
2
3
4
5
6
7
8
9
10
11
12
for (Result result : resultScanner) {
for (Cell cell : result.rawCells()) {
System.out.println("RK: " +
Bytes.toString(CellUtil.cloneRow(cell)) +
", CF: " +
Bytes.toString(CellUtil.cloneFamily(cell)) +
", CN: " +
Bytes.toString(CellUtil.cloneQualifier(cell)) +
", Value: " +
Bytes.toString((CellUtil.cloneValue(cell))));
}
}

This code snippet prints the row key (RK), column family (CF), column name (CN), and the value for each cell in the result set.
此代码片段打印结果集中每个单元格的行键 (RK)、列族 (CF)、列名 (CN) 和值。

HBase Management API

HBase 管理 API

Admin Package

Admin 包

Package Location: org.apache.hadoop.hbase.client.Admin
包位置: org.apache.hadoop.hbase.client.Admin

Function: HBase Management API interface.
功能: HBase 管理 API 接口。

Usage Example:
使用示例:

1
2
3
4
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "zk1, zk2, zk3");
Connection connection = ConnectionFactory.createConnection(conf);
Admin admin = connection.getAdmin();

TableDescriptorBuilder

Package Location: org.apache.hadoop.hbase.client.TableDescriptorBuilder
包位置: org.apache.hadoop.hbase.client.TableDescriptorBuilder

Function: Represents the information of the HBase table.
功能: 代表 HBase 表的信息。

Usage:
用法:

  • setMaxFileSize: Specify the largest region size.
  • setMaxFileSize: 指定最大区域大小。
  • setMemStoreFlushSize: Specify the file size on HDFS from the memory flush to HDFS.
  • setMemStoreFlushSize: 指定从内存刷新到 HDFS 的文件大小。

ColumnFamilyDescriptor

ColumnFamilyDescriptor

Package Location: org.apache.hadoop.hbase.client.ColumnFamilyDescriptor
包位置: org.apache.hadoop.hbase.client.ColumnFamilyDescriptor

Function: Represents the column family information of the HBase table.
功能: 代表 HBase 表的列族信息。

Usage Example:
使用示例:

1
2
TableDescriptorBuilder htd = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName));
ColumnFamilyDescriptor of = ColumnFamilyDescriptorBuilder.of("myFamily");

To create a table with multiple column families:
要创建具有多个列族的表:

1
2
3
4
5
6
7
TableDescriptorBuilder htd = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName));
for (String column : columns) {
ColumnFamilyDescriptor of = ColumnFamilyDescriptorBuilder.of(column);
htd.setColumnFamily(of);
}
// create table
admin.createTable(htd);

Admin API Operations

Admin API 操作

Adding and Modifying Tables

添加和修改表

  • Add Column Family:

  • 添加列族:

    1
    htd.setColumnFamily(obj);
  • Delete Table:

  • 删除表:

    • First disable the table:
    • 首先禁用表:
      1
      admin.disableTable(TableName.valueOf(tableName));
    • Then delete it:
    • 然后删除它:
      1
      admin.deleteTable(TableName.valueOf(tableName));
  • Modifying Column Family Properties:

  • 修改列族属性:

    • First disable the table, modify properties, then enable it back:
    • 首先禁用表,修改属性,然后重新启用它:
      1
      2
      3
      admin.disableTable(TableName.valueOf(tableName));
      admin.modifyTable(htd);
      admin.enableTable(TableName.valueOf(tableName));

HBase Practice Questions

HBase 练习题

  1. Which of the following is not a DDL command?

  2. 以下哪项不是 DDL 命令?

    • a. list
    • b. create
    • c. scan
    • d. drop
  3. Which of the following is not a DML command?

  4. 以下哪项不是 DML 命令?

    • a. put
    • b. get
    • c. scan
    • d. list
  5. Which package contains the Connection interface?

  6. 哪个包包含 Connection 接口?

    • a. org.apache.hadoop.hbase.client.Connection
    • b. org.apache.hadoop.hbase.Connection
    • c. org.apache.hadoop.Connection
    • d. org.apache.Connection
  7. Which package contains the Table interface?

  8. 哪个包包含 Table 接口?

    • a. org.apache.hadoop.hbase.client.Table
    • b. org.apache.hadoop.hbase.Table
    • c. org.apache.hadoop.Table
    • d. org.apache.Table

Key Takeaways

重点摘要

  • Understanding HBase shell commands, including general commands, DML commands, and security commands.
  • 理解 HBase shell 命令,包括通用命令、DML 命令和安全命令。
  • Use the command create <name_space>:<table>, {NAME => <family>, VERSIONS => <VERSIONS>} to create a table.
  • 使用 create <name_space>:<table>, {NAME => <family>, VERSIONS => <VERSIONS>} 命令来创建表。
  • Client configuration is required to connect to HMaster for API operations.
  • 连接到 HMaster 进行 API 操作需要客户端配置。
  • Permissions can be granted and revoked using the commands: grant, revoke, and user_permission.
  • 可以使用以下命令授予和撤销权限:grant、revoke 和 user_permission。
  • HBase provides an API in Java to perform basic CRUD operations and implements essential tool classes.
  • HBase 在 Java 中提供了一个 API 来执行基本的 CRUD 操作,并实现了一些必要的工具类。