All Products
Search
Document Center

:Initialize a Tablestore client

Last Updated:Aug 09, 2025

The Tablestore Client is the client for Tablestore. It provides a series of methods that you can use to manage tables and data in Tablestore. This topic describes how to use Tablestore SDK for Java to initialize a Tablestore client.

Important

This topic uses the AccessKey pair of an Alibaba Cloud account as an example. To initialize the client using other credentials, see Use the AccessKey pair of a RAM user to access Tablestore and Use temporary access credentials from STS to access Tablestore.

Preparations

Before you initialize a Tablestore client, you must obtain instance information, install the Tablestore software development kit (SDK), and configure access credentials.

Obtain instance information

  • Region ID: The ID of the region where the instance is located. For example, the region ID of China (Hangzhou) is cn-hangzhou.

  • Instance name and endpoint: Each Tablestore instance has a unique endpoint. Your application must specify the endpoint to perform operations on tables and data. To obtain the endpoint, perform the following steps:

    1. Log on to the Tablestore console.

    2. In the top navigation bar, select a resource group and a region.

    3. On the Overview page, click the alias of an instance or click Instance Management in the Actions column.

    4. On the Instance Details tab, view the instance name and endpoint.

      Important

      By default, Internet access is disabled for new instances. To access resources in the instance over the Internet, you must enable Internet access for the instance.

Install the Tablestore SDK

If you use a Maven project, add the following dependency to the pom.xml file of the project:

<dependency>
    <groupId>com.aliyun.openservices</groupId>
    <artifactId>tablestore</artifactId>
    <version>5.17.4</version>
</dependency>                 

For more information about how to install the Tablestore SDK, see Install the Tablestore SDK.

Configure access credentials

Create an AccessKey pair for your Alibaba Cloud account or RAM user, and then configure the AccessKey pair as environment variables.

Note

After the configuration is complete, restart or refresh your compile and runtime environment, such as your IDE, command-line interface, other desktop applications, and background services. This ensures that the latest system environment variables are loaded. For more information about how to configure access credentials, see Configure access credentials.

Linux

  1. Run the following commands in the command-line interface to append environment variable settings to the ~/.bashrc file.

    echo "export TABLESTORE_ACCESS_KEY_ID='YOUR_ACCESS_KEY_ID'" >> ~/.bashrc
    echo "export TABLESTORE_ACCESS_KEY_SECRET='YOUR_ACCESS_KEY_SECRET'" >> ~/.bashrc
  2. Run the following command to allow the changes to take effect:

    source ~/.bashrc
  3. Run the following commands to check whether the environment variables take effect:

    echo $TABLESTORE_ACCESS_KEY_ID
    echo $TABLESTORE_ACCESS_KEY_SECRET

macOS

  1. Run the following command in the terminal to check the default Shell type.

    echo $SHELL
  2. Perform operations based on the default Shell type.

    Zsh
    1. Run the following commands to append environment variable settings to the ~/.zshrc file.

      echo "export TABLESTORE_ACCESS_KEY_ID='YOUR_ACCESS_KEY_ID'" >> ~/.zshrc
      echo "export TABLESTORE_ACCESS_KEY_SECRET='YOUR_ACCESS_KEY_SECRET'" >> ~/.zshrc
    2. Run the following command to allow the changes to take effect:

      source ~/.zshrc
    3. Run the following commands to check whether the environment variables take effect:

      echo $TABLESTORE_ACCESS_KEY_ID
      echo $TABLESTORE_ACCESS_KEY_SECRET
    Bash
    1. Run the following commands to append environment variable settings to the ~/.bash_profile file.

      echo "export TABLESTORE_ACCESS_KEY_ID='YOUR_ACCESS_KEY_ID'" >> ~/.bash_profile
      echo "export TABLESTORE_ACCESS_KEY_SECRET='YOUR_ACCESS_KEY_SECRET'" >> ~/.bash_profile
    2. Run the following command to allow the changes to take effect:

      source ~/.bash_profile
    3. Run the following commands to check whether the environment variables take effect:

      echo $TABLESTORE_ACCESS_KEY_ID
      echo $TABLESTORE_ACCESS_KEY_SECRET

