表の変更

表の変更方法について学習します。

表は次のように変更できます。

  • 既存の表に新規フィールドを追加する

  • 表の既存のフィールドを削除する

  • デフォルトのTTL値を変更する

DDL文の例を次に示します。
/* Add a new field to the table */
ALTER TABLE users (ADD age INTEGER)

/* Drop an existing field from the table */
ALTER TABLE users (DROP age)

/* Modify the default TTL value*/
ALTER TABLE users USING TTL 4 days
表の定義を変更できます。TTL値は次のように変更されます。
/* Alter the users table to modify the TTL value to 4 days. 
* When modifying the table schema or other table state you cannot also 
* modify the table limits. These must be independent operations. 
*/
String alterTableDDL = "ALTER TABLE users " + "USING TTL 4 days";
TableRequest treq = new TableRequest().setStatement(alterTableDDL); 
/* start the operation, it is asynchronous */
TableResult tres = handle.tableRequest(treq);
/* wait for completion of the operation */ 
tres.waitForCompletion(handle, 60000, 1000);
/* wait for 60 sec */
/* delay in ms for poll */
次に示すように、表のTTL値を変更できます。
/* Alter the users table to modify the TTL value to 4 days. 
* When modifying the table schema or other table state you cannot also 
* modify the table limits. These must be independent operations. 
*/
TableRequest statement = "ALTER TABLE users " + "USING TTL 4 days";
request = TableRequest().set_statement(statement)
# assume that a handle has been created, as handle, make the request 
#wait for 60 seconds, polling every 1 seconds
result = handle.do_table_request(request, 60000, 1000) 
result.wait_for_completion(handle, 60000, 1000)
DDL文およびその他の情報をTableRequestに指定し、nosqldb.DoTableRequest()またはnosqldb.DoTableRequestAndWait()関数を使用してリクエストを実行します。
req:=&nosqldb.TableRequest{
    Statement: "ALTER TABLE users (ADD age INTEGER)",
}
res, err:=client.DoTableRequestAndWait(req, 5*time.Second, time.Second)
次に示すように、表のTTL値を変更できます。
/* Alter the users table to modify the TTL value to 4 days. 
*/
const statement = 'ALTER TABLE users ' + 'USING TTL 4 days';
let result = await client.tableDDL(statement, complete: true });
console.log('Table users altered');

ExecuteTableDDLAsyncまたはExecuteTableDDLWithCompletionAsyncを使用し、この表に対してDDL文を発行して表を変更します。

次に示すように、表のTTL値を変更できます。
/* Alter the users table to modify the TTL value to 4 days. 
*/
var statement = "ALTER TABLE users " + "USING TTL 4 days";
var result = await client.ExecuteTableDDLAsync(statement);
await result.WaitForCompletionAsync(); 
Console.WriteLine("Table users altered."); 
表を変更するには、NosqlTemplate.runTableRequest()メソッドを使用します。詳細は、SDK for Spring Data APIリファレンスを参照してください。

ノート:

Spring Data SDKでは表を変更するオプションが提供されていますが、Spring Data SDKでは表がデフォルト・スキーマ(String、int、longまたはtimestamp型の主キー列と、kv_json_と呼ばれるJSON列の2つの列)に準拠していることを想定しているため、スキーマを変更することはお薦めしません。