All Products
Search
Document Center

Tablestore:Initialize an OTSClient instance

Last Updated:Sep 26, 2024

OTSClient is a Tablestore client that provides various methods you can use to manage tables and perform read and write operations on a single row or multiple 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 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 obtain an endpoint of a Tablestore instance, install Tablestore SDK for Java, and configure access credentials.

Obtain an endpoint of a Tablestore instance

After you create a Tablestore instance, you must obtain an endpoint of the instance. Then, you can use the endpoint to access the instance.

An endpoint is a domain name that is used to access a Tablestore instance in a region. For example, https://sun.cn-hangzhou.ots.aliyuncs.com is the public endpoint that is used to access the instance named sun in the China (Hangzhou) region over HTTPS. For more information, see Endpoints.

Important

You can access Tablestore by using public and internal endpoints.

  1. Activate the Tablestore service if it is not activated. For more information, see Activate Tablestore.

  2. Create a Tablestore instance. For more information, see Create an instance.

  3. Obtain an endpoint of the created instance.

    1. Log on to the Tablestore console.

    2. On the Overview page, find the instance that you created and click the name of the instance.

    3. In the Instance Access URL section of the Instance Details tab, view the endpoints of the instance.

      image

Install Tablestore SDK for Java

For more information, see Install Tablestore SDK for Java.

Configure access credentials

To access Tablestore, you must have a valid AccessKey pair to verify your identity. For more information, see Configure access credentials.

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 of 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 use 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.

Note

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.

Examples

Warning

The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. We recommend that you use 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 within your account is compromised. In the examples, the security token and AccessKey pair are saved to environment variables.

(Recommended) Use the V4 signature algorithm

To ensure the security of your AccessKey pair, we recommend that you use the V4 signature algorithm when you initialize a client. For more information about how to use the V4 signature algorithm, see AccessKey pair security.

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.

Use an AccessKey pair

Note

Before you run the sample code, make sure that the TABLESTORE_ACCESS_KEY_ID and TABLESTORE_ACCESS_KEY_SECRET environment variables are configured. For more information, see Configure access credentials.

final String region = "yourRegion";
final String endpoint = "yourEndpoint";
final String instanceName = "yourInstance";
final String accessKeyId = System.getenv("TABLESTORE_ACCESS_KEY_ID");
final String accessKeySecret = System.getenv("TABLESTORE_ACCESS_KEY_SECRET");
final String v4SigningKey = "";
final String v4StsSigningKey = "";

{
    /**
     *  Example 1 in which the V4 signature algorithm is used: Use the original AccessKey ID and AccessKey secret to construct {@link DefaultCredentials } and then generate {@link V4Credentials }.
     */
    DefaultCredentials credentials = new DefaultCredentials(accessKeyId, accessKeySecret);
    V4Credentials credentialsV4 = V4Credentials.createByServiceCredentials(credentials, region);
    CredentialsProvider provider = new DefaultCredentialProvider(credentialsV4);

    /**
     * using {@link V4Credentials } initialize tableStore client
     */
    SyncClient client = new SyncClient(endpoint, provider, instanceName, null, new ResourceManager(null, null));
    // ClientConfiguration provides multiple configuration items. The following example includes some custom configuration items. 
    // ClientConfiguration clientConfiguration = new ClientConfiguration();
    // clientConfiguration.setConnectionTimeoutInMillisecond(5000); // Specify the timeout period for establishing a connection. Unit: milliseconds. 
    // clientConfiguration.setSocketTimeoutInMillisecond(5000); // Specify the socket timeout period. Unit: milliseconds. 
    // clientConfiguration.setRetryStrategy(new AlwaysRetryStrategy()); // Configure a retry policy. If you do not configure a retry policy, the default retry policy is used. 
    // SyncClient client = new SyncClient(endpoint, provider, instanceName, clientConfiguration, new ResourceManager(null, null));

    // do something
    
    // shutdown tableStore client
    client.shutdown();
}

