25
25
from google .cloud import spanner
26
26
27
27
28
+ # [START spanner_create_database]
28
29
def create_database (instance_id , database_id ):
29
30
"""Creates a database and tables for sample data."""
30
31
spanner_client = spanner .Client ()
@@ -52,8 +53,10 @@ def create_database(instance_id, database_id):
52
53
53
54
print ('Created database {} on instance {}' .format (
54
55
database_id , instance_id ))
56
+ # [END spanner_create_database]
55
57
56
58
59
+ # [START spanner_insert_data]
57
60
def insert_data (instance_id , database_id ):
58
61
"""Inserts sample data into the given database.
59
62
@@ -86,8 +89,10 @@ def insert_data(instance_id, database_id):
86
89
(2 , 3 , u'Terrified' )])
87
90
88
91
print ('Inserted data.' )
92
+ # [END spanner_insert_data]
89
93
90
94
95
+ # [START spanner_query_data]
91
96
def query_data (instance_id , database_id ):
92
97
"""Queries sample data from the database using SQL."""
93
98
spanner_client = spanner .Client ()
@@ -100,8 +105,10 @@ def query_data(instance_id, database_id):
100
105
101
106
for row in results :
102
107
print (u'SingerId: {}, AlbumId: {}, AlbumTitle: {}' .format (* row ))
108
+ # [END spanner_query_data]
103
109
104
110
111
+ # [START spanner_read_data]
105
112
def read_data (instance_id , database_id ):
106
113
"""Reads sample data from the database."""
107
114
spanner_client = spanner .Client ()
@@ -117,8 +124,10 @@ def read_data(instance_id, database_id):
117
124
118
125
for row in results :
119
126
print (u'SingerId: {}, AlbumId: {}, AlbumTitle: {}' .format (* row ))
127
+ # [END spanner_read_data]
120
128
121
129
130
+ # [START spanner_read_stale_data]
122
131
def read_stale_data (instance_id , database_id ):
123
132
"""Reads sample data from the database. The data is exactly 15 seconds
124
133
stale."""
@@ -138,8 +147,10 @@ def read_stale_data(instance_id, database_id):
138
147
139
148
for row in results :
140
149
print (u'SingerId: {}, AlbumId: {}, AlbumTitle: {}' .format (* row ))
150
+ # [END spanner_read_stale_data]
141
151
142
152
153
+ # [START spanner_query_data_with_new_column]
143
154
def query_data_with_new_column (instance_id , database_id ):
144
155
"""Queries sample data from the database using SQL.
145
156
@@ -160,8 +171,10 @@ def query_data_with_new_column(instance_id, database_id):
160
171
for row in results :
161
172
print (
162
173
u'SingerId: {}, AlbumId: {}, MarketingBudget: {}' .format (* row ))
174
+ # [END spanner_query_data_with_new_column]
163
175
164
176
177
+ # [START spanner_create_index]
165
178
def add_index (instance_id , database_id ):
166
179
"""Adds a simple index to the example database."""
167
180
spanner_client = spanner .Client ()
@@ -175,8 +188,10 @@ def add_index(instance_id, database_id):
175
188
operation .result ()
176
189
177
190
print ('Added the AlbumsByAlbumTitle index.' )
191
+ # [END spanner_create_index]
178
192
179
193
194
+ # [START spanner_query_data_with_index]
180
195
def query_data_with_index (
181
196
instance_id , database_id , start_title = 'Aardvark' , end_title = 'Goo' ):
182
197
"""Queries sample data from the database using SQL and an index.
@@ -220,8 +235,10 @@ def query_data_with_index(
220
235
print (
221
236
u'AlbumId: {}, AlbumTitle: {}, '
222
237
'MarketingBudget: {}' .format (* row ))
238
+ # [END spanner_query_data_with_index]
223
239
224
240
241
+ # [START spanner_read_data_with_index]
225
242
def read_data_with_index (instance_id , database_id ):
226
243
"""Reads sample data from the database using an index.
227
244
@@ -246,8 +263,10 @@ def read_data_with_index(instance_id, database_id):
246
263
247
264
for row in results :
248
265
print ('AlbumId: {}, AlbumTitle: {}' .format (* row ))
266
+ # [END spanner_read_data_with_index]
249
267
250
268
269
+ # [START spanner_create_storing_index]
251
270
def add_storing_index (instance_id , database_id ):
252
271
"""Adds an storing index to the example database."""
253
272
spanner_client = spanner .Client ()
@@ -262,8 +281,10 @@ def add_storing_index(instance_id, database_id):
262
281
operation .result ()
263
282
264
283
print ('Added the AlbumsByAlbumTitle2 index.' )
284
+ # [END spanner_create_storing_index]
265
285
266
286
287
+ # [START spanner_read_data_with_storing_index]
267
288
def read_data_with_storing_index (instance_id , database_id ):
268
289
"""Reads sample data from the database using an index with a storing
269
290
clause.
@@ -292,8 +313,10 @@ def read_data_with_storing_index(instance_id, database_id):
292
313
print (
293
314
u'AlbumId: {}, AlbumTitle: {}, '
294
315
'MarketingBudget: {}' .format (* row ))
316
+ # [END spanner_read_data_with_storing_index]
295
317
296
318
319
+ # [START spanner_add_column]
297
320
def add_column (instance_id , database_id ):
298
321
"""Adds a new column to the Albums table in the example database."""
299
322
spanner_client = spanner .Client ()
@@ -307,8 +330,10 @@ def add_column(instance_id, database_id):
307
330
operation .result ()
308
331
309
332
print ('Added the MarketingBudget column.' )
333
+ # [END spanner_add_column]
310
334
311
335
336
+ # [START spanner_update_data]
312
337
def update_data (instance_id , database_id ):
313
338
"""Updates sample data in the database.
314
339
@@ -333,8 +358,10 @@ def update_data(instance_id, database_id):
333
358
(2 , 2 , 500000 )])
334
359
335
360
print ('Updated data.' )
361
+ # [END spanner_update_data]
336
362
337
363
364
+ # [START spanner_read_write_transaction]
338
365
def read_write_transaction (instance_id , database_id ):
339
366
"""Performs a read-write transaction to update two sample records in the
340
367
database.
@@ -395,8 +422,10 @@ def update_albums(transaction):
395
422
database .run_in_transaction (update_albums )
396
423
397
424
print ('Transaction complete.' )
425
+ # [END spanner_read_write_transaction]
398
426
399
427
428
+ # [START spanner_read_only_transaction]
400
429
def read_only_transaction (instance_id , database_id ):
401
430
"""Reads data inside of a read-only transaction.
402
431
@@ -428,6 +457,7 @@ def read_only_transaction(instance_id, database_id):
428
457
print ('Results from second read:' )
429
458
for row in results :
430
459
print (u'SingerId: {}, AlbumId: {}, AlbumTitle: {}' .format (* row ))
460
+ # [END spanner_read_only_transaction]
431
461
432
462
433
463
if __name__ == '__main__' :
0 commit comments