Modifica delle tabelle Singleton mediante le API
Informazioni su come modificare una tabella singola utilizzando le API.
- Aggiungi nuovi campi a una tabella esistente
- Elimina campi esistenti in una tabella
- Per cambiare il valore TTL predefinito
- Modifica limiti della tabella
Argomenti correlati
Utilizzo dell'API TableRequest per modificare la tabella
È possibile utilizzare l'API TableRequest per modificare la definizione di una tabella NoSQL.
La classe TableRequest
viene utilizzata per modificare le tabelle. L'esecuzione delle operazioni specificate da questa richiesta è asincrona. Si tratta di operazioni potenzialmente di lunga durata. TableResult
viene restituito dalle operazioni TableRequest
e incapsula lo stato della tabella. Per ulteriori dettagli sulla classe TableRequest
e sui relativi metodi, vedere 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");
}
La classe borneo.TableRequest
viene utilizzata per modificare le tabelle. Tutte le chiamate a borneo.NoSQLHandle.table_request()
sono asincrone, pertanto è necessario controllare il risultato e chiamare borneo.TableResult.wait_for_completion()
per attendere il completamento dell'operazione. Per ulteriori informazioni su table_request
e sui relativi metodi, vedere 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')
La classe TableRequest
viene utilizzata per modificare le tabelle. L'esecuzione delle operazioni specificate da TableRequest
è asincrona. Si tratta di operazioni potenzialmente di lunga durata. Questa richiesta viene utilizzata come input di un'operazione Client.DoTableRequest()
, che restituisce un valore TableResult
che può essere utilizzato per eseguire il polling fino a quando la tabella non raggiunge lo stato desiderato. Per ulteriori dettagli sui vari metodi della classe TableRequest
, consulta Oracle NoSQL Go SDK API Reference.
//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
}
È possibile utilizzare il metodo tableDDL
per modificare una tabella. Questo metodo è asincrono e restituisce una promessa di TableResult
. TableResult
è un semplice oggetto JavaScript che incapsula lo stato della tabella dopo l'operazione DDL. Per ulteriori informazioni sul metodo, vedere la classe 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);
}
Per modificare una tabella è possibile utilizzare uno dei due metodi ExecuteTableDDLAsync
e ExecuteTableDDLWithCompletionAsync
. Entrambi i metodi restituiscono Task<TableResult>
. L'istanza TableResult
incapsula lo stato della tabella dopo l'operazione DDL. Per ulteriori dettagli su questi metodi, consulta Oracle NoSQL Riferimento API SDK Dotnet.
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);
}
Modifica delle tabelle Singleton mediante le API