{
    /**
     * Example 2 in which the V4 signature algorithm is used: Use the AccessKey ID and derived key to construct {@link V4Credentials }.
     */
    Date date = new Date();
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
    String signDate = dateFormat.format(date);      // Sample value of the signDate parameter: 20230527
    V4Credentials credentialsV4 = new V4Credentials(accessKeyId, v4SigningKey, v4StsSigningKey, region, signDate);
    CredentialsProvider provider = new DefaultCredentialProvider(credentialsV4);

    /**
     * using {@link V4Credentials } initialize tableStore client
     */
    SyncClient client = new SyncClient(endpoint, provider, instanceName, null, new ResourceManager(null, null));

    // do something
    
    // shutdown tableStore client
    client.shutdown();
}

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.

instanceName

myinstance

The name of the instance. For more information, see Instances.

accessKeyId

System.getenv("TABLESTORE_ACCESS_KEY_ID")

The AccessKey pair that is obtained from environment variables. Make sure that the environment variables are configured.

accessKeySecret

System.getenv("TABLESTORE_ACCESS_KEY_SECRET")

Use STS

Note

Before you run the sample code, make sure that the TABLESTORE_ACCESS_KEY_ID, TABLESTORE_ACCESS_KEY_SECRET, and TABLESTORE_SESSION_TOKEN environment variables are configured. For more information, see Configure access credentials.

final String region = "yourRegion";
final String endpoint = "yourEndpoint";
final String instanceName = "yourInstance";
final String accessKeyId = System.getenv("TABLESTORE_ACCESS_KEY_ID");
final String accessKeySecret = System.getenv("TABLESTORE_ACCESS_KEY_SECRET");
final String secretAccessKey = System.getenv("TABLESTORE_SESSION_TOKEN");

{
    /**
     *  Example 1 in which the V4 signature algorithm is used: Use the original AccessKey ID and AccessKey secret to construct {@link DefaultCredentials } and then generate {@link V4Credentials }.
     */
    DefaultCredentials credentials = new DefaultCredentials(accessKeyId, accessKeySecret, secretAccessKey);
    V4Credentials credentialsV4 = V4Credentials.createByServiceCredentials(credentials, region);
    CredentialsProvider provider = new DefaultCredentialProvider(credentialsV4);

    /**
     * using {@link V4Credentials } initialize tableStore client
     */
    SyncClient client = new SyncClient(endpoint, provider, instanceName, null, new ResourceManager(null, null));
    // ClientConfiguration provides multiple configuration items. The following example includes some custom configuration items. 
    // ClientConfiguration clientConfiguration = new ClientConfiguration();
    // clientConfiguration.setConnectionTimeoutInMillisecond(5000); // Specify the timeout period for establishing a connection. Unit: milliseconds. 
    // clientConfiguration.setSocketTimeoutInMillisecond(5000); // Specify the socket timeout period. Unit: milliseconds. 
    // clientConfiguration.setRetryStrategy(new AlwaysRetryStrategy()); // Configure a retry policy. If you do not configure a retry policy, the default retry policy is used. 
    // SyncClient client = new SyncClient(endpoint, provider, instanceName, clientConfiguration, new ResourceManager(null, null));

    // do something

    // shutdown tableStore client
    client.shutdown();
}

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.

instanceName

myinstance

The name of the instance. For more information, see Instances.

accessKeyId

System.getenv("TABLESTORE_ACCESS_KEY_ID")

The AccessKey pair and security token that are obtained from environment variables. Make sure that the environment variables are configured.

accessKeySecret

System.getenv("TABLESTORE_ACCESS_KEY_SECRET")

securityToken

System.getenv("TABLESTORE_SESSION_TOKEN")

V2 signature algorithm

Use an AccessKey pair

Note

Before you run the sample code, make sure that the TABLESTORE_ACCESS_KEY_ID and TABLESTORE_ACCESS_KEY_SECRET environment variables are configured. For more information, see Configure access credentials.

final String endpoint = "yourEndpoint";
final String instanceName = "yourInstance";
final String accessKeyId = System.getenv("TABLESTORE_ACCESS_KEY_ID");
final String accessKeySecret = System.getenv("TABLESTORE_ACCESS_KEY_SECRET");

