介绍通过Java SDK创建表格存储数据表的方法和参数配置。
方法说明
public CreateTableResponse createTable(CreateTableRequest createTableRequest) throws TableStoreException, ClientException示例代码
基本用法
以下示例创建名为test_table的数据表,包含1个String类型的主键。
创建数据表后,需等待数据表加载完成(通常需要几秒钟)后再进行数据操作,否则操作会失败。
重要
创建数据表后,数据表的加密方式无法修改,如需创建加密表,请参见设置数据表加密。
package org.example.ots;
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.*;
public class CreateTable {
public static void main(String[] args) {
// 从环境变量中获取访问凭证(需要配置TABLESTORE_ACCESS_KEY_ID和TABLESTORE_ACCESS_KEY_SECRET)
final String accessKeyId = System.getenv("TABLESTORE_ACCESS_KEY_ID");
final String accessKeySecret = System.getenv("TABLESTORE_ACCESS_KEY_SECRET");
// TODO: 根据实例信息修改以下配置
final String region = "yourRegion"; // 填写实例所属的地域ID,例如 "cn-hangzhou"
final String instanceName = "yourInstanceName"; // 填写实例名称
final String endpoint = "yourEndpoint"; // 填写实例访问地址
SyncClient client = null;
try {
// 构造凭证
DefaultCredentials credentials = new DefaultCredentials(accessKeyId, accessKeySecret);
V4Credentials credentialsV4 = V4Credentials.createByServiceCredentials(credentials, region);
CredentialsProvider provider = new DefaultCredentialProvider(credentialsV4);
// 创建客户端实例
client = new SyncClient(endpoint, provider, instanceName, null, new ResourceManager(null, null));
// 构造数据表的结构信息
TableMeta tableMeta = new TableMeta("test_table"); // TODO: 根据需求修改数据表名称
// 创建数据表至少需要添加一个主键
tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("id", PrimaryKeyType.STRING)); // TODO: 根据需求修改数据表主键
// 构造数据表的配置信息
TableOptions tableOptions = new TableOptions();
// 创建数据表时必须指定最大版本数
tableOptions.setMaxVersions(1);
// 创建数据表时必须指定数据生命周期,-1表示数据永不过期
tableOptions.setTimeToLive(-1);
// 构造Request并发起请求
CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions);
client.createTable(request);
System.out.println("创建数据表成功");
} catch (Exception e) {
System.err.println("创建数据表失败,详细信息如下:");
e.printStackTrace();
} finally {
// 关闭客户端
if (client != null) {
client.shutdown();
}
}
}
}
添加主键
通过addPrimaryKeyColumn或addPrimaryKeyColumns方法添加主键,以addPrimaryKeyColumn为例。
tableMeta.addPrimaryKeyColumn("name", PrimaryKeyType.STRING);添加预定义列
通过addDefinedColumn或addDefinedColumns方法添加预定义列,以addDefinedColumn为例。
tableMeta.addDefinedColumn("age", DefinedColumnType.INTEGER);设置有效版本偏差
通过setMaxTimeDeviation方法设置有效版本偏差。
tableOptions.setMaxTimeDeviation(86400);设置是否允许更新
通过setAllowUpdate方法设置是否允许更新表数据。
tableOptions.setAllowUpdate(false);添加二级索引
通过在构造请求时指定indexMetas参数添加二级索引。
// 构造二级索引列表
ArrayList<IndexMeta> indexMetas = new ArrayList<IndexMeta>();
// 构造二级索引
IndexMeta indexMeta = new IndexMeta("test_table_idx");
// 设置索引主键
indexMeta.addPrimaryKeyColumn("id");
// 如果需要添加更多主键列,请先在数据表中定义对应的主键或预定义列
// indexMeta.addPrimaryKeyColumn("additional_column");
// 设置索引类型
indexMeta.setIndexType(IndexType.IT_LOCAL_INDEX);
// 设置索引更新模式
indexMeta.setIndexUpdateMode(IndexUpdateMode.IUM_SYNC_INDEX);
// 添加二级索引
indexMetas.add(indexMeta);
// 构造Request请求
CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions, indexMetas);设置Stream信息
通过请求的setStreamSpecification方法设置Stream信息。
StreamSpecification streamSpecification = new StreamSpecification(true, 168);
request.setStreamSpecification(streamSpecification);开启局部事务
通过请求的setLocalTxnEnabled方法开启局部事务。
request.setLocalTxnEnabled(true);设置预留读写吞吐量
通过请求的setReservedThroughput方法设置表的预留读写吞吐量。
// 设置预留读吞吐量为10000,预留写吞吐量为5000
ReservedThroughput reservedThroughput = new ReservedThroughput(10000, 5000);
request.setReservedThroughput(reservedThroughput);设置数据表加密
通过请求的setSseSpecification方法设置数据表加密方式。
KMS密钥加密
SSESpecification sseSpecification = new SSESpecification(true, SSEKeyType.SSE_KMS_SERVICE); request.setSseSpecification(sseSpecification);BYOK加密
说明运行代码前需要获取用户主密钥ID和RAM角色ARN,具体操作请参见BYOK加密。
String keyId = "key-hzz6*****************"; String roleArn = "acs:ram::1705************:role/tabletorebyok"; SSESpecification sseSpecification = new SSESpecification(true, SSEKeyType.SSE_BYOK, keyId, roleArn); request.setSseSpecification(sseSpecification);
相关文档
该文章对您有帮助吗?