表および索引の削除

Oracle NoSQL Databaseで作成した表または索引を削除する方法について学習します。

表または索引を削除するには、DROP TABLEまたはDROP INDEX DDL文を使用します。たとえば:
/* Drop the table named users */
DROP TABLE users

/* Drop the index called nameIndex on the table users */
DROP INDEX IF EXISTS nameIndex ON users

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

TableRequests.setStatementメソッドを使用して表を削除するには:

/* create the TableRequest to drop the users table */
TableRequest tableRequest = new TableRequest().setStatement("drop table users");

/* start the operation, it is asynchronous */
TableResult tres = handle.tableRequest(tableRequest);

/* wait for completion of the operation */
tres.waitForCompletion(handle,
                        60000,  /* wait for 60 sec */
                        1000);  /* delay in ms for poll */
MR表は、他のOracle NoSQL Database表を削除する場合と同じ方法で、DROP文を使用して削除できます。特定のリージョンのMR表を削除することを選択した場合、参加している他のリージョンのMR表はそのまま残ります。特定のMR表を複数リージョンから削除する場合は、各リージョンでDROP TABLE文を個別に実行する必要があります。

ノート:

1つを除くすべてのリージョンのMR表を削除すると、それは単一のリージョンにリンクされたMR表になります。単一リージョンを持つMR表とローカル表との違いは、単一リージョンを持つMR表に将来的に新しいリージョンを追加できることです。
アプリケーションから表または索引を削除するには、TableRequestクラスを使用します。たとえば、users表を削除するには、次のようにします。
/* Drop the table identified by the tableName */
final String dropStatement = "drop table " + users; 

/* Pass the dropStatement string to the TableRequest.setStatement method */
TableRequest tableRequest = new TableRequest().setStatement(dropStatement);

/* Wait for the table state to change to DROPPED. */
TableResult tres = handle.tableRequest(tableRequest);
tres = TableResult.waitForState(handle,tres.getTableName(),
       TableResult.State.DROPPED,
                           30000, /* wait 30 sec */
                           1000); /* delay ms for poll */   
次の例では、表のユーザーを削除します。
from borneo import TableRequest 
# the drop statement 
statement = 'drop table users'
request = TableRequest().set_statement(statement) 
# perform the operation, wait for 40 seconds, polling every 3 seconds 
result = handle.do_table_request(request, 40000, 3000)
次の例では、指定された表を削除します。
// Drop the table
dropReq := &nosqldb.TableRequest{Statement: "DROP TABLE IF EXISTS " + tableName}
tableRes, err = client.DoTableRequestAndWait(dropReq, 60*time.Second, time.Second)
if err != nil {
    fmt.Printf("failed to drop table: %v\n", err)
    return
}
fmt.Println("Dropped table " + tableName)
次の例では、指定された表および索引を削除します。
const NoSQLClient = require('oracle-nosqldb').NoSQLClient;
const TableState = require('oracle-nosqldb').TableState;
.....
const client = new NoSQLClient('config.json');

async function dropNameIndexUsersTable() {
    try {
        let result = await client.tableDDL('DROP INDEX nameIndex ON users');
        // Before using the table again, wait for the operation completion
        // (when the table state changes from UPDATING to ACTIVE)
        await client.forCompletion(result);
        console.log('Index dropped');
    } catch(error) {
        //handle errors
    }
}

async function dropTableUsers() {
    try {
        // Here we are waiting until the drop table operation is completed
        // in the underlying store
        let result = await client.tableDDL('DROP TABLE users', {
            completion: true
        });
        console.log('Table dropped');
    } catch(error) {
        //handle errors
    }
}
表を削除するには、ExecuteTableDDLAsyncおよびExecuteTableDDLWithCompletionAsyncを使用します。
var client = new NoSQLClient("config.json");
try {
    // Drop index "nameIndex" on table "users".
    var result = await client.ExecuteTableDDLAsync(
        "DROP INDEX nameIndex ON users");
    // The following may print: Table state is Updating.
    Console.WriteLine("Table state is {0}", result.TableState);
    await result.WaitForCompletionAsync();    
    // Expected output: Table state is Active.
    Console.WriteLine("Table state is {0}.", result.TableState);
    // Drop table "TestTable".
    result = await client.ExecuteTableDDLWithCompletionAsync(
        "DROP TABLE TestTable");
    // Expected output: Table state is Dropped.
    Console.WriteLine("Table state is {0}.", result.TableState);    
}
catch(Exception ex) {
    // handle exceptions
}

表および索引を削除するには、NosqlTemplate.runTableRequest()またはNosqlTemplate.dropTableIfExists()メソッドを使用します。詳細は、SDK for Spring Data APIリファレンスを参照してください。

AbstractNosqlConfigurationクラスを拡張するAppConfigクラスを作成し、データベースの接続詳細を指定します。詳細は、「NoSQL接続の取得」を参照してください。

アプリケーションでは、AppConfigクラスのインスタンスにNosqlTemplate create(NosqlDbConfig nosqlDBConfig)メソッドを指定して、NosqlTemplateクラスをインスタンス化します。その後、NosqlTemplate.dropTableIfExists()メソッドを使用して表を削除します。NosqlTemplate.dropTableIfExists()メソッドは表を削除し、結果が表の状態がDROPPEDまたはDROPPINGに変更されたことを示す場合はtrueを返します。
import com.oracle.nosql.spring.data.core.NosqlTemplate;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
 
/* Drop the Users table.*/
 
try {
    AppConfig config = new AppConfig();
    NosqlTemplate tabledrop = NosqlTemplate.create(config.nosqlDbConfig());
    Boolean result = tabledrop.dropTableIfExists("Users");
    if (result == true) {
        System.out.println("Table dropped successfully");
    } else {
        System.out.println("Failed to drop table");
    }
} catch (Exception e) {
    System.out.println("Exception creating index" + e);
}