{
    /**
     * Example 1 in which the V2 signature algorithm (original authentication method) is used: Use the original AccessKey ID and AccessKey secret to construct {@link DefaultCredentials }.
     */
    DefaultCredentials credentials = new DefaultCredentials(accessKeyId, accessKeySecret);
    CredentialsProvider provider = new DefaultCredentialProvider(credentials);

    /**
     * using {@link DefaultCredentials } initialize tableStore client
     */
    SyncClient client = new SyncClient(endpoint, provider, instanceName, null, new ResourceManager(null, null));
    // ClientConfiguration provides multiple configuration items. The following example includes some custom configuration items. 
    // ClientConfiguration clientConfiguration = new ClientConfiguration();
    // clientConfiguration.setConnectionTimeoutInMillisecond(5000); // Specify the timeout period for establishing a connection. Unit: milliseconds. 
    // clientConfiguration.setSocketTimeoutInMillisecond(5000); // Specify the socket timeout period. Unit: milliseconds. 
    // clientConfiguration.setRetryStrategy(new AlwaysRetryStrategy()); // Configure a retry policy. If you do not configure a retry policy, the default retry policy is used. 
    // SyncClient client = new SyncClient(endpoint, provider, instanceName, clientConfiguration, new ResourceManager(null, null));
    // shutdown tableStore client

    // do something

    client.shutdown();
}

{
    /**
     * Example 2 in which the V2 signature algorithm (original authentication method) is used: Use the credential provider {@link EnvironmentVariableCredentialsProvider } to read the environment variables.
     */
    EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();

    /**
     * using {@link DefaultCredentials } initialize tableStore client
     */
    SyncClient client = new SyncClient(endpoint, credentialsProvider, instanceName, null, new ResourceManager(null, null));

    // do something

    // shutdown tableStore client
    client.shutdown();
}

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.

instanceName

myinstance

The name of the instance. For more information, see Instances.

accessKeyId

System.getenv("TABLESTORE_ACCESS_KEY_ID")

The AccessKey pair that is obtained from environment variables. Make sure that the environment variables are configured.

accessKeySecret

System.getenv("TABLESTORE_ACCESS_KEY_SECRET")

Use STS

Note

Before you run the sample code, make sure that the TABLESTORE_ACCESS_KEY_ID, TABLESTORE_ACCESS_KEY_SECRET, and TABLESTORE_SESSION_TOKEN environment variables are configured. For more information, see Configure access credentials.

final String endpoint = "yourEndpoint";
final String instanceName = "yourInstance";
final String accessKeyId = System.getenv("TABLESTORE_ACCESS_KEY_ID");
final String accessKeySecret = System.getenv("TABLESTORE_ACCESS_KEY_SECRET");
final String securityToken = System.getenv("TABLESTORE_SESSION_TOKEN");

{
    /**
     * Example 1 in which the V2 signature algorithm (original authentication method) is used: Use the original AccessKey ID, AccessKey secret, and security token to construct {@link DefaultCredentials }.
     */
    DefaultCredentials credentials = new DefaultCredentials(accessKeyId, accessKeySecret, securityToken);
    CredentialsProvider provider = new DefaultCredentialProvider(credentials);

    /**
     * using {@link DefaultCredentials } initialize tableStore client
     */
    SyncClient client = new SyncClient(endpoint, provider, instanceName, null, new ResourceManager(null, null));
    // ClientConfiguration provides multiple configuration items. The following example includes some custom configuration items. 
    // ClientConfiguration clientConfiguration = new ClientConfiguration();
    // clientConfiguration.setConnectionTimeoutInMillisecond(5000); // Specify the timeout period for establishing a connection. Unit: milliseconds. 
    // clientConfiguration.setSocketTimeoutInMillisecond(5000); // Specify the socket timeout period. Unit: milliseconds. 
    // clientConfiguration.setRetryStrategy(new AlwaysRetryStrategy()); // Configure a retry policy. If you do not configure a retry policy, the default retry policy is used. 
    // SyncClient client = new SyncClient(endpoint, provider, instanceName, clientConfiguration, new ResourceManager(null, null));

    // do something

    // shutdown tableStore client
    client.shutdown();
}

