このトピックでは、Tablestore SDK for Java を使用して Tablestore データテーブルを作成し、そのパラメーターを設定する方法について説明します。
メソッド
public CreateTableResponse createTable(CreateTableRequest createTableRequest) throws TableStoreException, ClientExceptionサンプルコード
基本機能
この例では、test_table という名前のデータテーブルを作成します。このテーブルには、String 型のプライマリキー列が 1 つあります。
データテーブルを作成した後、データ操作を実行する前にテーブルがロードされるのを待つ必要があります。このプロセスには通常、数秒かかります。待たない場合、操作は失敗します。
データテーブルを作成した後に暗号化メソッドを変更することはできません。暗号化されたテーブルを作成する方法については、「データテーブルの暗号化を設定する」をご参照ください。
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: 必要に応じてテーブル名を変更します。
// テーブルを作成するには、少なくとも 1 つのプライマリキー列を追加する必要があります。
tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("id", PrimaryKeyType.STRING)); // TODO: 必要に応じてテーブルのプライマリキーを変更します。
// テーブルを設定します。
TableOptions tableOptions = new TableOptions();
// データテーブルを作成するときは、バージョンの最大数を指定する必要があります。
tableOptions.setMaxVersions(1);
// データテーブルを作成するときは、生存時間 (TTL) を指定する必要があります。値 -1 は、データが無期限であることを意味します。
tableOptions.setTimeToLive(-1);
// リクエストを作成して送信します。
CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions);
client.createTable(request);
System.out.println("The data table is created.");
} catch (Exception e) {
System.err.println("Failed to create the data table. Details:");
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);
// リクエストを作成します。
CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions, indexMetas);ストリーム情報の設定
リクエストの setStreamSpecification メソッドを使用して、ストリーム情報を設定できます。
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 暗号化
説明コードを実行する前に、カスタマーマスターキー (CMK) 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);