ScalarDB Java API ガイド
このページは英語版のページが機械翻訳されたものです。英語版との間に矛盾または不一致がある場合は、英語版を正としてください。
ScalarDB Java API は、主に管理 API とトランザクション API で設定されています。このガイドでは、どのような種類の API が存在するのか、それらの使用方法、例外の処理方法などの関連トピックについて簡単に説明します。
Administrative API
このセクションでは、ScalarDB の管理 API を使用してプログラムで管理操作を実行する方法について説明します。
管理操作を実行する別の方法は、Schema Loader を使用することです。
DistributedTransactionAdmin
インスタンスを取得する
管理操作を実行するには、まず DistributedTransactionAdmin
インスタンスを取得する必要があります。
DistributedTransactionAdmin
インス タンスを取得するには、次のように TransactionFactory
を使用します。
TransactionFactory transactionFactory = TransactionFactory.create("<CONFIGURATION_FILE_PATH>");
DistributedTransactionAdmin admin = transactionFactory.getTransactionAdmin();
設定の詳細については、ScalarDB 設定を参照してください。
すべての管理操作を実行したら、次のように DistributedTransactionAdmin
インスタンスを閉じる必要があります。
admin.close();
名前空間を作成する
テーブルは1つの名前空間に属するため、テーブルを作成する前に名前空間を作成する必要があります。
名前空間は次のように作成できます。
// Create the namespace "ns". If the namespace already exists, an exception will be thrown.
admin.createNamespace("ns");
// Create the namespace only if it does not already exist.
boolean ifNotExists = true;
admin.createNamespace("ns", ifNotExists);
// Create the namespace with options.
Map<String, String> options = ...;
admin.createNamespace("ns", options);
作成オプション
名前空間の作成やテーブルの作成などの作成操作では、オプション名と値のマップであるオプション (Map<String, String>
) を指定できます。オプションを使用すると、ストレージアダプタ固有の設定がで きます。
データベースを選択して、使用可能なオプションを確認します。
- JDBC データベース
- DynamoDB
- Cosmos DB for NoSQL
- Cassandra
JDBC データベースには使用できるオプションはありません。
名前 | 説明 | デフォルト |
---|---|---|
no-scaling | DynamoDB の自動スケーリングを無効にします。 | false |
no-backup | DynamoDB の継続的なバックアップを無効にします。 | false |
ru | 基本リソース単位。 | 10 |
名前 | 説明 | デフォルト |
---|---|---|
ru | 基本リソース単位。 | 400 |
no-scaling | Cosmos DB for NoSQL の自動スケーリングを無効にします。 | false |
名前 | 説明 | デフォルト |
---|---|---|
replication-strategy | Cassandra レプリケーション戦略。SimpleStrategy または NetworkTopologyStrategy である必要があります。 | SimpleStrategy |
compaction-strategy | Cassandra 圧縮戦略。LCS 、STCS 、または TWCS である必要があります。 | STCS |
replication-factor | Cassandra の複製係数。 | 3 |
テーブルを作成する
テーブルを作成するときは、テーブルメタデータを定義してからテーブルを作成する必要があります。
テーブルメタデータを定義するには、TableMetadata
を使用できます。次の図は、テーブルの列、パーティションキー、クラスタリングキー (クラスタリング順序を含む)、およびセカンダリインデックスを定義する方法を示しています。
// Define the table metadata.
TableMetadata tableMetadata =
TableMetadata.newBuilder()
.addColumn("c1", DataType.INT)
.addColumn("c2", DataType.TEXT)
.addColumn("c3", DataType.BIGINT)
.addColumn("c4", DataType.FLOAT)
.addColumn("c5", DataType.DOUBLE)
.addPartitionKey("c1")
.addClusteringKey("c2", Scan.Ordering.Order.DESC)
.addClusteringKey("c3", Scan.Ordering.Order.ASC)
.addSecondaryIndex("c4")
.build();
ScalarDB のデータモデルの詳細については、データモデルを参照してください。
次に、次のようにテーブルを作成します。
// Create the table "ns.tbl". If the table already exists, an exception will be thrown.
admin.createTable("ns", "tbl", tableMetadata);
// Create the table only if it does not already exist.
boolean ifNotExists = true;
admin.createTable("ns", "tbl", tableMetadata, ifNotExists);
// Create the table with options.
Map<String, String> options = ...;
admin.createTable("ns", "tbl", tableMetadata, options);
セカンダリインデックスを作成する
セカンダリインデックスは次のように 作成できます。
// Create a secondary index on column "c5" for table "ns.tbl". If a secondary index already exists, an exception will be thrown.
admin.createIndex("ns", "tbl", "c5");
// Create the secondary index only if it does not already exist.
boolean ifNotExists = true;
admin.createIndex("ns", "tbl", "c5", ifNotExists);
// Create the secondary index with options.
Map<String, String> options = ...;
admin.createIndex("ns", "tbl", "c5", options);
テーブルに新しい列を追加する
次のように、テーブルに新しい非パーティションキー列を追加できます。
// Add a new column "c6" with the INT data type to the table "ns.tbl".
admin.addNewColumnToTable("ns", "tbl", "c6", DataType.INT)
テーブルに新しい列を追加する場合は、基盤となるストレージによって実行時間が大きく異なる可能性があるため、慎重に検討する必要があります。それに応じて計画を立て、特にデータベースが本番環境で実行されている場合は、次の点を考慮してください。
- Cosmos DB for NoSQL および DynamoDB の場合: テーブルスキーマは変更されないため、列の追加はほぼ瞬時に行われます。別のテーブルに格納されているテーブルメタデータのみが更新されます。
- Cassandra の場合: 列を追加すると、スキーマメタデータのみが更新され、既存のスキーマレコードは変更されません。クラスタートポロジが実行 時間の主な要因です。スキーマメタデータの変更は、ゴシッププロトコルを介して各クラスターノードに共有されます。このため、クラスターが大きいほど、すべてのノードが更新されるまでの時間が長くなります。
- リレーショナルデータベース (MySQL、Oracle など) の場合: 列の追加は実行にそれほど時間がかかりません。
テーブルを切り捨てる
テーブルを切り捨てるには、次のようにします。
// Truncate the table "ns.tbl".
admin.truncateTable("ns", "tbl");
セカンダリインデックスを削除する
セカンダリインデックスは次のように削除できます。
// Drop the secondary index on column "c5" from table "ns.tbl". If the secondary index does not exist, an exception will be thrown.
admin.dropIndex("ns", "tbl", "c5");
// Drop the secondary index only if it exists.
boolean ifExists = true;
admin.dropIndex("ns", "tbl", "c5", ifExists);
テーブルを削除する
テーブルを削除するには、次のようにします。
// Drop the table "ns.tbl". If the table does not exist, an exception will be thrown.
admin.dropTable("ns", "tbl");
// Drop the table only if it exists.
boolean ifExists = true;
admin.dropTable("ns", "tbl", ifExists);
名前空間を削除する
名前空間を削除するには、次のようにします。
// Drop the namespace "ns". If the namespace does not exist, an exception will be thrown.
admin.dropNamespace("ns");
// Drop the namespace only if it exists.
boolean ifExists = true;
admin.dropNamespace("ns", ifExists);
既存の名前空間を取得する
既存の名前空間は次のように取得できます。
Set<String> namespaces = admin.getNamespaceNames();
名前空間のテーブルを取得する
名前空間のテーブルは次のように取得できます。
// Get the tables of the namespace "ns".
Set<String> tables = admin.getNamespaceTableNames("ns");