{
    /**
     * Example 2 in which the V2 signature algorithm (original authentication method) is used: Use the credential provider {@link EnvironmentVariableCredentialsProvider } to read the environment variables.
     */
    EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();

    /**
     * using {@link DefaultCredentials } initialize tableStore client
     */
    SyncClient client = new SyncClient(endpoint, credentialsProvider, instanceName, null, new ResourceManager(null, null));

    // do something

    // shutdown tableStore client
    client.shutdown();
}

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.

instanceName

myinstance

The name of the instance. For more information, see Instances.

accessKeyId

System.getenv("TABLESTORE_ACCESS_KEY_ID")

The AccessKey pair and security token that are obtained from environment variables. Make sure that the environment variables are configured.

accessKeySecret

System.getenv("TABLESTORE_ACCESS_KEY_SECRET")

securityToken

System.getenv("TABLESTORE_SESSION_TOKEN")

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.

Examples

Warning

The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. We recommend that you use 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 within your account is compromised. In the examples, the security token and AccessKey pair are saved to environment variables.

(Recommended) Use the V4 signature algorithm

To ensure the security of your AccessKey pair, we recommend that you use the V4 signature algorithm when you initialize a client. For more information about how to use the V4 signature algorithm, see AccessKey pair security.

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.

Use an AccessKey pair

Note

Before you run the sample code, make sure that the TABLESTORE_ACCESS_KEY_ID and TABLESTORE_ACCESS_KEY_SECRET environment variables are configured. For more information, see Configure access credentials.

 final String region = "yourRegion";
 final String endpoint = "yourEndpoint";
 final String instanceName = "yourInstance";
 final String accessKeyId = System.getenv("TABLESTORE_ACCESS_KEY_ID");
 final String accessKeySecret = System.getenv("TABLESTORE_ACCESS_KEY_SECRET");

 /**
  *  Example in which the V4 signature algorithm is used: Use the original AccessKey ID and AccessKey secret to construct {@link DefaultCredentials } and then generate {@link V4Credentials }.
  */
 DefaultCredentials credentials = new DefaultCredentials(accessKeyId, accessKeySecret);
 V4Credentials credentialsV4 = V4Credentials.createByServiceCredentials(credentials, region);
 CredentialsProvider provider = new DefaultCredentialProvider(credentialsV4);

 /**
  * using {@link V4Credentials } initialize tableStore client
  */
 TimeseriesClient client = new TimeseriesClient(endpoint, provider, instanceName, null, new ResourceManager(null, null));
 // ClientConfiguration provides multiple configuration items. The following example includes some custom configuration items. 
 // ClientConfiguration clientConfiguration = new ClientConfiguration();
 // clientConfiguration.setConnectionTimeoutInMillisecond(5000); // Specify the timeout period for establishing a connection. Unit: milliseconds. 
 // clientConfiguration.setSocketTimeoutInMillisecond(5000); // Specify the socket timeout period. Unit: milliseconds. 
 // clientConfiguration.setRetryStrategy(new AlwaysRetryStrategy()); // Configure a retry policy. If you do not configure a retry policy, the default retry policy is used. 
 // TimeseriesClient client = new TimeseriesClient(endpoint, provider, instanceName, clientConfiguration, new ResourceManager(null, null));

 // do something
 
 // shutdown tableStore client
 client.shutdown();

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.

instanceName

myinstance

The name of the instance. For more information, see Instances.

accessKeyId

System.getenv("TABLESTORE_ACCESS_KEY_ID")

The AccessKey pair that is obtained from environment variables. Make sure that the environment variables are configured.

accessKeySecret

System.getenv("TABLESTORE_ACCESS_KEY_SECRET")

Use STS

Note

Before you run the sample code, make sure that the TABLESTORE_ACCESS_KEY_ID, TABLESTORE_ACCESS_KEY_SECRET, and TABLESTORE_SESSION_TOKEN environment variables are configured. For more information, see Configure access credentials.

