初始化Tablestore Client

Tablestore Client表格存储的客户端,它提供了一系列的方法,可以用来操作表格存储的表和数据。本文介绍如何在Java中进行Tablestore Client的初始化。

重要

本文以阿里云账号的AccessKey为例为您介绍如何初始化Tablestore Client,如果您想使用RAM用户的访问密钥或STS临时访问凭证进行初始化,请参见使用RAM用户访问密钥访问表格存储使用STS临时访问凭证访问表格存储

准备工作

初始化Tablestore Client前,您需要获取实例的相关信息、安装Tablestore SDK并配置访问凭证。

获取实例信息

  • 地域ID:实例所在地域的ID,例如华东1(杭州)的地域IDcn-hangzhou。

  • 实例名称和访问地址:每个表格存储实例对应一个访问地址(Endpoint),应用程序进行表和数据操作时需要指定访问地址,获取方式如下。

    1. 登录表格存储控制台

    2. 在页面上方,选择资源组和地域。

    3. 概览页面,单击实例别名或在操作列单击实例管理

    4. 实例详情页签,查看实例的名称和访问地址。

      重要

      新创建的实例默认未启用公网访问功能。如果您需要通过公网访问实例中的资源,则必须开启实例的公网访问功能

安装Tablestore SDK

如果您使用的是Maven项目,请在项目的pom.xml文件中添加如下依赖:

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

更多关于安装Tablestore SDK的信息,请参见安装Tablestore SDK

配置访问凭证

您需要为阿里云账号或RAM用户创建AccessKey,并按如下方式将AccessKey配置到环境变量中。

说明

配置完成后请重启或刷新您的编译运行环境,包括 IDE、命令行界面、其它桌面应用程序及后台服务,确保最新的系统环境变量成功加载。更多关于配置访问凭证的信息,请参见配置访问凭证

Linux

  1. 在命令行界面执行以下命令来将环境变量设置追加到 ~/.bashrc 文件中。

    echo "export TABLESTORE_ACCESS_KEY_ID='YOUR_ACCESS_KEY_ID'" >> ~/.bashrc
    echo "export TABLESTORE_ACCESS_KEY_SECRET='YOUR_ACCESS_KEY_SECRET'" >> ~/.bashrc
  2. 执行以下命令使变更生效。

    source ~/.bashrc
  3. 执行以下命令检查环境变量是否生效。

    echo $TABLESTORE_ACCESS_KEY_ID
    echo $TABLESTORE_ACCESS_KEY_SECRET

macOS

  1. 在终端中执行以下命令,查看默认 Shell 类型。

    echo $SHELL
  2. 根据默认 Shell 类型进行操作。

    Zsh
    1. 执行以下命令来将环境变量设置追加到 ~/.zshrc 文件中。

      echo "export TABLESTORE_ACCESS_KEY_ID='YOUR_ACCESS_KEY_ID'" >> ~/.zshrc
      echo "export TABLESTORE_ACCESS_KEY_SECRET='YOUR_ACCESS_KEY_SECRET'" >> ~/.zshrc
    2. 执行以下命令使变更生效。

      source ~/.zshrc
    3. 执行以下命令检查环境变量是否生效。

      echo $TABLESTORE_ACCESS_KEY_ID
      echo $TABLESTORE_ACCESS_KEY_SECRET
    Bash
    1. 执行以下命令来将环境变量设置追加到 ~/.bash_profile 文件中。

      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. 执行以下命令使变更生效。

      source ~/.bash_profile
    3. 执行以下命令检查环境变量是否生效。

      echo $TABLESTORE_ACCESS_KEY_ID
      echo $TABLESTORE_ACCESS_KEY_SECRET

Windows

CMD
  1. CMD中运行以下命令设置环境变量。

    setx TABLESTORE_ACCESS_KEY_ID "YOUR_ACCESS_KEY_ID"
    setx TABLESTORE_ACCESS_KEY_SECRET "YOUR_ACCESS_KEY_SECRET"
  2. 重启CMD后,运行以下命令,检查环境变量是否生效。

    echo %TABLESTORE_ACCESS_KEY_ID%
    echo %TABLESTORE_ACCESS_KEY_SECRET%
PowerShell
  1. 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. 运行以下命令,检查环境变量是否生效。

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

初始化Client

您需要先初始化一个Client,然后调用该Client的方法来访问表格存储服务。表格存储提供了SyncClientAsyncClient两种宽表模型客户端,以及TimeseriesClientAsyncTimeseriesClient两种时序模型客户端,分别对应同步和异步方式调用。

