赛尔校园公共服务平台 Logo
平台使用
阿里云
百度云
移动云
智算服务
教育生态
登录 →
赛尔校园公共服务平台 Logo
平台使用 阿里云 百度云 移动云 智算服务 教育生态
登录
  1. 首页
  2. 阿里云
  3. 表格存储
  4. 开发参考
  5. SDK参考
  6. Java SDK
  7. 初始化Tablestore Client

初始化Tablestore Client

  • Java SDK
  • 发布于 2025-04-22
  • 0 次阅读
文档编辑
文档编辑

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

重要

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

准备工作

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

获取实例信息

  • 地域ID:实例所在地域的ID,例如华东1(杭州)的地域ID为cn-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。

配置访问凭证

使用Tablestore SDK发起请求访问表格存储,您需要配置访问凭证,阿里云服务会通过访问凭证验证您的身份信息和访问权限。

本文以阿里云账号的AccessKey为例为您介绍如何配置访问凭证。AccessKey的获取方式,请参见如何获取AccessKey。

直接在代码中保存访问凭证容易导致信息泄露,建议您将访问凭证保存在系统环境变量中。

Windows

以管理员身份运行命令提示符,执行以下命令。

# 配置 AccessKey ID
setx TABLESTORE_ACCESS_KEY_ID your_access_key_id /m
# 配置 AccessKey Secret
setx TABLESTORE_ACCESS_KEY_SECRET your_access_key_secret /m
macOS/Linux/Unix
# 配置 AccessKey ID
export TABLESTORE_ACCESS_KEY_ID=your_access_key_id
# 配置 AccessKey Secret
export TABLESTORE_ACCESS_KEY_SECRET=your_access_key_secret

更多关于配置访问凭证的信息,请参见配置访问凭证。

初始化Client

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

说明

不管是同步还是异步,都是线程安全的,且内部会自动管理线程和连接资源,您不需要为每个线程创建一个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();
    }
}

常见问题

  • 使用表格存储SDK时出现Signature mismatch异常

  • 使用SDK访问表格存储时出现Request denied by instance ACL policies异常

  • 使用SDK访问表格存储时出现Request denied because this instance can only be accessed from the binded VPC异常

  • Java SDK报错:SocketTimeoutException

  • 使用Java SDK时出现The access key id is invalid异常

  • 使用Java SDK时报错java.lang.IllegalStateException: Request cannot be executed; I/O reactor status: STOPPED

相关文档

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

相关文章

安装Java SDK 2025-04-22 14:29

表格存储(Tablestore)为开发者提供了多种主流编程语言的SDK,本文介绍如何进行

初始化Tablestore Client 2025-04-22 14:29

Tablestore Client是表格存储的客户端,它提供了一系列的方法,可以用来操作

Java SDK快速入门 2025-04-22 14:29

通过表格存储Java SDK使用

表操作 2025-04-22 14:29

本文介绍表格存储Java SDK提供的表级别功能。

创建数据表 2025-04-22 14:29

本文将通过参数说明和示例代码为您介绍如何使用 Java SDK 创建数据表。在创建数据表时,您需要指定数据表的结构信息和配置信息。CU 模式(原按量模式)下高性能型实例中的数据表还可以根据需要设置预留读写吞吐量。

创建加密表 2025-04-22 14:29

为了保证表数据安全,表格存储提供了数据落盘加密功能。创建数据表时您可以配置数据表加密。表格存储提供基于密钥管理服务(Key Management Service,简称KMS)密钥加密和基于自带密钥(Bring Your Own Key,简称BYOK)自定义密钥加密两种加密方式,请根据实际需要选择。

目录
Copyright © 2025 your company All Rights Reserved. Powered by 赛尔网络.
京ICP备14022346号-15
gongan beian 京公网安备11010802041014号