final String region = "yourRegion";
final String endpoint = "yourEndpoint";
final String instanceName = "yourInstance";
final String accessKeyId = System.getenv("TABLESTORE_ACCESS_KEY_ID");
final String accessKeySecret = System.getenv("TABLESTORE_ACCESS_KEY_SECRET");
final String securityToken = System.getenv("TABLESTORE_SESSION_TOKEN");

/**
 *  Example in which the V4 signature algorithm is used: Use the original AccessKey ID and AccessKey secret to construct {@link DefaultCredentials } and then generate {@link V4Credentials }.
 */
DefaultCredentials credentials = new DefaultCredentials(accessKeyId, accessKeySecret, securityToken);
V4Credentials credentialsV4 = V4Credentials.createByServiceCredentials(credentials, region);
CredentialsProvider provider = new DefaultCredentialProvider(credentialsV4);

/**
 * using {@link V4Credentials } initialize tableStore client
 */
TimeseriesClient client = new TimeseriesClient(endpoint, provider, instanceName, null, new ResourceManager(null, null));
// ClientConfiguration provides multiple configuration items. The following example includes some custom configuration items. 
// ClientConfiguration clientConfiguration = new ClientConfiguration();
// clientConfiguration.setConnectionTimeoutInMillisecond(5000); // Specify the timeout period for establishing a connection. Unit: milliseconds. 
// clientConfiguration.setSocketTimeoutInMillisecond(5000); // Specify the socket timeout period. Unit: milliseconds. 
// clientConfiguration.setRetryStrategy(new AlwaysRetryStrategy()); // Configure a retry policy. If you do not configure a retry policy, the default retry policy is used. 
// TimeseriesClient client = new TimeseriesClient(endpoint, provider, instanceName, clientConfiguration, new ResourceManager(null, null));

// do something

// shutdown tableStore client
client.shutdown();

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.

instanceName

myinstance

The name of the instance. For more information, see Instances.

accessKeyId

System.getenv("TABLESTORE_ACCESS_KEY_ID")

The AccessKey pair and security token that are obtained from environment variables. Make sure that the environment variables are configured.

accessKeySecret

System.getenv("TABLESTORE_ACCESS_KEY_SECRET")

securityToken

System.getenv("TABLESTORE_SESSION_TOKEN")

V2 signature algorithm

Use an AccessKey pair

Note

Before you run the sample code, make sure that the TABLESTORE_ACCESS_KEY_ID and TABLESTORE_ACCESS_KEY_SECRET environment variables are configured. For more information, see Configure access credentials.

final String endpoint = "yourEndpoint";
final String instanceName = "yourInstance";
final String accessKeyId = System.getenv("TABLESTORE_ACCESS_KEY_ID");
final String accessKeySecret = System.getenv("TABLESTORE_ACCESS_KEY_SECRET");

{
    /**
     * Example 1 in which the V2 signature algorithm (original authentication method) is used: Use the original AccessKey ID and AccessKey secret to construct {@link DefaultCredentials }.
     */
    DefaultCredentials credentials = new DefaultCredentials(accessKeyId, accessKeySecret);
    CredentialsProvider provider = new DefaultCredentialProvider(credentials);

    /**
     * using {@link DefaultCredentials } initialize tableStore client
     */
    TimeseriesClient client = new TimeseriesClient(endpoint, provider, instanceName, null, new ResourceManager(null, null));
    // ClientConfiguration provides multiple configuration items. The following example includes some custom configuration items. 
    // ClientConfiguration clientConfiguration = new ClientConfiguration();
    // clientConfiguration.setConnectionTimeoutInMillisecond(5000); // Specify the timeout period for establishing a connection. Unit: milliseconds. 
    // clientConfiguration.setSocketTimeoutInMillisecond(5000); // Specify the socket timeout period. Unit: milliseconds. 
    // clientConfiguration.setRetryStrategy(new AlwaysRetryStrategy()); // Configure a retry policy. If you do not configure a retry policy, the default retry policy is used. 
    // TimeseriesClient client = new TimeseriesClient(endpoint, provider, instanceName, clientConfiguration, new ResourceManager(null, null));

    // do something
 
    // shutdown tableStore client
    client.shutdown();
}

