表の変更

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

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

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

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

  • デフォルトの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

次の例では、NoSQLハンドルの取得時に、デフォルトのコンパートメントがNoSQLHandleConfigに指定されていると想定しています。「NoSQLハンドルの取得」を参照してください。NoSQL表のコンパートメントを指定する他のオプションを調べるには、コンパートメントに関する項を参照してください。

プロビジョニングされた容量を持つ表を変更する場合は、TableRequests.setTableLimitsメソッドを使用して表の制限を変更することもできます。
TableLimits limits = new TableLimits(40, 10, 5);
TableRequest treq = new TableRequest().setTableName( "users" ).setTableLimits(limits);
TableResult tres = handle.tableRequest(treq);
/* wait for completion of the operation */ tres.waitForCompletion(handle, 60000,
/* wait for 60 sec */
1000);
また、Oracle NoSQL Database Java SDKを使用して、表を変更し、容量モデルをオンデマンド容量構成に変更することもできます。ストレージ容量を変更することもできます。
// Previous limit in Provisioned Mode
// TableLimits limits = new TableLimits(40, 10, 5);    
// Call the constructor to only set storage limit (for on-demand)    
TableLimits limits = newTableLimits(10);    
TableRequest treq = newTableRequest().setTableName("users").setTableLimits(limits);    
TableResult tres = serviceHandle.tableRequest(treq);    
tres.waitForCompletion(serviceHandle, 50000,3000);
表の定義を変更することもできます。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 */
Oracle NoSQL Database Cloud Serviceを使用する場合は、表の制限はborneo.TableRequest.set_table_limits()を使用して変更できます。表にプロビジョニングされた容量が構成されている場合、次の例に示すように制限を設定できます。
from borneo import TableLimits, TableRequest
# in this path the table name is required, as there is no DDL statement 
request = TableRequest().set_table_name('users')
request.set_table_limits(TableLimits(40, 10, 5))
result = handle.table_request(request)
# table_request is asynchronous, so wait for the operation to complete, 
# wait for 40 seconds, polling every 3 seconds 
result.wait_for_completion(handle, 40000, 3000)
また、Oracle NoSQL Database Python SDKを使用して、表を変更し、容量モデルをオンデマンド容量構成に変更することもできます。ストレージ容量を変更することもできます。
from borneo import TableLimits, TableRequest
# in this path the table name is required, as there is no DDL statement
request = TableRequest().set_table_name('users')
request.set_table_limits(TableLimits(10))
result = handle.table_request(request)
# table_request is asynchronous, so wait for the operation to complete,
# wait for 40 seconds, polling every 3 seconds
result.wait_for_completion(handle, 40000, 3000)
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)
Oracle NoSQL Database Cloud Serviceの表制限は、TableRequest.TableLimitsを使用して変更できます。表にプロビジョニングされた容量が構成されている場合、次の例に示すように制限を設定できます。
req := &nosqldb.TableRequest{
    TableName: "users",
    TableLimits: &nosqldb.TableLimits{
        ReadUnits: 100,
        WriteUnits: 100,
        StorageGB: 5,
    },
}
res, err := client.DoTableRequestAndWait(req, 5*time.Second, time.Second)
また、Oracle NoSQL Database Go SDKを使用して、表を変更し、容量モデルをオンデマンド容量構成に変更することもできます。ストレージ容量を変更することもできます。
req := &nosqldb.TableRequest{ TableName: "users",
TableLimits: &nosqldb.TableLimits{StorageGB: 10}}
res, err := client.DoTableRequestAndWait(req, 5*time.Second, time.Second)
NoSQLClient#tableDDLを使用し、この表に対してDDL文を発行して表を変更します。表の制限は、setTableLimitsメソッドを使用して変更できます。表名と新しいTableLimitsを引数として使用し、TableResultのPromiseを返します。表にプロビジョニングされた容量が構成されている場合、次の例に示すように制限を設定できます。
const NoSQLClient = require('oracle-nosqldb').NoSQLClient;
const TableState = require('oracle-nosqldb').TableState;
const client = new NoSQLClient('config.json');

async function modifyUsersTableLimits() {
    const tableName = 'users';
    try {
        let result = await client.setTableLimits(tableName, {
            readUnits: 40,
            writeUnits: 10,
            storageGB: 5
        });
        // Wait for the operation completion using specified timeout and
        // specified polling interval (delay)
        await client.forCompletion(result, TableState.ACTIVE, {
            timeout: 30000,
            delay: 2000
        });
        console.log('Table limits modified');
    } catch(error) {
        //handle errors
    }
}
また、Oracle NoSQL Database Node.js SDKを使用して、表を変更し、容量モデルをオンデマンド容量構成に変更することもできます。ストレージ容量を変更することもできます。
const NoSQLClient = require('oracle-nosqldb').NoSQLClient;
const TableState = require('oracle-nosqldb').TableState;
const client = new NoSQLClient('config.json');
async function modifyUsersTableLimits() {
const tableName = 'users';
  try {
     let result = await client.setTableLimits(tableName, {storageGB: 10 });
     // Wait for the operation completion using specified timeout and polling interval (delay)
     await client.forCompletion(result, TableState.ACTIVE, { timeout: 30000, delay: 2000 });
     console.log('Table limits modified');
  } 
  catch(error) { //handle errors } 
}
ExecuteTableDDLAsyncまたはExecuteTableDDLWithCompletionAsyncを使用し、この表に対してDDL文を発行して表を変更します。表の制限は、SetTableLimitsAsyncまたはSetTableLimitsWithCompletionAsyncメソッドを使用して変更できます。表名および新しいTableLimitsをパラメータとして取得し、Task<TableResult>を返します。表にプロビジョニングされた容量が構成されている場合、次の例に示すように制限を設定できます。
var client = new NoSQLClient("config.json");
var tableName = "users";
try {
    var result = await client.SetTableLimitsWithCompletionAsync(
       tableName, new TableLimits(40, 10, 5));
    // Expected output: Table state is Active.
    Console.WriteLine("Table state is {0}.", result.TableState);
    Console.WriteLine("Table limits have been changed");
}
catch(Exception ex) {
    // handle exceptions
}
また、Oracle NoSQL Database .NET SDKを使用して、表を変更し、容量モデルをオンデマンド容量構成に変更することもできます。ストレージ容量を変更することもできます。
var client = new NoSQLClient("config.json");
var tableName = "users";
try {
   var result = await client.SetTableLimitsWithCompletionAsync( tableName, new TableLimits(10));
   // Expected output: Table state is Active.
   Console.WriteLine("Table state is {0}.", result.TableState);
   Console.WriteLine("Table limits have been changed");
}
catch(Exception ex) { // handle exceptions }