说明

不管是同步还是异步,都是线程安全的,且内部会自动管理线程和连接资源,您不需要为每个线程创建一个Client,也不需要为每个请求创建一个Client。建议您在使用多线程时,共用一个Client对象。

宽表模型

V4签名(推荐)

以下示例代码使用V4签名初始化Client,获取实例下的数据表列表并打印到控制台。

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 InitClientV4 {
    public static void main(String[] args) {
        // yourRegion 填写您的实例所在地域,如 cn-hangzhou
        final String region = "yourRegion";
        // yourInstanceName 填写您的实例名称
        final String instanceName = "yourInstanceName";
        // yourEndpoint 填写您的实例访问地址
        final String endpoint = "yourEndpoint";
        // 获取系统变量里的 AccessKey ID 和 AccessKey Secret
        final String accessKeyId = System.getenv("TABLESTORE_ACCESS_KEY_ID");
        final String accessKeySecret = System.getenv("TABLESTORE_ACCESS_KEY_SECRET");

        // 构造 V4 签名
        DefaultCredentials credentials = new DefaultCredentials(accessKeyId, accessKeySecret);
        V4Credentials credentialsV4 = V4Credentials.createByServiceCredentials(credentials, region);
        CredentialsProvider provider = new DefaultCredentialProvider(credentialsV4);

        // 初始化表格存储客户端
        SyncClient client = new SyncClient(endpoint, provider, instanceName, null, new ResourceManager(null, null));

        /*
        // 您可以通过指定 ClientConfiguration 修改默认配置项,以下示例为部分自定义配置项。
        ClientConfiguration clientConfiguration = new ClientConfiguration();
        clientConfiguration.setConnectionTimeoutInMillisecond(5000); // 设置建立连接的超时时间,单位为毫秒。
        clientConfiguration.setSocketTimeoutInMillisecond(5000); // 设置 socket 超时时间,单位为毫秒。
        clientConfiguration.setRetryStrategy(new AlwaysRetryStrategy()); // 设置重试策略,如果不设置,则采用默认的重试策略。
        SyncClient client = new SyncClient(endpoint, provider, instanceName, clientConfiguration, new ResourceManager(null, null));
         */

        // 列出实例下的数据表列表并打印到控制台
        ListTableResponse listTableResponse = client.listTable();
        listTableResponse.getTableNames().forEach(System.out::println);

        // 关闭 Tablestore Client
        client.shutdown();
    }
}

V2签名

以下示例代码使用V2签名初始化Client,获取实例下的数据表列表并打印到控制台。

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 InitClientV2 {
    public static void main(String[] args) {
        // yourInstanceName 填写您的实例名称
        final String instanceName = "yourInstanceName";
        // yourEndpoint 填写您的实例访问地址
        final String endpoint = "yourEndpoint";
        // 获取系统变量里的 AccessKey ID 和 AccessKey Secret
        final String accessKeyId = System.getenv("TABLESTORE_ACCESS_KEY_ID");
        final String accessKeySecret = System.getenv("TABLESTORE_ACCESS_KEY_SECRET");

        // 构造 V2 签名
        DefaultCredentials credentials = new DefaultCredentials(accessKeyId, accessKeySecret);
        CredentialsProvider provider = new DefaultCredentialProvider(credentials);

        // 初始化表格存储客户端
        SyncClient client = new SyncClient(endpoint, provider, instanceName, null, new ResourceManager(null, null));

        /*
        // 您可以通过指定 ClientConfiguration 修改默认配置项,以下示例为部分自定义配置项。
        ClientConfiguration clientConfiguration = new ClientConfiguration();
        clientConfiguration.setConnectionTimeoutInMillisecond(5000); // 设置建立连接的超时时间,单位为毫秒。
        clientConfiguration.setSocketTimeoutInMillisecond(5000); // 设置 socket 超时时间,单位为毫秒。
        clientConfiguration.setRetryStrategy(new AlwaysRetryStrategy()); // 设置重试策略,如果不设置,则采用默认的重试策略。
        SyncClient client = new SyncClient(endpoint, provider, instanceName, clientConfiguration, new ResourceManager(null, null));
         */

        // 列出实例下的数据表列表并打印到控制台
        ListTableResponse listTableResponse = client.listTable();
        listTableResponse.getTableNames().forEach(System.out::println);

        // 关闭 Tablestore Client
        client.shutdown();
    }
}

时序模型

V4签名(推荐)