Windows

CMD
  1. Run the following commands in CMD to set environment variables.

    setx TABLESTORE_ACCESS_KEY_ID "YOUR_ACCESS_KEY_ID"
    setx TABLESTORE_ACCESS_KEY_SECRET "YOUR_ACCESS_KEY_SECRET"
  2. After restarting CMD, run the following commands to check whether the environment variables take effect:

    echo %TABLESTORE_ACCESS_KEY_ID%
    echo %TABLESTORE_ACCESS_KEY_SECRET%
PowerShell
  1. Run the following command in PowerShell:

    [Environment]::SetEnvironmentVariable("TABLESTORE_ACCESS_KEY_ID", "YOUR_ACCESS_KEY_ID", [EnvironmentVariableTarget]::User)
    [Environment]::SetEnvironmentVariable("TABLESTORE_ACCESS_KEY_SECRET", "YOUR_ACCESS_KEY_SECRET", [EnvironmentVariableTarget]::User)
  2. Run the following commands to check whether the environment variables take effect:

    [Environment]::GetEnvironmentVariable("TABLESTORE_ACCESS_KEY_ID", [EnvironmentVariableTarget]::User)
    [Environment]::GetEnvironmentVariable("TABLESTORE_ACCESS_KEY_SECRET", [EnvironmentVariableTarget]::User)

Initialize the client

After you initialize a client, you can call its methods to access the Tablestore service. Tablestore provides SyncClient and AsyncClient for wide table models, and TimeseriesClient and AsyncTimeseriesClient for time series models. These clients support both synchronous and asynchronous calls.

Note

Both synchronous and asynchronous clients are thread-safe. They automatically manage threads and connection resources. Do not create a separate client for each thread or request. In multi-threaded applications, share a single client object.

Wide table model

V4 signature (Recommended)

Synchronous method

The following sample code shows how to use a V4 signature to initialize a client in synchronous mode, retrieve the list of data tables in an instance, and print the list to the console.

import com.alicloud.openservices.tablestore.ClientConfiguration;
import com.alicloud.openservices.tablestore.SyncClient;
import com.alicloud.openservices.tablestore.core.ResourceManager;
import com.alicloud.openservices.tablestore.core.auth.CredentialsProvider;
import com.alicloud.openservices.tablestore.core.auth.DefaultCredentialProvider;
import com.alicloud.openservices.tablestore.core.auth.DefaultCredentials;
import com.alicloud.openservices.tablestore.core.auth.V4Credentials;
import com.alicloud.openservices.tablestore.model.AlwaysRetryStrategy;
import com.alicloud.openservices.tablestore.model.ListTableResponse;

public class InitSyncClientV4 {
    public static void main(String[] args) {
        // Replace yourRegion with the ID of the region where your instance is located, such as cn-hangzhou.
        final String region = "yourRegion";
        // Replace yourInstanceName with the name of your instance.
        final String instanceName = "yourInstanceName";
        // Replace yourEndpoint with the endpoint of your instance.
        final String endpoint = "yourEndpoint";
        // Obtain the AccessKey ID and AccessKey secret from the system environment variables.
        final String accessKeyId = System.getenv("TABLESTORE_ACCESS_KEY_ID");
        final String accessKeySecret = System.getenv("TABLESTORE_ACCESS_KEY_SECRET");

        // Construct a V4 signature.
        DefaultCredentials credentials = new DefaultCredentials(accessKeyId, accessKeySecret);
        V4Credentials credentialsV4 = V4Credentials.createByServiceCredentials(credentials, region);
        CredentialsProvider provider = new DefaultCredentialProvider(credentialsV4);

        // Initialize a synchronous Tablestore client.
        SyncClient client = new SyncClient(endpoint, provider, instanceName, null, new ResourceManager(null, null));

        /*
        // You can specify ClientConfiguration to modify the default configuration items. The following example shows some custom configuration items.
        ClientConfiguration clientConfiguration = new ClientConfiguration();
        clientConfiguration.setConnectionTimeoutInMillisecond(5000); // Set the connection timeout period in milliseconds.
        clientConfiguration.setSocketTimeoutInMillisecond(5000); // Set the socket timeout period in milliseconds.
        clientConfiguration.setRetryStrategy(new AlwaysRetryStrategy()); // Set the retry policy. If you do not set this parameter, the default retry policy is used.
        SyncClient client = new SyncClient(endpoint, provider, instanceName, clientConfiguration, new ResourceManager(null, null));
         */

        // List the data tables in the instance and print the list to the console.
        ListTableResponse listTableResponse = client.listTable();
        listTableResponse.getTableNames().forEach(System.out::println);

        // Shut down the Tablestore client.
        client.shutdown();
    }
}

