本文描述如何使用Tablestore HBase Client實現一個簡單的程式。

说明 當前樣本程式使用了HBase API訪問Tablestore服務,完整的樣本程式位於Github的 hbase專案中,目錄位置為src/test/java/samples/HelloWorld.java

操作步驟

  1. 設定項目依賴。
    Maven的依賴配置如下:
    <dependencies>
        <dependency>
            <groupId>com.aliyun.openservices</groupId>
            <artifactId>tablestore-hbase-client</artifactId>
            <version>1.2.0</version>
        </dependency>
    </dependencies>                            

    關於設定項目依賴的更多資訊,請參見從HBase遷移到Tablestore

  2. 設定檔。
    hbase-site.xml中增加以下配置項:
    <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>                            

    關於配置的更多資訊,請參見從HBase遷移到Tablestore

  3. 串連Tablestore。
    通過建立一個TableStoreConnection對象來連結資料表格儲存服務。
    Configuration config = HBaseConfiguration.create();
    
    // 建立一個Tablestore Connection。
    Connection connection = ConnectionFactory.createConnection(config);
    
    // Admin負責建立、管理、刪除等。
    Admin admin = connection.getAdmin();                            
  4. 建立表。
    通過指定表名建立一張表,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);                            
  5. 寫資料。
    寫入一行資料到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);
    
    // 增加一列,Tablestore只支援單列族,列族名稱在hbase-site.xml中配置。如果未配置則預設為f,所以寫入資料時COLUMN_FAMILY_NAME可以為空白值。
    
    put.addColumn(COLUMN_FAMILY_NAME, COLUMN_NAME, COLUMN_VALUE);
    
    // 執行Table的put操作,使用HBase API將這一行資料寫入Tablestore。
    table.put(put);                            
  6. 讀資料。
    讀取指定行的資料。
    // 建立一個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);                            
  7. 掃描資料。
    範圍讀取資料。
    // 掃描全表所有行資料.
    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));
    }                            
  8. 刪表。
    使用Admin API刪除一張表。
    System.out.println("Delete the table");
    admin.disableTable(table.getName());
    admin.deleteTable(table.getName());                          

完整代碼

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);
        }
    }
}