以下示例代码使用V4签名初始化Client,获取实例下的时序表列表并打印到控制台。

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) {
        // yourRegion 填写您的实例所在地域,如 cn-hangzhou
        final String region = "yourRegion";
        // yourInstanceName 填写您的实例名称
        final String instanceName = "yourInstanceName";
        // yourEndpoint 填写您的实例访问地址
        final String endpoint = "yourEndpoint";
        // 获取系统变量里的 AccessKey ID 和 AccessKey Secret
        final String accessKeyId = System.getenv("TABLESTORE_ACCESS_KEY_ID");
        final String accessKeySecret = System.getenv("TABLESTORE_ACCESS_KEY_SECRET");

        // 构造 V4 签名
        DefaultCredentials credentials = new DefaultCredentials(accessKeyId, accessKeySecret);
        V4Credentials credentialsV4 = V4Credentials.createByServiceCredentials(credentials, region);
        CredentialsProvider provider = new DefaultCredentialProvider(credentialsV4);

        // 初始化表格存储客户端
        TimeseriesClient client = new TimeseriesClient(endpoint, provider, instanceName, null, new ResourceManager(null, null));

        /*
        // 您可以通过指定 ClientConfiguration 修改默认配置项,以下示例为部分自定义配置项。
        ClientConfiguration clientConfiguration = new ClientConfiguration();
        clientConfiguration.setConnectionTimeoutInMillisecond(5000); // 设置建立连接的超时时间,单位为毫秒。
        clientConfiguration.setSocketTimeoutInMillisecond(5000); // 设置 socket 超时时间,单位为毫秒。
        clientConfiguration.setRetryStrategy(new AlwaysRetryStrategy()); // 设置重试策略,如果不设置,则采用默认的重试策略。
        TimeseriesClient client = new TimeseriesClient(endpoint, provider, instanceName, clientConfiguration, new ResourceManager(null, null));
         */

        // 列出实例下的时序表列表并打印到控制台
        ListTimeseriesTableResponse listTimeseriesTableResponse = client.listTimeseriesTable();
        listTimeseriesTableResponse.getTimeseriesTableNames().forEach(System.out::println);

        // 关闭 Tablestore TimeSeriesClient
        client.shutdown();
    }
}

V2签名

以下示例代码使用V2签名初始化Client,获取实例下的时序表列表并打印到控制台。

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) {
        // yourInstanceName 填写您的实例名称
        final String instanceName = "yourInstanceName";
        // yourEndpoint 填写您的实例访问地址
        final String endpoint = "yourEndpoint";
        // 获取系统变量里的 AccessKey ID 和 AccessKey Secret
        final String accessKeyId = System.getenv("TABLESTORE_ACCESS_KEY_ID");
        final String accessKeySecret = System.getenv("TABLESTORE_ACCESS_KEY_SECRET");

        // 构造 V2 签名
        DefaultCredentials credentials = new DefaultCredentials(accessKeyId, accessKeySecret);
        CredentialsProvider provider = new DefaultCredentialProvider(credentials);

        // 初始化表格存储客户端
        TimeseriesClient client = new TimeseriesClient(endpoint, provider, instanceName, null, new ResourceManager(null, null));

        /*
        // 您可以通过指定 ClientConfiguration 修改默认配置项,以下示例为部分自定义配置项。
        ClientConfiguration clientConfiguration = new ClientConfiguration();
        clientConfiguration.setConnectionTimeoutInMillisecond(5000); // 设置建立连接的超时时间,单位为毫秒。
        clientConfiguration.setSocketTimeoutInMillisecond(5000); // 设置 socket 超时时间,单位为毫秒。
        clientConfiguration.setRetryStrategy(new AlwaysRetryStrategy()); // 设置重试策略,如果不设置,则采用默认的重试策略。
        TimeseriesClient client = new TimeseriesClient(endpoint, provider, instanceName, clientConfiguration, new ResourceManager(null, null));
         */

        // 列出实例下的时序表列表并打印到控制台
        ListTimeseriesTableResponse listTimeseriesTableResponse = client.listTimeseriesTable();
        listTimeseriesTableResponse.getTimeseriesTableNames().forEach(System.out::println);

        // 关闭 Tablestore TimeSeriesClient
        client.shutdown();
    }
}

常见问题

相关文档

  • 为了进一步保证用户密钥的安全,表格存储支持在初始化客户端时使用V4签名算法对用户密钥进行保护,推荐您使用V4签名进行客户端初始化。更多信息,请参见用户密钥安全