In questa pagina viene descritto come inserire, aggiornare ed eliminare i dati utilizzando le mutazioni. R rappresenta una sequenza di inserimenti, aggiornamenti ed eliminazioni Spanner si applica atomicamente a righe e tabelle diverse in un il database Spanner.
Sebbene sia possibile commettere mutazioni utilizzando gRPC o REST, di accedere alle API mediante le librerie client.
Questa pagina mostra le attività di base di inserimento, aggiornamento ed eliminazione. Puoi visualizzare altri esempi nei tutorial introduttivi.
Se devi eseguire il commit di un numero elevato di scritture cieche, ma non è necessario una transazione atomica, puoi modificare collettivamente le tabelle Spanner utilizzando la scrittura in batch. Per ulteriori informazioni, consulta Modificare i dati utilizzando le scritture collettive.
Inserire nuove righe in una tabella
C++
Puoi scrivere i dati utilizzando la funzione InsertMutationBuilder()
.
Client::Commit()
aggiunge nuove righe a una tabella. Tutti gli inserti in
di un singolo batch sono applicati a livello atomico.
Questo codice mostra come scrivere i dati:
C#
Puoi inserire i dati utilizzando
connection.CreateInsertCommand()
, che
crea un nuovo SpannerCommand
per inserire righe in una tabella. Il metodo
SpannerCommand.ExecuteNonQueryAsync()
aggiunge nuove
riga alla tabella.
Questo codice mostra come inserire i dati:
Vai
Scrivi i dati utilizzando un
Mutation
Un Mutation
è un contenitore per le operazioni di mutazione. Un Mutation
rappresenta una sequenza di
inserti, aggiornamenti, eliminazioni e così via che possono essere applicati atomicamente
di righe e tabelle diverse in un database Spanner.
Utilizza Mutation.InsertOrUpdate()
per creare una mutazione INSERT_OR_UPDATE
, che aggiunge una nuova riga o aggiornamenti
i valori della colonna se la riga esiste già. In alternativa, utilizza
Mutation.Insert()
per creare una mutazione INSERT
, che aggiunge una nuova riga.
Si applica Client.Apply()
mutazioni a livello atomico in un database.
Questo codice mostra come scrivere i dati:
Java
Scrivi i dati utilizzando un oggetto Mutation
. Un Mutation
è un container per le operazioni di mutazione. Un Mutation
rappresenta una
sequenza di inserimenti, aggiornamenti ed eliminazioni che Spanner applica
atomicamente a righe e tabelle diverse in un database Spanner.
Il metodo newInsertBuilder()
in Mutation
genera una mutazione INSERT
, che inserisce una nuova
riga in una tabella. Se la riga esiste già, la scrittura non riesce. In alternativa,
puoi usare il metodo newInsertOrUpdateBuilder
per costruire una mutazione INSERT_OR_UPDATE
, che
aggiorna i valori della colonna se la riga esiste già.
Il metodo write()
nella classe DatabaseClient
scrive
le mutazioni. Tutte le mutazioni in un singolo batch vengono applicate a livello atomico.
Questo codice mostra come scrivere i dati:
Node.js
Scrivi i dati utilizzando un
Table
. La
Table.insert()
aggiunge nuove righe alla tabella. Vengono applicati tutti gli inserimenti di un singolo batch
a livello atomico.
Questo codice mostra come scrivere i dati:
PHP
Scrivi i dati utilizzando
Database::insertBatch
. insertBatch
aggiunge nuove righe a una tabella. Tutti gli inserimenti in un singolo
batch vengono applicati in modo atomico.
Questo codice mostra come scrivere i dati:
Python
Scrivi i dati utilizzando un oggetto Batch
. Un oggetto Batch
è
un container per le operazioni di mutazione. Una mutazione rappresenta una sequenza di
inserti, aggiornamenti, eliminazioni e così via che possono essere applicati atomicamente
di righe e tabelle diverse in un database Spanner.
Il metodo insert()
nella classe Batch
viene utilizzato per
aggiungi una o più mutazioni di inserimento al batch. Tutte le mutazioni in un singolo
vengono applicati a livello atomico.
Questo codice mostra come scrivere i dati:
Ruby
Scrivi i dati utilizzando un
Client
. La
Client#commit
crea ed esegue il commit di una transazione per le scritture eseguite atomicamente
in un singolo momento logico nel tempo tra colonne, righe e tabelle in un
per configurare un database.
Questo codice mostra come scrivere i dati:
Aggiornare le righe di una tabella
Supponiamo che le vendite di Albums(1, 1)
siano inferiori al previsto. Di conseguenza,
vuoi trasferire 200.000 $dal budget di marketing di Albums(2, 2)
a
Albums(1, 1)
, ma solo se il denaro è disponibile nel budget di
Albums(2, 2)
.
Poiché devi leggere i dati nelle tabelle per determinare se scrivere nuovi valori, dovresti utilizzare un'interfaccia di lettura/scrittura transazione per eseguire le letture e scrive a livello atomico.
C++
Utilizza la funzione Transaction()
per eseguire una transazione per un client.
Ecco il codice per eseguire la transazione:
C#
Per .NET Standard 2.0 (o .NET 4.5) e versioni successive, è possibile usare la classe
TransactionScope()
per eseguire una transazione. Per tutte le opzioni supportate
di .NET, puoi creare una transazione impostando il risultato
SpannerConnection.BeginTransactionAsync
come proprietà Transaction
di
SpannerCommand
.
Ecco i due modi per eseguire la transazione:
Standard .NET 2.0
Standard .NET 1.5
Vai
Utilizza il tipo ReadWriteTransaction
per eseguire un insieme di attività nel contesto di una transazione di lettura/scrittura.
Client.ReadWriteTransaction()
restituisce un oggetto ReadWriteTransaction
.
L'esempio utilizza ReadWriteTransaction.ReadRow()
per recuperare una riga di dati.
L'esempio utilizza anche ReadWriteTransaction.BufferWrite()
,
che aggiunge un elenco di mutazioni all'insieme di aggiornamenti che verranno applicati
al momento del commit della transazione.
Il sample utilizza anche il tipo Key
, che rappresenta una chiave di riga in un indice o una tabella Spanner.
Java
Utilizza l'interfaccia TransactionRunner
per l'esecuzione
un insieme di lavori nel contesto di una transazione di lettura/scrittura. Questa interfaccia
contiene il metodo run()
, utilizzato per eseguire un comando
e scrivere la transazione, con nuovi tentativi se necessario. La
Metodo readWriteTransaction
di DatabaseClient
restituisce un oggetto TransactionRunner
per l'esecuzione di una singola logica
transazione.
La classe TransactionRunner.TransactionCallable
contiene un metodo run()
per eseguire un singolo tentativo di transazione. run()
richiede un
TransactionContext
, che è un contesto
per una transazione.
L'esempio utilizza la classe Struct
, utile per
per archiviare i risultati delle chiamate readRow()
. L'esempio utilizza anche
Key
, che rappresenta una chiave di riga in una chiave Spanner
tabella o indice.
Ecco il codice per eseguire la transazione:
Node.js
Utilizza Database.runTransaction()
per eseguire una transazione.
Ecco il codice per eseguire la transazione:
PHP
Utilizza Database::runTransaction
per eseguire una transazione.
Ecco il codice per eseguire la transazione:
Python
Utilizza la run_in_transaction()
di Database
per eseguire una transazione.
Ecco il codice per eseguire la transazione:
Ruby
Utilizza la transaction
di Client
per eseguire una transazione.
Ecco il codice per eseguire la transazione:
Eliminare righe in una tabella
Ogni libreria client fornisce diversi modi per eliminare le righe:
- Elimina tutte le righe di una tabella.
- Elimina una singola riga specificando i valori della colonna chiave per la riga.
- Elimina un gruppo di righe creando un intervallo di chiavi.
- Elimina le righe in una tabella con interleaving eliminando le righe padre, se
la tabella con interfoliazione include
ON DELETE CASCADE
nella definizione dello schema.
C++
Elimina le righe utilizzando la funzione DeleteMutationBuilder()
per un client.
Questo codice mostra come eliminare i dati:
C#
Elimina le righe utilizzando
connection.CreateDeleteCommand()
, che
crea un nuovo SpannerCommand
per eliminare le righe. Il metodo SpannerCommand.ExecuteNonQueryAsync()
elimina le righe dalla tabella.
In questo esempio vengono eliminate singolarmente le righe nella tabella Singers
. Le righe
nella tabella Albums
vengono eliminate perché la tabella Albums
è con interleaving
nella tabella Singers
ed è definita con ON DELETE CASCADE
.
Vai
Elimina le righe utilizzando un elemento Mutation
. Utilizza la
Mutation.Delete()
per creare una mutazione DELETE
,
che elimina una riga. Si applica il metodo Client.Apply()
mutazioni a livello atomico
nel database.
Questo esempio elimina le righe della tabella Albums
singolarmente, quindi elimina tutte le righe della tabella Singers
utilizzando un intervallo di chiavi.
Java
Elimina le righe utilizzando il metodo Mutation.delete()
.
In questo esempio viene utilizzato il metodo KeySet.all()
per eliminare tutti
le righe della tabella Albums
. Dopo aver eliminato le righe in Albums
, l'esempio elimina singolarmente le righe nella tabella Singers
utilizzando chiavi create con il metodo KeySet.singleKey()
.
Node.js
Elimina le righe utilizzando il metodo table.deleteRows()
.
In questo esempio viene utilizzato il metodo table.deleteRows()
per eliminare tutte le righe
dalla tabella Singers
. Le righe nella tabella Albums
sono state eliminate perché
la tabella Albums
è con interleaving nella tabella Singers
ed è definita con ON
DELETE CASCADE
.
PHP
Elimina le righe utilizzando Database::delete() method
. La
La pagina Metodo Database::delete()
include un esempio.
Python
Elimina le righe utilizzando il metodo Batch.delete()
.
Questo esempio elimina tutte le righe nelle tabelle Albums
e Singers
singolarmente utilizzando un oggetto KeySet
.
Ruby
Elimina le righe utilizzando il metodo Client#delete
. La pagina
Client#delete
include un esempio.