Asynchronous method

The following sample code shows how to use a V4 signature to initialize a client in asynchronous mode, retrieve the list of data tables in an instance, and print the list to the console.

import com.alicloud.openservices.tablestore.AsyncClient;
import com.alicloud.openservices.tablestore.ClientConfiguration;
import com.alicloud.openservices.tablestore.TableStoreCallback;
import com.alicloud.openservices.tablestore.core.ResourceManager;
import com.alicloud.openservices.tablestore.core.auth.CredentialsProvider;
import com.alicloud.openservices.tablestore.core.auth.DefaultCredentialProvider;
import com.alicloud.openservices.tablestore.core.auth.DefaultCredentials;
import com.alicloud.openservices.tablestore.core.auth.V4Credentials;
import com.alicloud.openservices.tablestore.model.AlwaysRetryStrategy;
import com.alicloud.openservices.tablestore.model.ListTableRequest;
import com.alicloud.openservices.tablestore.model.ListTableResponse;

import java.util.concurrent.*;

public class InitAsyncClientV4 {
    public static void main(String[] args) throws InterruptedException, TimeoutException, ExecutionException {
        // Replace yourRegion with the ID of the region where your instance is located, such as cn-hangzhou.
        final String region = "yourRegion";
        // Replace yourInstanceName with the name of your instance.
        final String instanceName = "yourInstanceName";
        // Replace yourEndpoint with the endpoint of your instance.
        final String endpoint = "yourEndpoint";
        // Obtain the AccessKey ID and AccessKey secret from the system environment variables.
        final String accessKeyId = System.getenv("TABLESTORE_ACCESS_KEY_ID");
        final String accessKeySecret = System.getenv("TABLESTORE_ACCESS_KEY_SECRET");

        // Construct a V4 signature.
        DefaultCredentials credentials = new DefaultCredentials(accessKeyId, accessKeySecret);
        V4Credentials credentialsV4 = V4Credentials.createByServiceCredentials(credentials, region);
        CredentialsProvider provider = new DefaultCredentialProvider(credentialsV4);

        // Initialize an asynchronous Tablestore client.
        AsyncClient client = new AsyncClient(endpoint, provider, instanceName, null, new ResourceManager(null, null));

        /*
        // You can specify ClientConfiguration to modify the default configuration items. The following example shows some custom configuration items.
        ClientConfiguration clientConfiguration = new ClientConfiguration();
        clientConfiguration.setConnectionTimeoutInMillisecond(5000); // Set the connection timeout period in milliseconds.
        clientConfiguration.setSocketTimeoutInMillisecond(5000); // Set the socket timeout period in milliseconds.
        clientConfiguration.setRetryStrategy(new AlwaysRetryStrategy()); // Set the retry policy. If you do not set this parameter, the default retry policy is used.
        AsyncClient client = new AsyncClient(endpoint, provider, instanceName, clientConfiguration, new ResourceManager(null, null));
        */
        
        Future<ListTableResponse> future = client.listTable(new TableStoreCallback<ListTableRequest, ListTableResponse>() {
            @Override
            public void onCompleted(ListTableRequest listTableRequest, ListTableResponse listTableResponse) {}
            @Override
            public void onFailed(ListTableRequest listTableRequest, Exception e) {}
        });

        ListTableResponse response = future.get(5, TimeUnit.SECONDS);
        if (response.getTableNames() != null) {
            response.getTableNames().forEach(System.out::println);
        }

        // Shut down the Tablestore client.
        client.shutdown();
    }
}

