首页 Tablestore User Guide Compute-Analysis HBase support Quick Start for Tablestore HBase Client

Quick Start for Tablestore HBase Client

更新时间: 2026-06-03 18:33:50

Use the Tablestore HBase Client to build a program that reads and writes data in Tablestore.

Prerequisites

Procedure

Note

The examples use the Tablestore HBase Client for HBase 2.x.x.

Step 1: Import the Tablestore HBase Client dependency

Add this dependency to your Maven project pom.xml:

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

Step 2: Configure the HBase file

Add these items to hbase-site.xml. Replace the endpoint of the Tablestore instance, the Tablestore instance name, and RAM user AccessKey with your actual values.

<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>                       

Step 3: Connect to Tablestore

Create a TableStoreConnection object to connect to Tablestore.

Configuration config = HBaseConfiguration.create();

// Create a Tablestore connection.
Connection connection = ConnectionFactory.createConnection(config);

// The Admin object is used to create, manage, and delete tables.
Admin admin = connection.getAdmin();                            

Step 4: Perform table operations

Create a table

Specify a table name. MaxVersion and TimeToLive use default values.

// Create an HTableDescriptor that has only one column family.
HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(TABLE_NAME));

// Create a column family. The default values are used for MaxVersion and TimeToLive. The default value of MaxVersion is 1. The default value of TimeToLive is Integer.MAX_VALUE.
descriptor.addFamily(new HColumnDescriptor(COLUMN_FAMILY_NAME));

// Call the createTable operation of the Admin object to create the table.
System.out.println("Create table " + descriptor.getNameAsString());
admin.createTable(descriptor);                            

Delete a table

Delete a table with the Admin API.

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

Step 5: Perform basic data operations

Write data

Write a row of data to Tablestore.

// Create a TablestoreTable object to read, write, update, and delete data in a single table.
Table table = connection.getTable(TableName.valueOf(TABLE_NAME));

// Create a Put object. The primary key is row_1.
System.out.println("Write one row to the table");
Put put = new Put(ROW_KEY);

// Add a column. Tablestore supports only one column family. The column family name is configured in the hbase-site.xml file. If you do not configure this parameter, the default value f is used. Therefore, you can leave COLUMN_FAMILY_NAME empty when you write data.

put.addColumn(COLUMN_FAMILY_NAME, COLUMN_NAME, COLUMN_VALUE);

// Execute the put operation of the Table object. Use the HBase API to write the row of data to Tablestore.
table.put(put);                            

Read data

  • Read a single row of data

    Read data from a specified row.

    // Create a Get object to read the row whose primary key is ROW_KEY.
    Result getResult = table.get(new Get(ROW_KEY));
    
    
    // Print the result.
    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);                            
  • Scan data

    Read data from a range of rows.

    // Scan all rows in the table.
    System.out.println("Scan for all rows:");
    Scan scan = new Scan();
    ResultScanner scanner = table.getScanner(scan);
    
    // Print the results in a loop.
    for (Result row : scanner) {
        byte[] valueBytes = row.getValue(COLUMN_FAMILY_NAME, COLUMN_NAME);
        System.out.println('\t' + Bytes.toString(valueBytes));
    }                            

Complete sample code

Note

This program uses the HBase API to access Tablestore. The source code is in the HBase project on GitHub at 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 {
    /** The name of the data table. **/
    private static final byte[] TABLE_NAME = Bytes.toBytes("HelloTablestore");
    /** The primary key of the row. **/
    private static final byte[] ROW_KEY = Bytes.toBytes("row_1");
    /** The column family. **/
    private static final byte[] COLUMN_FAMILY_NAME = Bytes.toBytes("f");
    /** The column name. **/
    private static final byte[] COLUMN_NAME = Bytes.toBytes("col_1");
     /** The column value. **/
    private static final byte[] COLUMN_VALUE = Bytes.toBytes("col_value");
    public static void main(String[] args) {
        helloWorld();
    }
    private static void helloWorld() {
        try  {
            // Connect to Tablestore.
            Configuration config = HBaseConfiguration.create();
            Connection connection = ConnectionFactory.createConnection(config);
            Admin admin = connection.getAdmin();
            
            // Create a data table.
            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);
            
            // Write data to Tablestore.
            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));
            
            // Read a row of data.
            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 data.
            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));
            }
            
            // Delete the data table.
            System.out.println("Delete the table");
            admin.disableTable(table.getName());
            admin.deleteTable(table.getName());
            
            // Close the connection.
            table.close();
            admin.close();
            connection.close();
        } catch (IOException e) {
            System.err.println("Exception while running HelloTablestore: " + e.toString());
            System.exit(1);
        }
    }
}            

References

上一篇: Overview of Tablestore HBase Client 下一篇: Features supported by Tablestore HBase Client
阿里云首页 表格存储 相关技术圈