全部產品
Search
文件中心

Tablestore:Tablestore HBase Client快速入門

更新時間:Dec 30, 2025

本文介紹如何使用Tablestore HBase Client實現一個訪問Tablestore讀寫資料的簡單程式。

前提條件

操作步驟

說明

此處以適配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檔案中增加如下配置項,並按實際配置Tablestore執行個體的服務地址、Tablestore執行個體名稱和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>                       

步驟三:串連Tablestore

通過建立一個TableStoreConnection對象來串連Tablestore服務。

Configuration config = HBaseConfiguration.create();

// 建立一個Tablestore Connection。
Connection connection = ConnectionFactory.createConnection(config);

// Admin負責建立、管理、刪除等。
Admin admin = connection.getAdmin();                            

步驟四:表操作

建立表

通過指定表名建立一張表,MaxVersion和TimeToLive使用預設值。

// 建立一個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());                          

步驟五:基礎資料操作

寫資料

寫入一行資料到Tablestore

// 建立一個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);

// 增加一列,Table Store只支援單列族,列族名稱在hbase-site.xml中配置。如果未配置則預設為f,所以寫入資料時COLUMN_FAMILY_NAME可以為空白值。

put.addColumn(COLUMN_FAMILY_NAME, COLUMN_NAME, COLUMN_VALUE);

// 執行Table的put操作,使用HBase API將這一行資料寫入Table Store。
table.put(put);                            

讀資料

  • 讀取單行資料

    讀取指定行的資料。

    // 建立一個Get對象,讀取主鍵為ROW_KEY的行。
    Result getResult = table.get(new Get(ROW_KEY));
    
    
    // 列印結果。
    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訪問Tablestore服務,完整的樣本程式位於Github的HBase專案中,目錄位置為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  {
            // 串連Table Store。
            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 Store。
            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);
        }
    }
}            

相關文檔