@@ -2043,6 +2043,73 @@ def create_client_with_query_options(instance_id, database_id):
2043
2043
# [END spanner_create_client_with_query_options]
2044
2044
2045
2045
2046
+ def set_transaction_tag (instance_id , database_id ):
2047
+ """Executes a transaction with a transaction tag."""
2048
+ # [START spanner_set_transaction_tag]
2049
+ # instance_id = "your-spanner-instance"
2050
+ # database_id = "your-spanner-db-id"
2051
+ spanner_client = spanner .Client ()
2052
+ instance = spanner_client .instance (instance_id )
2053
+ database = instance .database (database_id )
2054
+
2055
+ def update_venues (transaction ):
2056
+ # Sets the request tag to "app=concert,env=dev,action=update".
2057
+ # This request tag will only be set on this request.
2058
+ transaction .execute_update (
2059
+ "UPDATE Venues SET Capacity = CAST(Capacity/4 AS INT64) WHERE OutdoorVenue = false" ,
2060
+ request_options = {"request_tag" : "app=concert,env=dev,action=update" }
2061
+ )
2062
+ print ("Venue capacities updated." )
2063
+
2064
+ # Sets the request tag to "app=concert,env=dev,action=insert".
2065
+ # This request tag will only be set on this request.
2066
+ transaction .execute_update (
2067
+ "INSERT INTO Venues (VenueId, VenueName, Capacity, OutdoorVenue, LastUpdateTime) "
2068
+ "VALUES (@venueId, @venueName, @capacity, @outdoorVenue, PENDING_COMMIT_TIMESTAMP())" ,
2069
+ params = {
2070
+ "venueId" : 81 ,
2071
+ "venueName" : "Venue 81" ,
2072
+ "capacity" : 1440 ,
2073
+ "outdoorVenue" : True
2074
+ },
2075
+ param_types = {
2076
+ "venueId" : param_types .INT64 ,
2077
+ "venueName" : param_types .STRING ,
2078
+ "capacity" : param_types .INT64 ,
2079
+ "outdoorVenue" : param_types .BOOL
2080
+ },
2081
+ request_options = {"request_tag" : "app=concert,env=dev,action=insert" }
2082
+ )
2083
+ print ("New venue inserted." )
2084
+
2085
+ database .run_in_transaction (
2086
+ update_venues , transaction_tag = "app=concert,env=dev"
2087
+ )
2088
+
2089
+ # [END spanner_set_transaction_tag]
2090
+
2091
+
2092
+ def set_request_tag (instance_id , database_id ):
2093
+ """Executes a snapshot read with a request tag."""
2094
+ # [START spanner_set_request_tag]
2095
+ # instance_id = "your-spanner-instance"
2096
+ # database_id = "your-spanner-db-id"
2097
+ spanner_client = spanner .Client ()
2098
+ instance = spanner_client .instance (instance_id )
2099
+ database = instance .database (database_id )
2100
+
2101
+ with database .snapshot () as snapshot :
2102
+ results = snapshot .execute_sql (
2103
+ "SELECT SingerId, AlbumId, AlbumTitle FROM Albums" ,
2104
+ request_options = {"request_tag" : "app=concert,env=dev,action=select" }
2105
+ )
2106
+
2107
+ for row in results :
2108
+ print (u"SingerId: {}, AlbumId: {}, AlbumTitle: {}" .format (* row ))
2109
+
2110
+ # [END spanner_set_request_tag]
2111
+
2112
+
2046
2113
if __name__ == "__main__" : # noqa: C901
2047
2114
parser = argparse .ArgumentParser (
2048
2115
description = __doc__ , formatter_class = argparse .RawDescriptionHelpFormatter
0 commit comments