Skip to content

Commit 7549383

Browse files
authored
docs: update samples from python-docs-samples (#146)
NUMERIC samples were recently added and need to be copied over to here.
1 parent 0fba41a commit 7549383

File tree

6 files changed

+189
-51
lines changed

6 files changed

+189
-51
lines changed

samples/samples/README.rst

Lines changed: 73 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
.. This file is automatically generated. Do not edit this file directly.
32
43
Google Cloud Spanner Python Samples
@@ -15,12 +14,10 @@ This directory contains samples for Google Cloud Spanner. `Google Cloud Spanner`
1514

1615
.. _Google Cloud Spanner: https://cloud.google.com/spanner/docs
1716

18-
1917
Setup
2018
-------------------------------------------------------------------------------
2119

2220

23-
2421
Authentication
2522
++++++++++++++
2623

@@ -31,9 +28,6 @@ credentials for applications.
3128
.. _Authentication Getting Started Guide:
3229
https://cloud.google.com/docs/authentication/getting-started
3330

34-
35-
36-
3731
Install Dependencies
3832
++++++++++++++++++++
3933

@@ -48,7 +42,7 @@ Install Dependencies
4842
.. _Python Development Environment Setup Guide:
4943
https://cloud.google.com/python/setup
5044

51-
#. Create a virtualenv. Samples are compatible with Python 3.6+.
45+
#. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+.
5246

5347
.. code-block:: bash
5448
@@ -64,15 +58,9 @@ Install Dependencies
6458
.. _pip: https://pip.pypa.io/
6559
.. _virtualenv: https://virtualenv.pypa.io/
6660

67-
68-
69-
70-
71-
7261
Samples
7362
-------------------------------------------------------------------------------
7463

75-
7664
Snippets
7765
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7866

@@ -88,10 +76,32 @@ To run this sample:
8876
8977
$ python snippets.py
9078
91-
9279
usage: snippets.py [-h] [--database-id DATABASE_ID]
9380
instance_id
94-
{create_instance,create_database,insert_data,delete_data,query_data,read_data,read_stale_data,add_column,update_data,query_data_with_new_column,read_write_transaction,read_only_transaction,add_index,query_data_with_index,read_data_with_index,add_storing_index,read_data_with_storing_index,create_table_with_timestamp,insert_data_with_timestamp,add_timestamp_column,update_data_with_timestamp,query_data_with_timestamp,write_struct_data,query_with_struct,query_with_array_of_struct,query_struct_field,query_nested_struct_field,insert_data_with_dml,update_data_with_dml,delete_data_with_dml,update_data_with_dml_timestamp,dml_write_read_transaction,update_data_with_dml_struct,insert_with_dml,query_data_with_parameter,write_with_dml_transaction,update_data_with_partitioned_dml,delete_data_with_partitioned_dml,update_with_batch_dml,create_table_with_datatypes,insert_datatypes_data,query_data_with_array,query_data_with_bool,query_data_with_bytes,query_data_with_date,query_data_with_float,query_data_with_int,query_data_with_string,query_data_with_timestamp_parameter,query_data_with_query_options,create_client_with_query_options}
81+
{create_database,insert_data,query_data,read_data,
82+
read_stale_data,add_column,update_data,
83+
query_data_with_new_column,read_write_transaction,
84+
read_only_transaction,add_index,query_data_with_index,
85+
read_data_with_index,add_storing_index,
86+
read_data_with_storing_index,
87+
create_table_with_timestamp,insert_data_with_timestamp,
88+
add_timestamp_column,update_data_with_timestamp,
89+
query_data_with_timestamp,write_struct_data,
90+
query_with_struct,query_with_array_of_struct,
91+
query_struct_field,query_nested_struct_field,
92+
insert_data_with_dml,update_data_with_dml,
93+
delete_data_with_dml,update_data_with_dml_timestamp,
94+
dml_write_read_transaction,update_data_with_dml_struct,
95+
insert_with_dml,query_data_with_parameter,
96+
write_with_dml_transaction,
97+
update_data_with_partitioned_dml,
98+
delete_data_with_partitioned_dml,update_with_batch_dml,
99+
create_table_with_datatypes,insert_datatypes_data,
100+
query_data_with_array,query_data_with_bool,
101+
query_data_with_bytes,query_data_with_date,
102+
query_data_with_float,query_data_with_int,
103+
query_data_with_string,
104+
query_data_with_timestamp_parameter}
95105
...
96106
97107
This application demonstrates how to do basic operations using Cloud
@@ -101,15 +111,32 @@ To run this sample:
101111
102112
positional arguments:
103113
instance_id Your Cloud Spanner instance ID.
104-
{create_instance,create_database,insert_data,delete_data,query_data,read_data,read_stale_data,add_column,update_data,query_data_with_new_column,read_write_transaction,read_only_transaction,add_index,query_data_with_index,read_data_with_index,add_storing_index,read_data_with_storing_index,create_table_with_timestamp,insert_data_with_timestamp,add_timestamp_column,update_data_with_timestamp,query_data_with_timestamp,write_struct_data,query_with_struct,query_with_array_of_struct,query_struct_field,query_nested_struct_field,insert_data_with_dml,update_data_with_dml,delete_data_with_dml,update_data_with_dml_timestamp,dml_write_read_transaction,update_data_with_dml_struct,insert_with_dml,query_data_with_parameter,write_with_dml_transaction,update_data_with_partitioned_dml,delete_data_with_partitioned_dml,update_with_batch_dml,create_table_with_datatypes,insert_datatypes_data,query_data_with_array,query_data_with_bool,query_data_with_bytes,query_data_with_date,query_data_with_float,query_data_with_int,query_data_with_string,query_data_with_timestamp_parameter,query_data_with_query_options,create_client_with_query_options}
105-
create_instance Creates an instance.
114+
{create_database, insert_data, delete_data, query_data, read_data,
115+
read_stale_data, add_column, update_data, query_data_with_new_column,
116+
read_write_transaction, read_only_transaction, add_index,
117+
query_data_with_index, read_data_with_index, add_storing_index,
118+
read_data_with_storing_index, create_table_with_timestamp,
119+
insert_data_with_timestamp, add_timestamp_column,
120+
update_data_with_timestamp, query_data_with_timestamp,
121+
write_struct_data, query_with_struct, query_with_array_of_struct,
122+
query_struct_field, query_nested_struct_field, insert_data_with_dml,
123+
update_data_with_dml, delete_data_with_dml,
124+
update_data_with_dml_timestamp, dml_write_read_transaction,
125+
update_data_with_dml_struct, insert_with_dml, query_data_with_parameter,
126+
write_with_dml_transaction, update_data_with_partitioned_dml,
127+
delete_data_with_partitioned_dml, update_with_batch_dml,
128+
create_table_with_datatypes, insert_datatypes_data,
129+
query_data_with_array, query_data_with_bool, query_data_with_bytes,
130+
query_data_with_date, query_data_with_float, query_data_with_int,
131+
query_data_with_string, query_data_with_timestamp_parameter}
106132
create_database Creates a database and tables for sample data.
107133
insert_data Inserts sample data into the given database. The
108134
database and table must already exist and can be
109135
created using `create_database`.
110136
delete_data Deletes sample data from the given database. The
111-
database, table, and data must already exist and can
112-
be created using `create_database` and `insert_data`.
137+
database, table, and data must already exist and
138+
can be created using `create_database` and
139+
`insert_data`.
113140
query_data Queries sample data from the database using SQL.
114141
read_data Reads sample data from the database.
115142
read_stale_data Reads sample data from the database. The data is
@@ -210,53 +237,59 @@ To run this sample:
210237
Deletes sample data from the database using a DML
211238
statement.
212239
update_data_with_dml_timestamp
213-
Updates data with Timestamp from the database using a
214-
DML statement.
240+
Updates data with Timestamp from the database using
241+
a DML statement.
215242
dml_write_read_transaction
216243
First inserts data then reads it from within a
217244
transaction using DML.
218245
update_data_with_dml_struct
219246
Updates data with a DML statement and STRUCT
220247
parameters.
221-
insert_with_dml Inserts data with a DML statement into the database.
248+
insert_with_dml Inserts data with a DML statement into the
249+
database.
222250
query_data_with_parameter
223-
Queries sample data from the database using SQL with a
224-
parameter.
251+
Queries sample data from the database using SQL
252+
with a parameter.
225253
write_with_dml_transaction
226-
Transfers part of a marketing budget from one album to
227-
another.
254+
Transfers part of a marketing budget from one
255+
album to another.
228256
update_data_with_partitioned_dml
229-
Update sample data with a partitioned DML statement.
257+
Update sample data with a partitioned DML
258+
statement.
230259
delete_data_with_partitioned_dml
231-
Delete sample data with a partitioned DML statement.
260+
Delete sample data with a partitioned DML
261+
statement.
232262
update_with_batch_dml
233-
Updates sample data in the database using Batch DML.
263+
Updates sample data in the database using Batch
264+
DML.
234265
create_table_with_datatypes
235266
Creates a table with supported dataypes.
236267
insert_datatypes_data
237268
Inserts data with supported datatypes into a table.
238269
query_data_with_array
239-
Queries sample data using SQL with an ARRAY parameter.
270+
Queries sample data using SQL with an ARRAY
271+
parameter.
240272
query_data_with_bool
241-
Queries sample data using SQL with a BOOL parameter.
273+
Queries sample data using SQL with a BOOL
274+
parameter.
242275
query_data_with_bytes
243-
Queries sample data using SQL with a BYTES parameter.
276+
Queries sample data using SQL with a BYTES
277+
parameter.
244278
query_data_with_date
245-
Queries sample data using SQL with a DATE parameter.
279+
Queries sample data using SQL with a DATE
280+
parameter.
246281
query_data_with_float
247282
Queries sample data using SQL with a FLOAT64
248283
parameter.
249284
query_data_with_int
250-
Queries sample data using SQL with a INT64 parameter.
285+
Queries sample data using SQL with a INT64
286+
parameter.
251287
query_data_with_string
252-
Queries sample data using SQL with a STRING parameter.
288+
Queries sample data using SQL with a STRING
289+
parameter.
253290
query_data_with_timestamp_parameter
254291
Queries sample data using SQL with a TIMESTAMP
255292
parameter.
256-
query_data_with_query_options
257-
Queries sample data using SQL with query options.
258-
create_client_with_query_options
259-
Create a client with query options.
260293
261294
optional arguments:
262295
-h, --help show this help message and exit
@@ -267,10 +300,6 @@ To run this sample:
267300
268301
269302
270-
271-
272-
273-
274303
The client library
275304
-------------------------------------------------------------------------------
276305

@@ -286,5 +315,4 @@ to `browse the source`_ and `report issues`_.
286315
https://github.com/GoogleCloudPlatform/google-cloud-python/issues
287316

288317

289-
290-
.. _Google Cloud SDK: https://cloud.google.com/sdk/
318+
.. _Google Cloud SDK: https://cloud.google.com/sdk/

samples/samples/backup_sample.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def restore_database(instance_id, new_database_id, backup_id):
6969
operation = new_database.restore(backup)
7070

7171
# Wait for restore operation to complete.
72-
operation.result(1200)
72+
operation.result(1600)
7373

7474
# Newly created database has restore information.
7575
new_database.reload()

samples/samples/backup_sample_test.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
# limitations under the License.
1414
import uuid
1515

16+
from google.api_core.exceptions import DeadlineExceeded
1617
from google.cloud import spanner
1718
import pytest
19+
from test_utils.retry import RetryErrors
1820

1921
import backup_sample
2022

@@ -68,6 +70,7 @@ def test_create_backup(capsys, database):
6870
assert BACKUP_ID in out
6971

7072

73+
@RetryErrors(exception=DeadlineExceeded, max_tries=2)
7174
def test_restore_database(capsys):
7275
backup_sample.restore_database(INSTANCE_ID, RESTORE_DB_ID, BACKUP_ID)
7376
out, _ = capsys.readouterr()

samples/samples/requirements-test.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
pytest==5.4.3
1+
pytest==6.0.1
22
mock==4.0.2
3+
google-cloud-testutils==0.1.0

samples/samples/snippets.py

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import argparse
2424
import base64
2525
import datetime
26+
import decimal
2627

2728
from google.cloud import spanner
2829
from google.cloud.spanner_v1 import param_types
@@ -723,6 +724,64 @@ def query_data_with_timestamp(instance_id, database_id):
723724
# [END spanner_query_data_with_timestamp_column]
724725

725726

727+
# [START spanner_add_numeric_column]
728+
def add_numeric_column(instance_id, database_id):
729+
""" Adds a new NUMERIC column to the Venues table in the example database.
730+
"""
731+
spanner_client = spanner.Client()
732+
instance = spanner_client.instance(instance_id)
733+
734+
database = instance.database(database_id)
735+
736+
operation = database.update_ddl(["ALTER TABLE Venues ADD COLUMN Revenue NUMERIC"])
737+
738+
print("Waiting for operation to complete...")
739+
operation.result(120)
740+
741+
print(
742+
'Altered table "Venues" on database {} on instance {}.'.format(
743+
database_id, instance_id
744+
)
745+
)
746+
747+
748+
# [END spanner_add_numeric_column]
749+
750+
751+
# [START spanner_update_data_with_numeric_column]
752+
def update_data_with_numeric(instance_id, database_id):
753+
"""Updates Venues tables in the database with the NUMERIC
754+
column.
755+
756+
This updates the `Revenue` column which must be created before
757+
running this sample. You can add the column by running the
758+
`add_numeric_column` sample or by running this DDL statement
759+
against your database:
760+
761+
ALTER TABLE Venues ADD COLUMN Revenue NUMERIC
762+
"""
763+
spanner_client = spanner.Client()
764+
instance = spanner_client.instance(instance_id)
765+
766+
database = instance.database(database_id)
767+
768+
with database.batch() as batch:
769+
batch.update(
770+
table="Venues",
771+
columns=("VenueId", "Revenue"),
772+
values=[
773+
(4, decimal.Decimal("35000")),
774+
(19, decimal.Decimal("104500")),
775+
(42, decimal.Decimal("99999999999999999999999999999.99")),
776+
],
777+
)
778+
779+
print("Updated data.")
780+
781+
782+
# [END spanner_update_data_with_numeric_column]
783+
784+
726785
# [START spanner_write_data_for_struct_queries]
727786
def write_struct_data(instance_id, database_id):
728787
"""Inserts sample data that can be used to test STRUCT parameters
@@ -843,7 +902,7 @@ def query_struct_field(instance_id, database_id):
843902
print(u"SingerId: {}".format(*row))
844903

845904

846-
# [START spanner_field_access_on_struct_parameters]
905+
# [END spanner_field_access_on_struct_parameters]
847906

848907

849908
# [START spanner_field_access_on_nested_struct_parameters]
@@ -1500,6 +1559,31 @@ def query_data_with_string(instance_id, database_id):
15001559
# [END spanner_query_with_string_parameter]
15011560

15021561

1562+
def query_data_with_numeric_parameter(instance_id, database_id):
1563+
"""Queries sample data using SQL with a NUMERIC parameter. """
1564+
# [START spanner_query_with_numeric_parameter]
1565+
# instance_id = "your-spanner-instance"
1566+
# database_id = "your-spanner-db-id"
1567+
spanner_client = spanner.Client()
1568+
instance = spanner_client.instance(instance_id)
1569+
database = instance.database(database_id)
1570+
1571+
example_numeric = decimal.Decimal("100000")
1572+
param = {"revenue": example_numeric}
1573+
param_type = {"revenue": param_types.NUMERIC}
1574+
1575+
with database.snapshot() as snapshot:
1576+
results = snapshot.execute_sql(
1577+
"SELECT VenueId, Revenue FROM Venues " "WHERE Revenue < @revenue",
1578+
params=param,
1579+
param_types=param_type,
1580+
)
1581+
1582+
for row in results:
1583+
print(u"VenueId: {}, Revenue: {}".format(*row))
1584+
# [END spanner_query_with_numeric_parameter]
1585+
1586+
15031587
def query_data_with_timestamp_parameter(instance_id, database_id):
15041588
"""Queries sample data using SQL with a TIMESTAMP parameter. """
15051589
# [START spanner_query_with_timestamp_parameter]
@@ -1510,6 +1594,13 @@ def query_data_with_timestamp_parameter(instance_id, database_id):
15101594
database = instance.database(database_id)
15111595

15121596
example_timestamp = datetime.datetime.utcnow().isoformat() + "Z"
1597+
# [END spanner_query_with_timestamp_parameter]
1598+
# Avoid time drift on the local machine.
1599+
# https://github.com/GoogleCloudPlatform/python-docs-samples/issues/4197.
1600+
example_timestamp = (
1601+
datetime.datetime.utcnow() + datetime.timedelta(days=1)
1602+
).isoformat() + "Z"
1603+
# [START spanner_query_with_timestamp_parameter]
15131604
param = {"last_update_time": example_timestamp}
15141605
param_type = {"last_update_time": param_types.TIMESTAMP}
15151606

0 commit comments

Comments
 (0)