V2 signature

Synchronous method

The following sample code shows how to use a V2 signature to initialize a client in synchronous mode, retrieve the list of data tables in an instance, and print the list to the console.

import com.alicloud.openservices.tablestore.ClientConfiguration;
import com.alicloud.openservices.tablestore.SyncClient;
import com.alicloud.openservices.tablestore.core.ResourceManager;
import com.alicloud.openservices.tablestore.core.auth.CredentialsProvider;
import com.alicloud.openservices.tablestore.core.auth.DefaultCredentialProvider;
import com.alicloud.openservices.tablestore.core.auth.DefaultCredentials;
import com.alicloud.openservices.tablestore.model.AlwaysRetryStrategy;
import com.alicloud.openservices.tablestore.model.ListTableResponse;

public class InitSyncClientV2 {
    public static void main(String[] args) {
        // Replace yourInstanceName with the name of your instance.
        final String instanceName = "yourInstanceName";
        // Replace yourEndpoint with the endpoint of your instance.
        final String endpoint = "yourEndpoint";
        // Obtain the AccessKey ID and AccessKey secret from the system environment variables.
        final String accessKeyId = System.getenv("TABLESTORE_ACCESS_KEY_ID");
        final String accessKeySecret = System.getenv("TABLESTORE_ACCESS_KEY_SECRET");

        // Construct a V2 signature.
        DefaultCredentials credentials = new DefaultCredentials(accessKeyId, accessKeySecret);
        CredentialsProvider provider = new DefaultCredentialProvider(credentials);

        // Initialize a synchronous Tablestore client.
        SyncClient client = new SyncClient(endpoint, provider, instanceName, null, new ResourceManager(null, null));

        /*
        // You can specify ClientConfiguration to modify the default configuration items. The following example shows some custom configuration items.
        ClientConfiguration clientConfiguration = new ClientConfiguration();
        clientConfiguration.setConnectionTimeoutInMillisecond(5000); // Set the connection timeout period in milliseconds.
        clientConfiguration.setSocketTimeoutInMillisecond(5000); // Set the socket timeout period in milliseconds.
        clientConfiguration.setRetryStrategy(new AlwaysRetryStrategy()); // Set the retry policy. If you do not set this parameter, the default retry policy is used.
        SyncClient client = new SyncClient(endpoint, provider, instanceName, clientConfiguration, new ResourceManager(null, null));
         */

        // List the data tables in the instance and print the list to the console.
        ListTableResponse listTableResponse = client.listTable();
        listTableResponse.getTableNames().forEach(System.out::println);

        // Shut down the Tablestore client.
        client.shutdown();
    }
}

Asynchronous method

The following sample code shows how to use a V2 signature to initialize a client in asynchronous mode, retrieve the list of data tables in an instance, and print the list to the console.

import com.alicloud.openservices.tablestore.AsyncClient;
import com.alicloud.openservices.tablestore.ClientConfiguration;
import com.alicloud.openservices.tablestore.TableStoreCallback;
import com.alicloud.openservices.tablestore.core.ResourceManager;
import com.alicloud.openservices.tablestore.core.auth.CredentialsProvider;
import com.alicloud.openservices.tablestore.core.auth.DefaultCredentialProvider;
import com.alicloud.openservices.tablestore.core.auth.DefaultCredentials;
import com.alicloud.openservices.tablestore.model.AlwaysRetryStrategy;
import com.alicloud.openservices.tablestore.model.ListTableRequest;
import com.alicloud.openservices.tablestore.model.ListTableResponse;

