介紹通過Java SDK建立Table Store資料表的方法和參數配置。
方法說明
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);