1
1
Batching Modifications
2
2
######################
3
3
4
- A :class: `~google.cloud.spanner .batch.Batch ` represents a set of data
4
+ A :class: `~google.cloud.spanner_v1 .batch.Batch ` represents a set of data
5
5
modification operations to be performed on tables in a database. Use of a
6
6
``Batch `` does not require creating an explicit
7
- :class: `~google.cloud.spanner .snapshot.Snapshot ` or
8
- :class: `~google.cloud.spanner .transaction.Transaction `. Until
9
- :meth: `~google.cloud.spanner .batch.Batch.commit ` is called on a ``Batch ``,
7
+ :class: `~google.cloud.spanner_v1 .snapshot.Snapshot ` or
8
+ :class: `~google.cloud.spanner_v1 .transaction.Transaction `. Until
9
+ :meth: `~google.cloud.spanner_v1 .batch.Batch.commit ` is called on a ``Batch ``,
10
10
no changes are propagated to the back-end.
11
11
12
12
13
- Starting a Batch
14
- ----------------
13
+ Use Batch via BatchCheckout
14
+ --------------------------------
15
15
16
- Construct a :class: `~google.cloud.spanner.batch.Batch ` object from a :class: `~google.cloud.spanner.database.Database ` object:
16
+ :meth: `Database.batch ` creates a :class: `~google.cloud.spanner_v1.database.BatchCheckout `
17
+ instance to use as a context manager to handle creating and committing a
18
+ :class: `~google.cloud.spanner_v1.batch.Batch `. The
19
+ :class: `BatchCheckout ` will automatically call
20
+ :meth: `~google.cloud.spanner_v1.batch.Batch.commit ` if the ``with `` block exits
21
+ without raising an exception.
17
22
18
23
.. code :: python
19
24
20
- from google.cloud import spanner
25
+ from google.cloud.spanner import KeySet
21
26
22
27
client = spanner.Client()
23
28
instance = client.instance(INSTANCE_NAME )
24
29
database = instance.database(DATABASE_NAME )
25
30
26
- batch = database.batch()
31
+ to_delete = KeySet(keys = [
32
+
33
+
34
+ ])
35
+
36
+ with database.batch() as batch:
37
+
38
+ batch.insert(
39
+ ' citizens' , columns = [' email' , ' first_name' , ' last_name' , ' age' ],
40
+ values = [
41
+ [' [email protected] ' , ' Phred' , ' Phlyntstone' , 32 ],
42
+ [' [email protected] ' , ' Bharney' , ' Rhubble' , 31 ],
43
+ ])
44
+
45
+ batch.update(
46
+ ' citizens' , columns = [' email' , ' age' ],
47
+ values = [
48
+
49
+
50
+ ])
51
+
52
+ ...
53
+
54
+ batch.delete(' citizens' , to_delete)
27
55
28
56
29
57
Inserting records using a Batch
30
58
-------------------------------
31
59
32
- :meth: `Batch.insert ` adds one or more new records to a table. Fails if
33
- any of the records already exists .
60
+ :meth: `Batch.insert ` adds one or more new records to a table. This fails if
61
+ any of the records already exist .
34
62
35
63
.. code :: python
36
64
@@ -53,8 +81,8 @@ any of the records already exists.
53
81
Update records using a Batch
54
82
-------------------------------
55
83
56
- :meth: `Batch.update ` updates one or more existing records in a table. Fails
57
- if any of the records does not already exist.
84
+ :meth: `Batch.update ` updates one or more existing records in a table. This fails
85
+ if any of the records do not already exist.
58
86
59
87
.. code :: python
60
88
@@ -127,8 +155,8 @@ column values are set to null.
127
155
Delete records using a Batch
128
156
----------------------------
129
157
130
- :meth: `Batch.delete ` removes one or more records from a table. Non-existent
131
- rows do not cause errors.
158
+ :meth: `Batch.delete ` removes one or more records from a table. Attempting to delete
159
+ rows that do not exist will not cause errors.
132
160
133
161
.. code :: python
134
162
@@ -151,50 +179,13 @@ After describing the modifications to be made to table data via the
151
179
the back-end by calling :meth: `Batch.commit `, which makes the ``Commit ``
152
180
API call.
153
181
154
- .. code :: python
155
-
156
- batch.commit()
157
-
158
-
159
- Use a Batch as a Context Manager
160
- --------------------------------
161
-
162
- Rather than calling :meth: `Batch.commit ` manually, you can use the
163
- :class: `Batch ` instance as a context manager, and have it called automatically
164
- if the ``with `` block exits without raising an exception.
182
+ You do not need to call this yourself as
183
+ :class: `~google.cloud.spanner_v1.database.BatchCheckout ` will call
184
+ this method automatically upon exiting the ``with `` block.
165
185
166
186
.. code :: python
167
187
168
- from google.cloud.spanner import KeySet
169
-
170
- client = spanner.Client()
171
- instance = client.instance(INSTANCE_NAME )
172
- database = instance.database(DATABASE_NAME )
173
-
174
- to_delete = KeySet(keys = [
175
-
176
-
177
- ])
178
-
179
- with database.batch() as batch:
180
-
181
- batch.insert(
182
- ' citizens' , columns = [' email' , ' first_name' , ' last_name' , ' age' ],
183
- values = [
184
- [' [email protected] ' , ' Phred' , ' Phlyntstone' , 32 ],
185
- [' [email protected] ' , ' Bharney' , ' Rhubble' , 31 ],
186
- ])
187
-
188
- batch.update(
189
- ' citizens' , columns = [' email' , ' age' ],
190
- values = [
191
-
192
-
193
- ])
194
-
195
- ...
196
-
197
- batch.delete(' citizens' , to_delete)
188
+ batch.commit()
198
189
199
190
200
191
Next Step
0 commit comments