The Tablestore client provides methods for users to manage tables and perform read and write operations on a single row or multiple rows. To use Java SDK to initiate a request, you must initialize an OTSClient instance and modify the default configuration items of ClientConfiguration as needed.

Obtain an endpoint

An endpoint is the domain name used to access a Tablestore instance in a region. The following table lists the examples of endpoints.

Example Description
http://sun.cn-hangzhou.ots.aliyuncs.com The public endpoint used to access the sun instance in the China (Hangzhou) region over HTTP.
https://sun.cn-hangzhou.ots.aliyuncs.com The public endpoint used to access the sun instance in the China (Hangzhou) region over HTTPS.
Notice Tablestore supports access over the Internet and internal network. For more information, see Endpoint.

To query the endpoints of your Tablestore instance, perform the following steps:

  1. Log on to the Tablestore console.
  2. Go to the Instance Details tab. You can view the endpoints in the Instance Access URL section.

Configure an AccessKey pair

To access Tablestore, you must have an AccessKey pair that consists of an AccessKey ID and AccessKey secret to verify your identity. The following types of AccessKeys are supported:

The AccessKey pair of an Alibaba Cloud account. To obtain the AccessKey pair of an Alibaba Cloud account, take the following steps:

  1. Create an Alibaba Cloud account on the Alibaba Cloud official website.
  2. Log on to the AccessKey Console. Apply for an AccessKey pair.

The AccessKey pair of a RAM user. To obtain the AccessKey pair of a RAM user, take the following steps:

  1. Use your Alibaba Cloud account to log on to RAM. Create a RAM user or use an existing RAM user.
  2. Use your Alibaba Cloud account to authorize the RAM user to access Tablestore.
  3. After authorization, the AccessKey ID and AccessKey secret of the RAM user can be used to access Tablestore.

The AccessKey pair in temporary access credentials. The process of obtaining temporary access credentials is as follows:

  1. The application server uses RAM or STS to obtain access credentials that consist of the temporary AccessKey ID, AccessKey secret, and token. After the access credentials are obtained, the application server sends them to you.
  2. You can use the received access credentials to access Tablestore.

Initialize an OTSClient instance

After you obtain the AccessKey pair, you can perform the following operations to initialize an OTSClient instance:

Create a client

When you use Tablestore SDKs, you must create a client and then call the client API to access Tablestore. The client API provides the same functions as the RESTful API provided by Tablestore.

Tablestore SDKs offers two client types: SyncClient and AsyncClient. SyncClient is designed for synchronous operations and AsyncClient is for asynchronous operations. After a synchronous operation is called, the service request succeeds. You can call synchronous operations to learn more about the various functions of Tablestore. Compared with synchronous operations, asynchronous operations provide greater flexibility. If you have high requirements for performance, you can choose between the use of asynchronous operations and multithreading as needed.

Note Both SyncClient and AsyncClient are thread-safe and can internally manage threads and connection resources. You do not need to create a client for each thread or request. Instead, create a global client.

Examples

  • Use the default configurations to create a SyncClient.
            final String endPoint = "";
            final String accessKeyId = "";
            final String accessKeySecret = "";
            final String instanceName = "";
    
            SyncClient client = new SyncClient(endPoint, accessKeyId, accessKeySecret, instanceName);                    
  • Use the custom configurations to create a SyncClient.
    // ClientConfiguration provides multiple configuration items. Only the most commonly used items are listed.
            ClientConfiguration clientConfiguration = new ClientConfiguration();
            // Configure the timeout period for establishing a connection.
            clientConfiguration.setConnectionTimeoutInMillisecond(5000);
            // Configure the timeout period for the socket connection.
            clientConfiguration.setSocketTimeoutInMillisecond(5000);
            // Configure a retry policy. If you do not configure any policy, the default retry policy is used.
            clientConfiguration.setRetryStrategy(new AlwaysRetryStrategy());
            SyncClient client = new SyncClient(endPoint, accessId, accessKey,
                    instanceName, clientConfiguration);

HTTPS

Upgrade to Java 7 for HTTPS access.

Multithreading

  • Multithreading is supported.
  • We recommend that you use the same OTSClient object to perform multithreading tasks.