使用 API 修改单例表

了解如何使用 API 修改单例表。

可以将单例表修改为:

使用 TableRequest API 更改表

可以使用 TableRequest API 更改 NoSQL 表的定义。

TableRequest 类用于修改表。此请求指定的操作的执行是异步的。这些操作可能是长时间运行的操作。TableResult 是从 TableRequest 操作返回的,它将封装表的状态。有关 TableRequest 类及其方法的更多详细信息,请参阅 Oracle NoSQL Java SDK API Reference

Download the full code AlterTable.java from the examples here.

/**
* Alter the table stream_acct and add a column
*/
private static void alterTab(NoSQLHandle handle) throws Exception {
   String alterTableDDL = "ALTER TABLE " + tableName +"(ADD acctname STRING)";
   TableRequest treq = new TableRequest().setStatement(alterTableDDL);
   System.out.println("Altering table " + tableName);
   TableResult tres = handle.tableRequest(treq);
   tres.waitForCompletion(handle, 60000, /* wait 60 sec */
   1000); /* delay ms for poll */
   System.out.println("Table " + tableName + " is altered");
}

borneo.TableRequest 类用于修改表。对 borneo.NoSQLHandle.table_request() 的所有调用都是异步的,因此需要检查结果并调用 borneo.TableResult.wait_for_completion() 以等待操作完成。有关 table_request 及其方法的更多详细信息,请参阅 Oracle NoSQL Python SDK API Reference

Download the full code AlterTable.py from the examples here.

def alter_table(handle):
   statement = '''ALTER TABLE  stream_acct(ADD acctname STRING)'''
   request = TableRequest().set_statement(statement)
   table_result = handle.do_table_request(request, 40000, 3000)
   table_result.wait_for_completion(handle, 40000, 3000)
   print('Table stream_acct is altered')

TableRequest 类用于修改表。TableRequest 指定的操作的执行是异步的。这些操作可能是长时间运行的操作。此请求用作 Client.DoTableRequest() 操作的输入,该操作将返回可用于轮询的 TableResult,直到表达到所需状态。有关 TableRequest 类的各种方法的更多详细信息,请参阅 Oracle NoSQL Go SDK API Reference

Download the full code AlterTable.go from the examples here.

//alter an existing table and add a column
func alterTable(client *nosqldb.Client, err error, tableName string)(){
   stmt := fmt.Sprintf("ALTER TABLE %s (ADD acctName STRING)",tableName)
   tableReq := &nosqldb.TableRequest{
		Statement: stmt,
   }
   tableRes, err := client.DoTableRequest(tableReq)
   if err != nil {
      fmt.Printf("cannot initiate ALTER TABLE request: %v\n", err)
      return
   }
   // The alter table request is asynchronous, wait for table alteration to complete.
   _, err = tableRes.WaitForCompletion(client, 60*time.Second, time.Second)
   if err != nil {
      fmt.Printf("Error finishing ALTER TABLE request: %v\n", err)
      return
   }
   fmt.Println("Altered table ", tableName)
   return
}

可以使用执行 tableDDL 方法修改表。此方法为异步方法,返回 Promise of TableResultTableResult 是一个普通的 JavaScript 对象,在执行 DDL 操作后封装表的状态。有关方法详细信息,请参见 NoSQLClient 类。

Download the full JavaScript code AlterTable.js from the examples here and the full TypeScript code AlterTable.ts from the examples here.

//alter a table and add a column
async function alterTable(handle) {
   const alterDDL = `ALTER TABLE ${TABLE_NAME} (ADD acctname STRING)`;
   let res =  await handle.tableDDL(alterDDL);
   console.log('Table altered: ' + TABLE_NAME);
}

可以使用两种方法之一 ExecuteTableDDLAsyncExecuteTableDDLWithCompletionAsync 来修改表。这两种方法都返回 Task<TableResult>TableResult 实例在执行 DDL 操作后封装表的状态。有关这些方法的更多详细信息,请参阅 Oracle NoSQL Dotnet SDK API Reference

Download the full code AlterTable.cs from the examples here.

private static async Task alterTable(NoSQLClient client){
   var sql = $@"ALTER TABLE {TableName}(ADD acctname STRING)";
   var tableResult = await client.ExecuteTableDDLAsync(sql);
   // Wait for the operation completion
   await tableResult.WaitForCompletionAsync();
   Console.WriteLine("  Table {0} is altered", tableResult.TableName);
}

相关主题