使用 API 修改單一表格

瞭解如何使用 API 修改單一表格。

您可以將單一表格修改為:
  • 新增欄位至現有表格
  • 刪除表格中目前存在的欄位
  • 變更預設 TTL 值
  • 修改表格限制

使用 TableRequest API 更改表格

您可以使用 TableRequest API 變更 NoSQL 表格的定義。

TableRequest 類別是用來修改表格。此要求所指定的作業執行非同步。這些作業可能會長時間執行。TableResult 會從 TableRequest 作業傳回,並且會封裝表格的狀態。請參閱 Oracle NoSQL Java SDK API Reference ,瞭解 TableRequest 類別及其方法的詳細資訊。

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() 以等待作業完成。請參閱 Oracle NoSQL Python SDK API Reference ,瞭解 table_request 及其方法的詳細資訊。

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,直到表格達到所需的狀態為止。請參閱 Oracle NoSQL Go SDK API Reference ,瞭解 TableRequest 類別各種方法的詳細資訊。

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 方法來修改表格。此方法為非同步,而且會傳回 TableResult 的 Promise。TableResult 是純 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);
}