Skip to content

Commit

Permalink
Update sample
Browse files Browse the repository at this point in the history
  • Loading branch information
sunny1612 committed Nov 2, 2023
1 parent a32dd63 commit 0f0d42f
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions samples/samples/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,13 +403,15 @@ def insert_data(instance_id, database_id):
# [END spanner_insert_data]


# [START spanner_batch_write]
# [START spanner_batch_write_at_least_once]
def batch_write(instance_id, database_id):
"""Inserts sample data into the given database via BatchWrite API.
The database and table must already exist and can be created using
`create_database`.
"""
from google.rpc.code_pb2 import OK

spanner_client = spanner.Client()
instance = spanner_client.instance(instance_id)
database = instance.database(database_id)
Expand All @@ -429,7 +431,7 @@ def batch_write(instance_id, database_id):
table="Singers",
columns=("SingerId", "FirstName", "LastName"),
values=[
(17, "Marc", "Richards"),
(17, "Marc", ""),
(18, "Catalina", "Smith"),
],
)
Expand All @@ -443,12 +445,23 @@ def batch_write(instance_id, database_id):
)

for response in groups.batch_write():
print(response)
if response.status.code == OK:
print(
"Mutation group indexes {} have been applied with commit timestamp {}".format(
response.indexes, response.commit_timestamp
)
)
else:
print(
"Mutation group indexes {} could not be applied with error {}".format(
response.indexes, response.status
)
)

print("Inserted data.")


# [END spanner_batch_write]
# [END spanner_batch_write_at_least_once]


# [START spanner_delete_data]
Expand Down

0 comments on commit 0f0d42f

Please sign in to comment.