import java.util.concurrent.*;

public class InitAsyncClientV2 {

    public static void main(String[] args) throws InterruptedException, ExecutionException, TimeoutException {
        // Replace yourInstanceName with the name of your instance.
        final String instanceName = "yourInstanceName";
        // Replace yourEndpoint with the endpoint of your instance.
        final String endpoint = "yourEndpoint";
        // Obtain the AccessKey ID and AccessKey secret from the system environment variables.
        final String accessKeyId = System.getenv("TABLESTORE_ACCESS_KEY_ID");
        final String accessKeySecret = System.getenv("TABLESTORE_ACCESS_KEY_SECRET");

        // Construct a V2 signature.
        DefaultCredentials credentials = new DefaultCredentials(accessKeyId, accessKeySecret);
        CredentialsProvider provider = new DefaultCredentialProvider(credentials);

        // Initialize an asynchronous Tablestore client.
        AsyncClient client = new AsyncClient(endpoint, provider, instanceName, null, new ResourceManager(null, null));

        /*
        // You can specify ClientConfiguration to modify the default configuration items. The following example shows some custom configuration items.
        ClientConfiguration clientConfiguration = new ClientConfiguration();
        clientConfiguration.setConnectionTimeoutInMillisecond(5000); // Set the connection timeout period in milliseconds.
        clientConfiguration.setSocketTimeoutInMillisecond(5000); // Set the socket timeout period in milliseconds.
        clientConfiguration.setRetryStrategy(new AlwaysRetryStrategy()); // Set the retry policy. If you do not set this parameter, the default retry policy is used.
        AsyncClient client = new AsyncClient(endpoint, provider, instanceName, clientConfiguration, new ResourceManager(null, null));
        */

        Future<ListTableResponse> future = client.listTable(new TableStoreCallback<ListTableRequest, ListTableResponse>() {
            @Override
            public void onCompleted(ListTableRequest listTableRequest, ListTableResponse listTableResponse) {}
            @Override
            public void onFailed(ListTableRequest listTableRequest, Exception e) {}
        });

        ListTableResponse response = future.get(5, TimeUnit.SECONDS);
        if (response.getTableNames() != null) {
            response.getTableNames().forEach(System.out::println);
        }

        // Shut down the Tablestore client.
        client.shutdown();

    }
}

Time series model

V4 signature (Recommended)

Synchronous method

The following sample code shows how to use a V4 signature to initialize a client in synchronous mode, retrieve the list of time series tables in an instance, and print the list to the console.

import com.alicloud.openservices.tablestore.ClientConfiguration;
import com.alicloud.openservices.tablestore.TimeseriesClient;
import com.alicloud.openservices.tablestore.core.ResourceManager;
import com.alicloud.openservices.tablestore.core.auth.CredentialsProvider;
import com.alicloud.openservices.tablestore.core.auth.DefaultCredentialProvider;
import com.alicloud.openservices.tablestore.core.auth.DefaultCredentials;
import com.alicloud.openservices.tablestore.core.auth.V4Credentials;
import com.alicloud.openservices.tablestore.model.AlwaysRetryStrategy;
import com.alicloud.openservices.tablestore.model.timeseries.ListTimeseriesTableResponse;