{
    /**
     * Example 2 in which the V2 signature algorithm (original authentication method) is used: Use the credential provider {@link EnvironmentVariableCredentialsProvider } to read the environment variables.
     */
    EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();

    /**
     * using {@link DefaultCredentials } initialize tableStore client
     */
    TimeseriesClient client = new TimeseriesClient(endpoint, credentialsProvider, instanceName, null, new ResourceManager(null, null));

    // do something

    // shutdown tableStore client
    client.shutdown();
}

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.

instanceName

myinstance

The name of the instance. For more information, see Instances.

accessKeyId

System.getenv("TABLESTORE_ACCESS_KEY_ID")

The AccessKey pair that is obtained from environment variables. Make sure that the environment variables are configured.

accessKeySecret

System.getenv("TABLESTORE_ACCESS_KEY_SECRET")

Use STS

Note

Before you run the sample code, make sure that the TABLESTORE_ACCESS_KEY_ID, TABLESTORE_ACCESS_KEY_SECRET, and TABLESTORE_SESSION_TOKEN environment variables are configured. For more information, see Configure access credentials.

  final String endpoint = "yourEndpoint";
  final String instanceName = "yourInstance";
  final String accessKeyId = System.getenv("TABLESTORE_ACCESS_KEY_ID");
  final String accessKeySecret = System.getenv("TABLESTORE_ACCESS_KEY_SECRET");
  final String securityToken = System.getenv("TABLESTORE_SESSION_TOKEN");

  {
      /**
       * Example 1 in which the V2 signature algorithm (original authentication method) is used: Use the original AccessKey ID, AccessKey secret, and security token to construct {@link DefaultCredentials }.
       */
      DefaultCredentials credentials = new DefaultCredentials(accessKeyId, accessKeySecret, securityToken);
      CredentialsProvider provider = new DefaultCredentialProvider(credentials);

      /**
       * using {@link DefaultCredentials } initialize tableStore client
       */
      TimeseriesClient client = new TimeseriesClient(endpoint, provider, instanceName, null, new ResourceManager(null, null));
      // ClientConfiguration provides multiple configuration items. The following example includes some custom configuration items. 
      // ClientConfiguration clientConfiguration = new ClientConfiguration();
      // clientConfiguration.setConnectionTimeoutInMillisecond(5000); // Specify the timeout period for establishing a connection. Unit: milliseconds. 
      // clientConfiguration.setSocketTimeoutInMillisecond(5000); // Specify the socket timeout period. Unit: milliseconds. 
      // clientConfiguration.setRetryStrategy(new AlwaysRetryStrategy()); // Configure a retry policy. If you do not configure a retry policy, the default retry policy is used. 
      // TimeseriesClient client = new TimeseriesClient(endpoint, provider, instanceName, clientConfiguration, new ResourceManager(null, null));

      // do something

      // shutdown tableStore client
      client.shutdown();
  }

  {
      /**
       * Example 2 in which the V2 signature algorithm (original authentication method) is used: Use the credential provider {@link EnvironmentVariableCredentialsProvider } to read the environment variables.
       */
      EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();

      /**
       * using {@link DefaultCredentials } initialize tableStore client
       */
      TimeseriesClient client = new TimeseriesClient(endpoint, credentialsProvider, instanceName, null, new ResourceManager(null, null));

      // do something

      // shutdown tableStore client
      client.shutdown();
  }

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.

instanceName

myinstance

The name of the instance. For more information, see Instances.

accessKeyId

System.getenv("TABLESTORE_ACCESS_KEY_ID")

The AccessKey pair and security token that are obtained from environment variables. Make sure that the environment variables are configured.

accessKeySecret

System.getenv("TABLESTORE_ACCESS_KEY_SECRET")

securityToken

System.getenv("TABLESTORE_SESSION_TOKEN")

FAQ