Tablestore HBase Client快速入门

本文介绍如何使用Tablestore HBase Client实现一个访问表格存储读写数据的简单程序。

前提条件

操作步骤

说明

此处以适配HBase 2.x.x版本的Tablestore HBase Client为例进行介绍。

步骤一:引入Tablestore HBase Client依赖

Maven工程的pom.xml文件中添加如下依赖:

<dependencies>
    <dependency>
        <groupId>com.aliyun.openservices</groupId>
        <artifactId>tablestore-hbase-client</artifactId>
        <version>2.0.12</version>
    </dependency>
</dependencies>                            

步骤二:配置HBase文件

hbase-site.xml文件中增加如下配置项,并按实际配置表格存储实例的服务地址、表格存储实例名称和RAM用户的AccessKey。

<configuration>
    <property>
        <name>hbase.client.connection.impl</name>
        <value>com.alicloud.tablestore.hbase.TablestoreConnection</value>
    </property>
    <property>
        <name>tablestore.client.endpoint</name>
        <value>endpoint</value>
    </property>
    <property>
        <name>tablestore.client.instancename</name>
        <value>instance_name</value>
    </property>
    <property>
        <name>tablestore.client.accesskeyid</name>
        <value>access_key_id</value>
    </property>
    <property>
        <name>tablestore.client.accesskeysecret</name>
        <value>access_key_secret</value>
    </property>
    <property>
        <name>hbase.client.tablestore.family</name>
        <value>f1</value>
    </property>
    <property>
        <name>hbase.client.tablestore.table</name>
        <value>ots_adaptor</value>
    </property>
</configuration>                       

步骤三:连接表格存储

通过创建一个TableStoreConnection对象来连接表格存储服务。

Configuration config = HBaseConfiguration.create();

// 创建一个Tablestore Connection。
Connection connection = ConnectionFactory.createConnection(config);

// Admin负责创建、管理、删除等。
Admin admin = connection.getAdmin();                            

步骤四:表操作

创建表

通过指定表名创建一张表,MaxVersionTimeToLive使用默认值。

// 创建一个HTableDescriptor,只有一个列族。
HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(TABLE_NAME));

// 创建一个列族,MaxVersion和TimeToLive使用默认值,MaxVersion默认值为1,TimeToLive默认值为Integer.INF_MAX。
descriptor.addFamily(new HColumnDescriptor(COLUMN_FAMILY_NAME));

// 通过Admin的createTable接口创建表。
System.out.println("Create table " + descriptor.getNameAsString());
admin.createTable(descriptor);                            

删除表

使用Admin API删除一张表。

System.out.println("Delete the table");
admin.disableTable(table.getName());
admin.deleteTable(table.getName());                          

步骤五:基础数据操作

写数据

写入一行数据到表格存储

// 创建一个TablestoreTable,用于单个表上的读写更新删除等操作。
Table table = connection.getTable(TableName.valueOf(TABLE_NAME));

// 创建一个Put对象,主键为row_1。
System.out.println("Write one row to the table");
Put put = new Put(ROW_KEY);

// 增加一列,表格存储只支持单列族,列族名称在hbase-site.xml中配置。如果未配置则默认为f,所以写入数据时COLUMN_FAMILY_NAME可以为空值。

put.addColumn(COLUMN_FAMILY_NAME, COLUMN_NAME, COLUMN_VALUE);

// 执行Table的put操作,使用HBase API将这一行数据写入表格存储。
table.put(put);                            

读数据

  • 读取单行数据

    读取指定行的数据。

    // 创建一个Get对象,读取主键为ROW_KEY的行。
    Result getResult = table.get(new Get(ROW_KEY));
    Result result = table.get(get);
    
    // 打印结果。
    String value = Bytes.toString(getResult.getValue(COLUMN_FAMILY_NAME, COLUMN_NAME));
    System.out.println("Get one row by row key");
    System.out.printf("\t%s = %s\n", Bytes.toString(ROW_KEY), value);                            
  • 扫描数据

    范围读取数据。

    // 扫描全表所有行数据.
    System.out.println("Scan for all rows:");
    Scan scan = new Scan();
    ResultScanner scanner = table.getScanner(scan);
    
    // 循环打印结果.
    for (Result row : scanner) {
        byte[] valueBytes = row.getValue(COLUMN_FAMILY_NAME, COLUMN_NAME);
        System.out.println('\t' + Bytes.toString(valueBytes));
    }                            

完整示例代码

说明

当前示例程序使用了HBase API访问表格存储服务,完整的示例程序位于GithubHBase项目中,目录位置为src/test/java/samples/HelloWorld.java

package samples;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
public class HelloWorld {
    /** 数据表名称。**/
    private static final byte[] TABLE_NAME = Bytes.toBytes("HelloTablestore");
    /** 行主键。**/
    private static final byte[] ROW_KEY = Bytes.toBytes("row_1");
    /** 列族。**/
    private static final byte[] COLUMN_FAMILY_NAME = Bytes.toBytes("f");
    /** 列名。**/
    private static final byte[] COLUMN_NAME = Bytes.toBytes("col_1");
     /** 列值。**/
    private static final byte[] COLUMN_VALUE = Bytes.toBytes("col_value");
    public static void main(String[] args) {
        helloWorld();
    }
    private static void helloWorld() {
        try  {
            // 连接表格存储。
            Configuration config = HBaseConfiguration.create();
            Connection connection = ConnectionFactory.createConnection(config);
            Admin admin = connection.getAdmin();
            
            // 创建数据表。
            HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(TABLE_NAME));
            descriptor.addFamily(new HColumnDescriptor(COLUMN_FAMILY_NAME));
            System.out.println("Create table " + descriptor.getNameAsString());
            admin.createTable(descriptor);
            
            // 写入数据到表格存储。
            Table table = connection.getTable(TableName.valueOf(TABLE_NAME));
            System.out.println("Write one row to the table");
            Put put = new Put(ROW_KEY);
            put.addColumn(COLUMN_FAMILY_NAME, COLUMN_NAME, COLUMN_VALUE);
            table.put(put);
            Result getResult = table.get(new Get(ROW_KEY));
            
            // 读取一行数据。
            String value = Bytes.toString(getResult.getValue(COLUMN_FAMILY_NAME, COLUMN_NAME));
            System.out.println("Get a one row by row key");
            System.out.printf("\t%s = %s\n", Bytes.toString(ROW_KEY), value);
            
            // 扫描数据。
            Scan scan = new Scan();
            System.out.println("Scan for all rows:");
            ResultScanner scanner = table.getScanner(scan);
            for (Result row : scanner) {
                byte[] valueBytes = row.getValue(COLUMN_FAMILY_NAME, COLUMN_NAME);
                System.out.println('\t' + Bytes.toString(valueBytes));
            }
            
            // 删除数据表。
            System.out.println("Delete the table");
            admin.disableTable(table.getName());
            admin.deleteTable(table.getName());
            
            // 关闭连接。
            table.close();
            admin.close();
            connection.close();
        } catch (IOException e) {
            System.err.println("Exception while running HelloTablestore: " + e.toString());
            System.exit(1);
        }
    }
}            

相关文档