@@ -403,13 +403,15 @@ def insert_data(instance_id, database_id):
403
403
# [END spanner_insert_data]
404
404
405
405
406
- # [START spanner_batch_write ]
406
+ # [START spanner_batch_write_at_least_once ]
407
407
def batch_write (instance_id , database_id ):
408
408
"""Inserts sample data into the given database via BatchWrite API.
409
409
410
410
The database and table must already exist and can be created using
411
411
`create_database`.
412
412
"""
413
+ from google .rpc .code_pb2 import OK
414
+
413
415
spanner_client = spanner .Client ()
414
416
instance = spanner_client .instance (instance_id )
415
417
database = instance .database (database_id )
@@ -429,7 +431,7 @@ def batch_write(instance_id, database_id):
429
431
table = "Singers" ,
430
432
columns = ("SingerId" , "FirstName" , "LastName" ),
431
433
values = [
432
- (17 , "Marc" , "Richards " ),
434
+ (17 , "Marc" , "" ),
433
435
(18 , "Catalina" , "Smith" ),
434
436
],
435
437
)
@@ -443,12 +445,21 @@ def batch_write(instance_id, database_id):
443
445
)
444
446
445
447
for response in groups .batch_write ():
446
- print (response )
447
-
448
- print ("Inserted data." )
448
+ if response .status .code == OK :
449
+ print (
450
+ "Mutation group indexes {} have been applied with commit timestamp {}" .format (
451
+ response .indexes , response .commit_timestamp
452
+ )
453
+ )
454
+ else :
455
+ print (
456
+ "Mutation group indexes {} could not be applied with error {}" .format (
457
+ response .indexes , response .status
458
+ )
459
+ )
449
460
450
461
451
- # [END spanner_batch_write ]
462
+ # [END spanner_batch_write_at_least_once ]
452
463
453
464
454
465
# [START spanner_delete_data]
0 commit comments