public class InitTimeseriesClientV4 {
    public static void main(String[] args) {
        // Replace yourRegion with the ID of the region where your instance is located, such as cn-hangzhou.
        final String region = "yourRegion";
        // Replace yourInstanceName with the name of your instance.
        final String instanceName = "yourInstanceName";
        // Replace yourEndpoint with the endpoint of your instance.
        final String endpoint = "yourEndpoint";
        // Obtain the AccessKey ID and AccessKey secret from the system environment variables.
        final String accessKeyId = System.getenv("TABLESTORE_ACCESS_KEY_ID");
        final String accessKeySecret = System.getenv("TABLESTORE_ACCESS_KEY_SECRET");

        // Construct a V4 signature.
        DefaultCredentials credentials = new DefaultCredentials(accessKeyId, accessKeySecret);
        V4Credentials credentialsV4 = V4Credentials.createByServiceCredentials(credentials, region);
        CredentialsProvider provider = new DefaultCredentialProvider(credentialsV4);

        // Initialize a synchronous Tablestore client.
        TimeseriesClient client = new TimeseriesClient(endpoint, provider, instanceName, null, new ResourceManager(null, null));

        /*
        // You can specify ClientConfiguration to modify the default configuration items. The following example shows some custom configuration items.
        ClientConfiguration clientConfiguration = new ClientConfiguration();
        clientConfiguration.setConnectionTimeoutInMillisecond(5000); // Set the connection timeout period in milliseconds.
        clientConfiguration.setSocketTimeoutInMillisecond(5000); // Set the socket timeout period in milliseconds.
        clientConfiguration.setRetryStrategy(new AlwaysRetryStrategy()); // Set the retry policy. If you do not set this parameter, the default retry policy is used.
        TimeseriesClient client = new TimeseriesClient(endpoint, provider, instanceName, clientConfiguration, new ResourceManager(null, null));
         */

        // List the time series tables in the instance and print the list to the console.
        ListTimeseriesTableResponse listTimeseriesTableResponse = client.listTimeseriesTable();
        listTimeseriesTableResponse.getTimeseriesTableNames().forEach(System.out::println);

        // Shut down the Tablestore TimeSeriesClient.
        client.shutdown();
    }
}

Asynchronous method

The following sample code shows how to use a V4 signature to initialize a client in asynchronous mode, retrieve the list of time series tables in an instance, and print the list to the console.

import com.alicloud.openservices.tablestore.AsyncTimeseriesClient;
import com.alicloud.openservices.tablestore.ClientConfiguration;
import com.alicloud.openservices.tablestore.TableStoreCallback;
import com.alicloud.openservices.tablestore.core.ResourceManager;
import com.alicloud.openservices.tablestore.core.auth.CredentialsProvider;
import com.alicloud.openservices.tablestore.core.auth.DefaultCredentialProvider;
import com.alicloud.openservices.tablestore.core.auth.DefaultCredentials;
import com.alicloud.openservices.tablestore.core.auth.V4Credentials;
import com.alicloud.openservices.tablestore.model.AlwaysRetryStrategy;
import com.alicloud.openservices.tablestore.model.timeseries.ListTimeseriesTableRequest;
import com.alicloud.openservices.tablestore.model.timeseries.ListTimeseriesTableResponse;

import java.util.concurrent.*;

