1
1
Read-only Transactions via Snapshots
2
2
####################################
3
3
4
- A :class: `~google.cloud.spanner .snapshot.Snapshot ` represents a read-only
5
- transaction: when multiple read operations are peformed via a Snapshot,
4
+ A :class: `~google.cloud.spanner_v1 .snapshot.Snapshot ` represents a read-only
5
+ transaction: when multiple read operations are performed via a Snapshot,
6
6
the results are consistent as of a particular point in time.
7
7
8
8
@@ -15,7 +15,8 @@ transactions are visible:
15
15
16
16
.. code :: python
17
17
18
- snapshot = database.snapshot()
18
+ with database.snapshot() as snapshot:
19
+ ...
19
20
20
21
You can also specify a weaker bound, which can either be to perform all
21
22
reads as of a given timestamp:
@@ -25,15 +26,19 @@ reads as of a given timestamp:
25
26
import datetime
26
27
from pytz import UTC
27
28
TIMESTAMP = datetime.datetime.utcnow().replace(tzinfo = UTC )
28
- snapshot = database.snapshot(read_timestamp = TIMESTAMP )
29
+
30
+ with database.snapshot(read_timestamp = TIMESTAMP ) as snapshot:
31
+ ...
29
32
30
33
or as of a given duration in the past:
31
34
32
35
.. code :: python
33
36
34
37
import datetime
35
38
DURATION = datetime.timedelta(seconds = 5 )
36
- snapshot = database.snapshot(exact_staleness = DURATION )
39
+
40
+ with database.snapshot(exact_staleness = DURATION ) as snapshot:
41
+ ...
37
42
38
43
Single Use and Multiple Use Snapshots
39
44
-------------------------------------
@@ -48,18 +53,19 @@ reused.
48
53
49
54
.. code :: python
50
55
51
- snapshot = database.snapshot(multi_use = True )
56
+ with database.snapshot(multi_use = True ) as snapshot:
57
+ ...
52
58
53
- :meth: `~.spanner_v1.snapshot.Snapshot.begin ` can only be used on a
59
+ :meth: `~google.cloud .spanner_v1.snapshot.Snapshot.begin ` can only be used on a
54
60
snapshot with ``multi_use=True ``. In which case it is also necessary
55
61
to call if you need to have multiple pending operations.
56
62
57
63
Read Table Data
58
64
---------------
59
65
60
- Read data for selected rows from a table in the database. Calls
61
- the `` Read `` API, which returns all rows specified in `` key_set ``, or else
62
- fails if the result set is too large,
66
+ To read data for selected rows from a table in the database, call
67
+ :meth: ` ~google.cloud.spanner_v1.snapshot.Snapshot.read ` which will return
68
+ all rows specified in `` key_set ``, or fail if the result set is too large,
63
69
64
70
.. code :: python
65
71
@@ -73,16 +79,17 @@ fails if the result set is too large,
73
79
74
80
.. note ::
75
81
76
- Perform all iteration within the context of the ``with database.snapshot() ``
82
+ Perform all iterations within the context of the ``with database.snapshot() ``
77
83
block.
78
84
79
85
80
86
Execute a SQL Select Statement
81
87
------------------------------
82
88
83
- Read data from a query against tables in the database. Calls
84
- the ``ExecuteSql `` API, which returns all rows matching the query, or else
85
- fails if the result set is too large,
89
+ To read data from tables in the database using a query, call
90
+ :meth: `~google.cloud.spanner_v1.snapshot.Snapshot.execute_sql `
91
+ which will return all rows matching the query, or fail if the
92
+ result set is too large,
86
93
87
94
.. code :: python
88
95
0 commit comments