OTSClient is a Tablestore client that provides various methods for you to manage tables and perform read and write operations on one or more rows. If you want to use the Wide Column model to manage data tables and perform read and write operations on one or more rows, you must initialize an OTSClient instance and modify the default settings of the configuration items in OTSClientConfig based on your business requirements. If you want to use the TimeSeries model to manage time series tables, query time series, and read and write time series data, you must initialize a TimeseriesClient instance.
Usage notes
If you want to access Tablestore resources over HTTPS, use Java 7.
Tablestore SDK for Java supports multithreading. We recommend that you use the same OTSClient object to run multiple threads of a task.
Preparations
Before you initialize an OTSClient instance, you must configure an AccessKey pair, obtain an endpoint, and install Tablestore SDK for Java.
Configure an AccessKey pair
Obtain an endpoint of a Tablestore instance
Install Tablestore SDK for Java
Initialize a client
If you want to use the Wide Column model by using Tablestore SDK for Java, you must create a client and call the operations in the client to access Tablestore. The operations in the client provide the same features as the RESTful API operations provided by Tablestore.
Tablestore SDK for Java provides the following clients: SyncClient and AsyncClient. SyncClient contains synchronous operations whereas AsyncClient contains asynchronous operations. If you call a synchronous operation, the operation directly returns a response, which indicates that the request is executed. You can call synchronous operations to get started with various features of Tablestore. Compared with synchronous operations, asynchronous operations are more flexible. Multithreading provides higher performance than asynchronous operations. You can select asynchronous operations or multithreading based on your business requirements.
SyncClient and AsyncClient are thread-safe. You can use the clients to manage internal threads and connection resources. We recommend that you create a global client. This way, you do not need to create a client for each thread or request.
The following examples show how to initiate a SyncClient after you obtain an AccessKey ID and an AccessKey secret.
The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. We recommend that you use the AccessKey pair of a RAM user to call API operations or perform routine O&M. We recommend that you do not hard-code the AccessKey ID and AccessKey secret into your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources in your account is compromised. In the following examples, the AccessKey pair is configured in the environment variables to verify your identity.
To ensure the security of your AccessKey pair, we recommend that you use the V4 signature algorithm to initialize client instances.
For more information about how to use the V4 signature algorithm, see AccessKey pair security.
Use the V4 signature algorithm
Tablestore SDK for Java V5.16.1 and later support the V4 signature algorithm. Before you use the V4 signature algorithm, make sure that the SDK that you use supports this algorithm.
String region = "";
String endPoint = "";
String accessKeyId = System.getenv("OTS_AK_ENV");
String accessKeySecret = System.getenv("OTS_SK_ENV");
String instanceName = "";
/**
* Construct V4 signature credentials.
*/
DefaultCredentials credentials = new DefaultCredentials(accessKeyId, accessKeySecret);
V4Credentials credentialsV4 = V4Credentials.createByServiceCredentials(credentials, region);
CredentialsProvider provider = new DefaultCredentialProvider(credentialsV4);
/**
* Use V4 signature credentials to construct the OTSClient instance.
*/
SyncClient client = new SyncClient(endPoint, provider, instanceName, null, new ResourceManager(null, null));
The following table describes the parameters.
Parameter | Example | Description |
region | cn-hangzhou | The ID of the region in which the instance resides. For more information, see Regions. |
endPoint | https://myinstance.cn-hangzhou.ots.aliyuncs.com | The endpoint that is used to access the Tablestore instance. For more information, see Obtain an endpoint of a Tablestore instance. |
accessKeyId | System.getenv("OTS_AK_ENV") | The AccessKey pair that is used to access the Tablestore instance. Obtain the AccessKey pair by using environment variables. Make sure that the relevant environment variables are configured. For more information, see Configure an AccessKey pair. |
accessKeySecret | System.getenv("OTS_SK_ENV") | |
instanceName | myinstance | The name of the Tablestore instance that you want to access. For more information, see Instances. |
Use methods other than the V4 signature algorithm
You can use the default or custom configurations when you create a SyncClient.
Use the default configurations to create a SyncClient
final String endPoint = ""; String accessKeyId = System.getenv("OTS_AK_ENV"); String accessKeySecret = System.getenv("OTS_SK_ENV"); final String instanceName = ""; SyncClient client = new SyncClient(endPoint, accessKeyId, accessKeySecret, instanceName);
The following table describes the parameters.
Parameter
Example
Description
endPoint
https://myinstance.cn-hangzhou.ots.aliyuncs.com
The endpoint that is used to access the Tablestore instance. For more information, see Obtain an endpoint of a Tablestore instance.
accessKeyId
System.getenv("OTS_AK_ENV")
The AccessKey pair that is used to access the Tablestore instance. Obtain the AccessKey pair by using environment variables.
Make sure that the relevant environment variables are configured. For more information, see Configure an AccessKey pair.
accessKeySecret
System.getenv("OTS_SK_ENV")
instanceName
myinstance
The name of the Tablestore instance that you want to access. For more information, see Instances.
Use custom configurations to create a SyncClient
// ClientConfiguration provides multiple configuration items. Only the most commonly used items are displayed. ClientConfiguration clientConfiguration = new ClientConfiguration(); // Specify a timeout period for establishing a connection. Unit: milliseconds. clientConfiguration.setConnectionTimeoutInMillisecond(5000); // Specify a timeout period for the socket connection. Unit: milliseconds. clientConfiguration.setSocketTimeoutInMillisecond(5000); // Configure a retry policy. If you do not configure a retry policy, the default retry policy is used. clientConfiguration.setRetryStrategy(new AlwaysRetryStrategy()); SyncClient client = new SyncClient(endPoint, accessKeyId, accessKeySecret, instanceName, clientConfiguration);
Initialize a TimeseriesClient instance
If you want to use the TimeSeries model by using Tablestore SDK for Java, you must create a TimeseriesClient instance and call the operations of the TimeseriesClient instance to access Tablestore. The TimeseriesClient instance must be separately initialized for the TimeSeries model.
Tablestore SDK for Java provides the TimeseriesClient and AsyncTimeseriesClient methods. You can use the TimeseriesClient method to perform synchronous operations and the AsyncTimeseriesClient method to perform asynchronous operations.
The following examples show how to initialize a TimeseriesClient instance after you obtain an AccessKey ID and an AccessKey secret.
Use the default configurations to create a TimeseriesClient instance
final String endPoint = "";
String accessKeyId = System.getenv("OTS_AK_ENV");
String accessKeySecret = System.getenv("OTS_SK_ENV");
final String instanceName = "";
TimeseriesClient client = new TimeseriesClient(endPoint, accessKeyId, accessKeySecret, instanceName);
The following table describes the parameters.
Parameter | Example | Description |
endPoint | https://myinstance.cn-hangzhou.ots.aliyuncs.com | The endpoint that is used to access the Tablestore instance. For more information, see Obtain an endpoint of a Tablestore instance. |
accessKeyId | System.getenv("OTS_AK_ENV") | The AccessKey pair that is used to access the Tablestore instance. Obtain the AccessKey pair by using environment variables. Make sure that the relevant environment variables are configured. For more information, see Configure an AccessKey pair. |
accessKeySecret | System.getenv("OTS_SK_ENV") | |
instanceName | myinstance | The name of the Tablestore instance that you want to access. For more information, see Instances. |
Use custom configurations to create a TimeseriesClient instance
// ClientConfiguration provides multiple configuration items. Only the most commonly used items are displayed.
ClientConfiguration clientConfiguration = new ClientConfiguration();
// Specify a timeout period for establishing a connection. Unit: milliseconds.
clientConfiguration.setConnectionTimeoutInMillisecond(5000);
// Specify a timeout period for the socket connection. Unit: milliseconds.
clientConfiguration.setSocketTimeoutInMillisecond(5000);
// Configure a retry policy. If you do not configure a retry policy, the default retry policy is used.
clientConfiguration.setRetryStrategy(new AlwaysRetryStrategy());
TimeseriesClient client = new TimeseriesClient(endPoint, accessKeyId, accessKeySecret, instanceName, clientConfiguration);