|
| 1 | +Batching Modifications |
| 2 | +###################### |
| 3 | + |
| 4 | +A :class:`~google.cloud.spanner.batch.Batch` represents a set of data |
| 5 | +modification operations to be performed on tables in a dataset. Use of a |
| 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``, |
| 10 | +no changes are propagated to the back-end. |
| 11 | + |
| 12 | + |
| 13 | +Starting a Batch |
| 14 | +---------------- |
| 15 | + |
| 16 | +.. code:: python |
| 17 | +
|
| 18 | + batch = session.batch() |
| 19 | +
|
| 20 | +
|
| 21 | +Inserting records using a Batch |
| 22 | +------------------------------- |
| 23 | + |
| 24 | +:meth:`Batch.insert` adds one or more new records to a table. Fails if |
| 25 | +any of the records already exists. |
| 26 | + |
| 27 | +.. code:: python |
| 28 | +
|
| 29 | + batch.insert( |
| 30 | + 'citizens', columns=['email', 'first_name', 'last_name', 'age'], |
| 31 | + values=[ |
| 32 | + ['phred@exammple.com', 'Phred', 'Phlyntstone', 32], |
| 33 | + ['bharney@example.com', 'Bharney', 'Rhubble', 31], |
| 34 | + ]) |
| 35 | +
|
| 36 | +.. note:: |
| 37 | + |
| 38 | + Ensure that data being sent for ``STRING`` columns uses a text string |
| 39 | + (``str`` in Python 3; ``unicode`` in Python 2). |
| 40 | + |
| 41 | + Additionally, if you are writing data intended for a ``BYTES`` column, you |
| 42 | + must base64 encode it. |
| 43 | + |
| 44 | + |
| 45 | +Update records using a Batch |
| 46 | +------------------------------- |
| 47 | + |
| 48 | +:meth:`Batch.update` updates one or more existing records in a table. Fails |
| 49 | +if any of the records does not already exist. |
| 50 | + |
| 51 | +.. code:: python |
| 52 | +
|
| 53 | + batch.update( |
| 54 | + 'citizens', columns=['email', 'age'], |
| 55 | + values=[ |
| 56 | + ['phred@exammple.com', 33], |
| 57 | + ['bharney@example.com', 32], |
| 58 | + ]) |
| 59 | +
|
| 60 | +.. note:: |
| 61 | + |
| 62 | + Ensure that data being sent for ``STRING`` columns uses a text string |
| 63 | + (``str`` in Python 3; ``unicode`` in Python 2). |
| 64 | + |
| 65 | + Additionally, if you are writing data intended for a ``BYTES`` column, you |
| 66 | + must base64 encode it. |
| 67 | + |
| 68 | + |
| 69 | +Insert or update records using a Batch |
| 70 | +-------------------------------------- |
| 71 | + |
| 72 | +:meth:`Batch.insert_or_update` inserts *or* updates one or more records in a |
| 73 | +table. Existing rows have values for the supplied columns overwritten; other |
| 74 | +column values are preserved. |
| 75 | + |
| 76 | +.. code:: python |
| 77 | +
|
| 78 | + batch.insert_or_update( |
| 79 | + 'citizens', columns=['email', 'first_name', 'last_name', 'age'], |
| 80 | + values=[ |
| 81 | + ['phred@exammple.com', 'Phred', 'Phlyntstone', 31], |
| 82 | + ['wylma@example.com', 'Wylma', 'Phlyntstone', 29], |
| 83 | + ]) |
| 84 | +
|
| 85 | +.. note:: |
| 86 | + |
| 87 | + Ensure that data being sent for ``STRING`` columns uses a text string |
| 88 | + (``str`` in Python 3; ``unicode`` in Python 2). |
| 89 | + |
| 90 | + Additionally, if you are writing data intended for a ``BYTES`` column, you |
| 91 | + must base64 encode it. |
| 92 | + |
| 93 | + |
| 94 | +Replace records using a Batch |
| 95 | +----------------------------- |
| 96 | + |
| 97 | +:meth:`Batch.replace` inserts *or* updates one or more records in a |
| 98 | +table. Existing rows have values for the supplied columns overwritten; other |
| 99 | +column values are set to null. |
| 100 | + |
| 101 | +.. code:: python |
| 102 | +
|
| 103 | + batch.replace( |
| 104 | + 'citizens', columns=['email', 'first_name', 'last_name', 'age'], |
| 105 | + values=[ |
| 106 | + ['bharney@example.com', 'Bharney', 'Rhubble', 30], |
| 107 | + ['bhettye@example.com', 'Bhettye', 'Rhubble', 30], |
| 108 | + ]) |
| 109 | +
|
| 110 | +.. note:: |
| 111 | + |
| 112 | + Ensure that data being sent for ``STRING`` columns uses a text string |
| 113 | + (``str`` in Python 3; ``unicode`` in Python 2). |
| 114 | + |
| 115 | + Additionally, if you are writing data intended for a ``BYTES`` column, you |
| 116 | + must base64 encode it. |
| 117 | + |
| 118 | + |
| 119 | +Delete records using a Batch |
| 120 | +---------------------------- |
| 121 | + |
| 122 | +:meth:`Batch.delete` removes one or more records from a table. Non-existent |
| 123 | +rows do not cause errors. |
| 124 | + |
| 125 | +.. code:: python |
| 126 | +
|
| 127 | + batch.delete('citizens', |
| 128 | + keyset['bharney@example.com', 'nonesuch@example.com']) |
| 129 | +
|
| 130 | +
|
| 131 | +Commit changes for a Batch |
| 132 | +-------------------------- |
| 133 | + |
| 134 | +After describing the modifications to be made to table data via the |
| 135 | +:meth:`Batch.insert`, :meth:`Batch.update`, :meth:`Batch.insert_or_update`, |
| 136 | +:meth:`Batch.replace`, and :meth:`Batch.delete` methods above, send them to |
| 137 | +the back-end by calling :meth:`Batch.commit`, which makes the ``Commit`` |
| 138 | +API call. |
| 139 | + |
| 140 | +.. code:: python |
| 141 | +
|
| 142 | + batch.commit() |
| 143 | +
|
| 144 | +
|
| 145 | +Use a Batch as a Context Manager |
| 146 | +-------------------------------- |
| 147 | + |
| 148 | +Rather than calling :meth:`Batch.commit` manually, you can use the |
| 149 | +:class:`Batch` instance as a context manager, and have it called automatically |
| 150 | +if the ``with`` block exits without raising an exception. |
| 151 | + |
| 152 | +.. code:: python |
| 153 | +
|
| 154 | + with session.batch() as batch: |
| 155 | +
|
| 156 | + batch.insert( |
| 157 | + 'citizens', columns=['email', 'first_name', 'last_name', 'age'], |
| 158 | + values=[ |
| 159 | + ['phred@exammple.com', 'Phred', 'Phlyntstone', 32], |
| 160 | + ['bharney@example.com', 'Bharney', 'Rhubble', 31], |
| 161 | + ]) |
| 162 | +
|
| 163 | + batch.update( |
| 164 | + 'citizens', columns=['email', 'age'], |
| 165 | + values=[ |
| 166 | + ['phred@exammple.com', 33], |
| 167 | + ['bharney@example.com', 32], |
| 168 | + ]) |
| 169 | +
|
| 170 | + ... |
| 171 | +
|
| 172 | + batch.delete('citizens', |
| 173 | + keyset['bharney@example.com', 'nonesuch@example.com']) |
| 174 | +
|
| 175 | +
|
| 176 | +Next Step |
| 177 | +--------- |
| 178 | + |
| 179 | +Next, learn about :doc:`spanner-snapshot-usage`. |
0 commit comments