public class InitAsyncTimeseriesClientV4 {
    public static void main(String[] args) throws InterruptedException, TimeoutException, ExecutionException {
        // Replace yourRegion with the ID of the region where your instance is located, such as cn-hangzhou.
        final String region = "yourRegion";
        // Replace yourInstanceName with the name of your instance.
        final String instanceName = "yourInstanceName";
        // Replace yourEndpoint with the endpoint of your instance.
        final String endpoint = "yourEndpoint";
        // Obtain the AccessKey ID and AccessKey secret from the system environment variables.
        final String accessKeyId = System.getenv("TABLESTORE_ACCESS_KEY_ID");
        final String accessKeySecret = System.getenv("TABLESTORE_ACCESS_KEY_SECRET");

        // Construct a V4 signature.
        DefaultCredentials credentials = new DefaultCredentials(accessKeyId, accessKeySecret);
        V4Credentials credentialsV4 = V4Credentials.createByServiceCredentials(credentials, region);
        CredentialsProvider provider = new DefaultCredentialProvider(credentialsV4);

        // Initialize an asynchronous Tablestore client.
        AsyncTimeseriesClient client = new AsyncTimeseriesClient(endpoint, provider, instanceName, null, new ResourceManager(null, null));

        /*
        // You can specify ClientConfiguration to modify the default configuration items. The following example shows some custom configuration items.
        ClientConfiguration clientConfiguration = new ClientConfiguration();
        clientConfiguration.setConnectionTimeoutInMillisecond(5000); // Set the connection timeout period in milliseconds.
        clientConfiguration.setSocketTimeoutInMillisecond(5000); // Set the socket timeout period in milliseconds.
        clientConfiguration.setRetryStrategy(new AlwaysRetryStrategy()); // Set the retry policy. If you do not set this parameter, the default retry policy is used.
        AsyncTimeseriesClient client = new AsyncTimeseriesClient(endpoint, provider, instanceName, clientConfiguration, new ResourceManager(null, null));
        */
        
        Future<ListTimeseriesTableResponse> future = client.listTimeseriesTable(new TableStoreCallback<ListTimeseriesTableRequest, ListTimeseriesTableResponse>() {
            @Override
            public void onCompleted(ListTimeseriesTableRequest listTimeseriesTableRequest, ListTimeseriesTableResponse listTimeseriesTableResponse) {}
            @Override
            public void onFailed(ListTimeseriesTableRequest listTimeseriesTableRequest, Exception e) {}
        });

        ListTimeseriesTableResponse response = future.get(5, TimeUnit.SECONDS);
        if (response.getTimeseriesTableNames() != null) {
            response.getTimeseriesTableNames().forEach(System.out::println);
        }

        // Shut down the Tablestore client.
        client.shutdown();
    }
}

V2 signature

Synchronous method

The following sample code shows how to use a V2 signature to initialize a client in synchronous mode, retrieve the list of time series tables in an instance, and print the list to the console.

import com.alicloud.openservices.tablestore.ClientConfiguration;
import com.alicloud.openservices.tablestore.TimeseriesClient;
import com.alicloud.openservices.tablestore.core.ResourceManager;
import com.alicloud.openservices.tablestore.core.auth.CredentialsProvider;
import com.alicloud.openservices.tablestore.core.auth.DefaultCredentialProvider;
import com.alicloud.openservices.tablestore.core.auth.DefaultCredentials;
import com.alicloud.openservices.tablestore.model.AlwaysRetryStrategy;
import com.alicloud.openservices.tablestore.model.timeseries.ListTimeseriesTableResponse;

public class InitTimeseriesClientV2 {
    public static void main(String[] args) {
        // Replace yourInstanceName with the name of your instance.
        final String instanceName = "yourInstanceName";
        // Replace yourEndpoint with the endpoint of your instance.
        final String endpoint = "yourEndpoint";
        // Obtain the AccessKey ID and AccessKey secret from the system environment variables.
        final String accessKeyId = System.getenv("TABLESTORE_ACCESS_KEY_ID");
        final String accessKeySecret = System.getenv("TABLESTORE_ACCESS_KEY_SECRET");

        // Construct a V2 signature.
        DefaultCredentials credentials = new DefaultCredentials(accessKeyId, accessKeySecret);
        CredentialsProvider provider = new DefaultCredentialProvider(credentials);

        // Initialize a synchronous Tablestore client.
        TimeseriesClient client = new TimeseriesClient(endpoint, provider, instanceName, null, new ResourceManager(null, null));

        /*
        // You can specify ClientConfiguration to modify the default configuration items. The following example shows some custom configuration items.
        ClientConfiguration clientConfiguration = new ClientConfiguration();
        clientConfiguration.setConnectionTimeoutInMillisecond(5000); // Set the connection timeout period in milliseconds.
        clientConfiguration.setSocketTimeoutInMillisecond(5000); // Set the socket timeout period in milliseconds.
        clientConfiguration.setRetryStrategy(new AlwaysRetryStrategy()); // Set the retry policy. If you do not set this parameter, the default retry policy is used.
        TimeseriesClient client = new TimeseriesClient(endpoint, provider, instanceName, clientConfiguration, new ResourceManager(null, null));
         */

        // List the time series tables in the instance and print the list to the console.
        ListTimeseriesTableResponse listTimeseriesTableResponse = client.listTimeseriesTable();
        listTimeseriesTableResponse.getTimeseriesTableNames().forEach(System.out::println);

        // Shut down the Tablestore TimeSeriesClient.
        client.shutdown();
    }
}

