Modifica delle tabelle singleton mediante le API
Scopri come modificare una tabella singleton utilizzando le API.
Modificare una tabella singleton in:
-
Aggiungi nuovi campi a una tabella esistente
Esempio:
String alterTableDDL = ALTER TABLE <table name> (ADD age INTEGER); TableRequest treq = new TableRequest().setStatement(alterTableDDL); -
Elimina i campi esistenti in una tabella
Esempio:
String alterTableDDL = ALTER TABLE <table name> (DROP age); TableRequest treq = new TableRequest().setStatement(alterTableDDL); -
Per modificare il valore TTL predefinito
Esempio:
String alterTableDDL = ALTER TABLE <table name> USING TTL 7 DAYS; TableRequest treq = new TableRequest().setStatement(alterTableDDL); -
Modifica limiti tabella
Esempio:
TableLimits newLimits = new TableLimits(10, 10, 1); TableRequest treq = new TableRequest().setTableName(tableName).setTableLimits(newLimits); -
Congela lo schema della tabella
Esempio: utilizzare congela schema se la tabella contiene colonne JSON
String alterTableDDL = ALTER TABLE <table name> freeze schema; TableRequest treq = new TableRequest().setStatement(alterTableDDL);Esempio: utilizzare congela forza schema se la tabella non contiene colonne JSON
String alterTableDDL = ALTER TABLE <table name> freeze schema force; TableRequest treq = new TableRequest().setStatement(alterTableDDL); -
Scongelare lo schema della tabella
Esempio:
String alterTableDDL = ALTER TABLE <table name> unfreeze schema; TableRequest treq = new TableRequest().setStatement(alterTableDDL);
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 lunghe. TableResult viene restituito dalle operazioni TableRequest e incapsula lo stato della tabella. Per ulteriori dettagli sulla classe TableRequest e sui relativi metodi, consultare Oracle NoSQL Java SDK API Reference.
Scarica il codice completo AlterTable.java dagli esempi qui.
/**
* 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 quindi è necessario controllare il risultato e chiamare borneo.TableResult.wait_for_completion() per attendere il completamento dell'operazione. Per ulteriori dettagli su table_request e sui relativi metodi, consultare Oracle NoSQL Python SDK API Reference.
Scarica il codice completo AlterTable.py dagli esempi qui.
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 lunghe. 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, vedere Oracle NoSQL Go SDK API Reference.
Scarica il codice completo AlterTable.go dagli esempi qui.
//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 oggetto JavaScript normale che incapsula lo stato della tabella dopo l'operazione DDL. Per i dettagli del metodo, vedere la classe NoSQLClient.
Scarica il codice JavaScript completo AlterTable.js dagli esempi riportati qui e il codice TypeScript completo AlterTable.ts dagli esempi riportati qui.
//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 la riferimento API Oracle NoSQL Dotnet SDK.
Scaricare il codice completo AlterTable.cs dagli esempi qui.
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);
}