使用 API 修改单例表
使用 TableRequest API 更改表
可以使用 TableRequest API 更改 NoSQL 表的定义。
TableRequest
类用于修改表。执行此请求指定的操作是异步的。这些可能是长时间运行的操作。TableResult
从 TableRequest
操作返回,它封装表的状态。有关 TableRequest
类及其方法的更多详细信息,请参阅 Oracle NoSQL Java SDK API Reference 。
/**
* 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 。
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')
The TableRequest
class is used to modify tables. Execution of operations specified by TableRequest
is asynchronous. These are potentially long-running operations. This request is used as the input of a Client.DoTableRequest()
operation, which returns a TableResult
that can be used to poll until the table reaches the desired state. See Oracle NoSQL Go SDK API Reference for more details on the various methods of the TableRequest
class.
//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 TableResult
。TableResult
是一个简单的 JavaScript 对象,用于封装 DDL 操作后表的状态。有关方法详细信息,请参见 NoSQLClient 类。
//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);
}
可以使用 ExecuteTableDDLAsync
和 ExecuteTableDDLWithCompletionAsync
这两种方法之一修改表。这两个方法均返回 Task<TableResult>
。TableResult
实例封装 DDL 操作后表的状态。有关这些方法的更多详细信息,请参阅 Oracle NoSQL Dotnet SDK API Reference 。
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);
}