Asynchronous method

The following sample code shows how to use a V2 signature to initialize a client in asynchronous mode, retrieve the list of time series tables in an instance, and print the list to the console.

import com.alicloud.openservices.tablestore.AsyncTimeseriesClient;
import com.alicloud.openservices.tablestore.ClientConfiguration;
import com.alicloud.openservices.tablestore.TableStoreCallback;
import com.alicloud.openservices.tablestore.core.ResourceManager;
import com.alicloud.openservices.tablestore.core.auth.CredentialsProvider;
import com.alicloud.openservices.tablestore.core.auth.DefaultCredentialProvider;
import com.alicloud.openservices.tablestore.core.auth.DefaultCredentials;
import com.alicloud.openservices.tablestore.model.AlwaysRetryStrategy;
import com.alicloud.openservices.tablestore.model.timeseries.ListTimeseriesTableRequest;
import com.alicloud.openservices.tablestore.model.timeseries.ListTimeseriesTableResponse;

import java.util.concurrent.*;

public class InitAsyncTimeseriesClientV2 {

    public static void main(String[] args) throws InterruptedException, TimeoutException, ExecutionException {
        // Replace yourInstanceName with the name of your instance.
        final String instanceName = "yourInstanceName";
        // Replace yourEndpoint with the endpoint of your instance.
        final String endpoint = "yourEndpoint";
        // Obtain the AccessKey ID and AccessKey secret from the system environment variables.
        final String accessKeyId = System.getenv("TABLESTORE_ACCESS_KEY_ID");
        final String accessKeySecret = System.getenv("TABLESTORE_ACCESS_KEY_SECRET");

        // Construct a V2 signature.
        DefaultCredentials credentials = new DefaultCredentials(accessKeyId, accessKeySecret);
        CredentialsProvider provider = new DefaultCredentialProvider(credentials);

        // Initialize an asynchronous Tablestore client.
        AsyncTimeseriesClient client = new  AsyncTimeseriesClient(endpoint, provider, instanceName, null, new ResourceManager(null, null));

        /*
        // You can specify ClientConfiguration to modify the default configuration items. The following example shows some custom configuration items.
        ClientConfiguration clientConfiguration = new ClientConfiguration();
        clientConfiguration.setConnectionTimeoutInMillisecond(5000); // Set the connection timeout period in milliseconds.
        clientConfiguration.setSocketTimeoutInMillisecond(5000); // Set the socket timeout period in milliseconds.
        clientConfiguration.setRetryStrategy(new AlwaysRetryStrategy()); // Set the retry policy. If you do not set this parameter, the default retry policy is used.
        AsyncTimeseriesClient client = new  AsyncTimeseriesClient(endpoint, provider, instanceName, null, new ResourceManager(null, null));
        */
        
        Future<ListTimeseriesTableResponse> future = client.listTimeseriesTable(new TableStoreCallback<ListTimeseriesTableRequest, ListTimeseriesTableResponse>() {
            @Override
            public void onCompleted(ListTimeseriesTableRequest listTimeseriesTableRequest, ListTimeseriesTableResponse listTimeseriesTableResponse) {}
            @Override
            public void onFailed(ListTimeseriesTableRequest listTimeseriesTableRequest, Exception e) {}
        });

        ListTimeseriesTableResponse response = future.get(5, TimeUnit.SECONDS);
        if (response.getTimeseriesTableNames() != null) {
            response.getTimeseriesTableNames().forEach(System.out::println);
        }

        // Shut down the Tablestore client.
        client.shutdown();
    }
}

FAQ

References

  • To better protect your keys, Tablestore supports the V4 signature algorithm. We recommend that you use the V4 signature when you initialize the client. For more information, see User key security.