Insert graph data with DML
Stay organized with collections
Save and categorize content based on your preferences.
Insert data into a Spanner Graph using DML.
Explore further
For detailed documentation that includes this code sample, see the following:
Code sample
C++
To learn how to install and use the client library for Spanner, see Spanner client libraries.
To authenticate to Spanner, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
voidInsertDataWithDml(google::cloud::spanner::Clientclient){
using::google::cloud::StatusOr;
namespacespanner=::google::cloud::spanner;
std::int64_trows_inserted;
autocommit_result=client.Commit(
[&client,&rows_inserted](
spanner::Transactiontxn)->StatusOr<spanner::Mutations>{
autoinsert=
client.ExecuteDml(std::move(txn),spanner::SqlStatement(R"""(
INSERT INTO Account (id, create_time, is_blocked)
VALUES
(1, CAST('2000-08-10 08:18:48.463959-07:52' AS TIMESTAMP), false),
(2, CAST('2000-08-12 07:13:16.463959-03:41' AS TIMESTAMP), true)
)"""));
if(!insert)returnstd::move(insert).status();
rows_inserted=insert->RowsModified();
returnspanner::Mutations{};
});
if(!commit_result)throwstd::move(commit_result).status();
std::cout << "Rows inserted into Account: " << rows_inserted << "\n";
commit_result=client.Commit(
[&client,&rows_inserted](
spanner::Transactiontxn)->StatusOr<spanner::Mutations>{
autoinsert=
client.ExecuteDml(std::move(txn),spanner::SqlStatement(R"""(
INSERT INTO AccountTransferAccount (id, to_id, create_time, amount)
VALUES
(1, 2, CAST('2000-09-11 03:11:18.463959-06:36' AS TIMESTAMP), 100),
(1, 1, CAST('2000-09-12 04:09:34.463959-05:12' AS TIMESTAMP), 200)
)"""));
if(!insert)returnstd::move(insert).status();
rows_inserted=insert->RowsModified();
returnspanner::Mutations{};
});
if(!commit_result)throwstd::move(commit_result).status();
std::cout << "Rows inserted into AccountTransferAccount: " << rows_inserted
<< "\n";
std::cout << "Insert was successful [spanner_insert_graph_data_with_dml]\n";
}
Go
To learn how to install and use the client library for Spanner, see Spanner client libraries.
To authenticate to Spanner, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
import(
"context"
"fmt"
"io"
"cloud.google.com/go/spanner"
)
funcinsertGraphDataWithDml(wio.Writer,dbstring)error{
ctx:=context.Background()
client,err:=spanner.NewClient(ctx,db)
iferr!=nil{
returnerr
}
deferclient.Close()
// Execute a ReadWriteTransaction to insert values into the 'Account' table
// underpinning 'Account' nodes in 'FinGraph'. The function run by ReadWriteTransaction
// executes an 'INSERT' SQL DML statement. Graph queries run after this
// transaction is committed will observe the effects of the new 'Account's
// added to the graph.
_,err1:=client.ReadWriteTransaction(ctx,func(ctxcontext.Context,txn*spanner.ReadWriteTransaction)error{
stmt:=spanner.Statement {
SQL:`INSERT INTO Account (id, create_time, is_blocked)
VALUES
(1, CAST('2000-08-10 08:18:48.463959-07:52' AS TIMESTAMP), false),
(2, CAST('2000-08-12 07:13:16.463959-03:41' AS TIMESTAMP), true)`,
}
rowCount,err:=txn.Update(ctx,stmt)
iferr!=nil{
returnerr
}
fmt.Fprintf(w,"%d Account record(s) inserted.\n",rowCount)
returnerr
})
iferr1!=nil{
returnerr1
}
// Execute a ReadWriteTransaction to insert values into the 'AccountTransferAccount'
// table underpinning 'AccountTransferAccount' edges in 'FinGraph'. The function run
// by ReadWriteTransaction executes an 'INSERT' SQL DML statement.
// Graph queries run after this transaction is committed will observe the effects
// of the edges added to the graph.
_,err2:=client.ReadWriteTransaction(ctx,func(ctxcontext.Context,txn*spanner.ReadWriteTransaction)error{
stmt:=spanner.Statement {
SQL:`INSERT INTO AccountTransferAccount (id, to_id, create_time, amount)
VALUES
(1, 2, CAST('2000-09-11 03:11:18.463959-06:36' AS TIMESTAMP), 100),
(1, 1, CAST('2000-09-12 04:09:34.463959-05:12' AS TIMESTAMP), 200)`,
}
rowCount,err:=txn.Update(ctx,stmt)
iferr!=nil{
returnerr
}
fmt.Fprintf(w,"%d AccountTransferAccount record(s) inserted.\n",rowCount)
returnerr
})
returnerr2
}
Java
To learn how to install and use the client library for Spanner, see Spanner client libraries.
To authenticate to Spanner, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
staticvoidinsertUsingDml(DatabaseClientdbClient){
dbClient
.readWriteTransaction()
.run(
transaction->{
Stringsql=
"INSERT INTO Account (id, create_time, is_blocked) "
+" VALUES"
+" (1, CAST('2000-08-10 08:18:48.463959-07:52' AS TIMESTAMP), false),"
+" (2, CAST('2000-08-12 07:13:16.463959-03:41' AS TIMESTAMP), true)";
longrowCount=transaction.executeUpdate(Statement.of(sql));
System.out.printf("%d record(s) inserted into Account.\n",rowCount);
returnnull;
});
dbClient
.readWriteTransaction()
.run(
transaction->{
Stringsql=
"INSERT INTO AccountTransferAccount (id, to_id, create_time, amount) "
+" VALUES"
+" (1, 2, CAST('2000-09-11 03:11:18.463959-06:36' AS TIMESTAMP), 100),"
+" (1, 1, CAST('2000-09-12 04:09:34.463959-05:12' AS TIMESTAMP), 200) ";
longrowCount=transaction.executeUpdate(Statement.of(sql));
System.out.printf("%d record(s) inserted into AccountTransferAccount.\n",rowCount);
returnnull;
});
}
Python
To learn how to install and use the client library for Spanner, see Spanner client libraries.
To authenticate to Spanner, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
definsert_data_with_dml(instance_id, database_id):
"""Inserts sample data into the given database using a DML statement."""
spanner_client = spanner.Client()
instance = spanner_client.instance(instance_id)
database = instance.database(database_id)
definsert_accounts(transaction):
row_ct = transaction.execute_update(
"INSERT INTO Account (id, create_time, is_blocked) "
" VALUES"
" (1, CAST('2000-08-10 08:18:48.463959-07:52' AS TIMESTAMP), false),"
" (2, CAST('2000-08-12 07:13:16.463959-03:41' AS TIMESTAMP), true)"
)
print("{} record(s) inserted into Account.".format(row_ct))
definsert_transfers(transaction):
row_ct = transaction.execute_update(
"INSERT INTO AccountTransferAccount (id, to_id, create_time, amount) "
" VALUES"
" (1, 2, CAST('2000-09-11 03:11:18.463959-06:36' AS TIMESTAMP), 100),"
" (1, 1, CAST('2000-09-12 04:09:34.463959-05:12' AS TIMESTAMP), 200) "
)
print("{} record(s) inserted into AccountTransferAccount.".format(row_ct))
database.run_in_transaction(insert_accounts)
database.run_in_transaction(insert_transfers)
What's next
To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.