All Products
Search
Document Center

Tablestore:Getting started

Last Updated:Mar 14, 2024

This topic describes how to use Tablestore HBase Client to develop a program that can access Tablestore.

Note

The sample program in this topic uses HBase API to access Tablestore. The complete sample program is located in the src/test/java/samples/HelloWorld.java directory of the hbase project at Github.

Procedure

  1. Configure project dependencies.

    The following sample code provides an example on how to configure dependencies for a Maven project:

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

    You can use HBase Client of other versions or Tablestore SDK for Java to configure project dependencies. For more information, see Migrate from HBase Client to Tablestore HBase Client.

  2. Modify the configuration file.

    Add the following configuration items to the hbase-site.xml configuration file:

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

    For more information, see Migrate from HBase Client to Tablestore HBase Client.

  3. Connect to Tablestore.

    The following sample code provides an example on how to create a TableStoreConnection object to connect to Tablestore:

    Configuration config = HBaseConfiguration.create();
    
    // Create a Tablestore Connection object. 
    Connection connection = ConnectionFactory.createConnection(config);
    
    // Admin is used to create, manage, and delete tables. 
    Admin admin = connection.getAdmin();                            
  4. Create a table.

    The following sample code provides an example on how to create a table that has a specific name. In this example, the default value of the MaxVersion and TimeToLive parameters are used.

    // Create an HTableDescriptor, which contains only one ColumnFamily. 
    HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(TABLE_NAME));
    
    // Create a ColumnFamily in which the default value of the MaxVersion and TimeToLive parameters are used. The default value of the MaxVersion parameter is 1. The default value of the TimeToLive parameter is Integer.INF_MAX. 
    descriptor.addFamily(new HColumnDescriptor(COLUMN_FAMILY_NAME));
    
    // Use the createTable operation of the Admin to create a table. 
    System.out.println("Create table " + descriptor.getNameAsString());
    admin.createTable(descriptor);                            
  5. Write data.

    The following sample code provides an example on how to write a row of data to Tablestore:

    // Create a Tablestore table to perform operations such as read, write, update, and deletion operations. 
    Table table = connection.getTable(TableName.valueOf(TABLE_NAME));
    
    // Create a PUT object to write a row whose primary key value is row_1. 
    System.out.println("Write one row to the table");
    Put put = new Put(ROW_KEY);
    
    // Add an attribute column of the row to Tablestore. Tablestore supports only single column families. You need to specify the family name in the hbase-site.xml configuration file. If you do not specify the family name in the hbase-site.xml configuration file, the default value f is used. Therefore, when you write data to Tablestore, the COLUMN_FAMILY_NAME parameter can be left empty. 
    
    put.addColumn(COLUMN_FAMILY_NAME, COLUMN_NAME, COLUMN_VALUE);
    
    // Use HBase API to call the PUT operation to write the row to Tablestore. 
    table.put(put);                            
  6. Read data.

    The following sample code provides an example on how to read the data of a row:

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

    Read data whose primary key values are within a specific range.

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

    The following sample code provides an example on how to use Admin API to delete a table:

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

Complete sample code

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