google-cloud-bigtable 2.30.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (115) hide show
  1. google/cloud/bigtable/__init__.py +25 -0
  2. google/cloud/bigtable/app_profile.py +377 -0
  3. google/cloud/bigtable/backup.py +490 -0
  4. google/cloud/bigtable/batcher.py +414 -0
  5. google/cloud/bigtable/client.py +475 -0
  6. google/cloud/bigtable/cluster.py +541 -0
  7. google/cloud/bigtable/column_family.py +362 -0
  8. google/cloud/bigtable/data/__init__.py +103 -0
  9. google/cloud/bigtable/data/_async/__init__.py +25 -0
  10. google/cloud/bigtable/data/_async/_mutate_rows.py +226 -0
  11. google/cloud/bigtable/data/_async/_read_rows.py +363 -0
  12. google/cloud/bigtable/data/_async/client.py +1522 -0
  13. google/cloud/bigtable/data/_async/mutations_batcher.py +534 -0
  14. google/cloud/bigtable/data/_cross_sync/__init__.py +20 -0
  15. google/cloud/bigtable/data/_cross_sync/_decorators.py +441 -0
  16. google/cloud/bigtable/data/_cross_sync/_mapping_meta.py +64 -0
  17. google/cloud/bigtable/data/_cross_sync/cross_sync.py +334 -0
  18. google/cloud/bigtable/data/_helpers.py +249 -0
  19. google/cloud/bigtable/data/_sync_autogen/_mutate_rows.py +182 -0
  20. google/cloud/bigtable/data/_sync_autogen/_read_rows.py +304 -0
  21. google/cloud/bigtable/data/_sync_autogen/client.py +1293 -0
  22. google/cloud/bigtable/data/_sync_autogen/mutations_batcher.py +449 -0
  23. google/cloud/bigtable/data/exceptions.py +340 -0
  24. google/cloud/bigtable/data/execute_query/__init__.py +43 -0
  25. google/cloud/bigtable/data/execute_query/_async/__init__.py +13 -0
  26. google/cloud/bigtable/data/execute_query/_async/execute_query_iterator.py +291 -0
  27. google/cloud/bigtable/data/execute_query/_byte_cursor.py +123 -0
  28. google/cloud/bigtable/data/execute_query/_checksum.py +43 -0
  29. google/cloud/bigtable/data/execute_query/_parameters_formatting.py +155 -0
  30. google/cloud/bigtable/data/execute_query/_query_result_parsing_utils.py +134 -0
  31. google/cloud/bigtable/data/execute_query/_reader.py +122 -0
  32. google/cloud/bigtable/data/execute_query/_sync_autogen/execute_query_iterator.py +239 -0
  33. google/cloud/bigtable/data/execute_query/metadata.py +399 -0
  34. google/cloud/bigtable/data/execute_query/values.py +123 -0
  35. google/cloud/bigtable/data/mutations.py +380 -0
  36. google/cloud/bigtable/data/read_modify_write_rules.py +112 -0
  37. google/cloud/bigtable/data/read_rows_query.py +536 -0
  38. google/cloud/bigtable/data/row.py +535 -0
  39. google/cloud/bigtable/data/row_filters.py +968 -0
  40. google/cloud/bigtable/encryption_info.py +64 -0
  41. google/cloud/bigtable/enums.py +223 -0
  42. google/cloud/bigtable/error.py +64 -0
  43. google/cloud/bigtable/gapic_version.py +16 -0
  44. google/cloud/bigtable/helpers.py +31 -0
  45. google/cloud/bigtable/instance.py +789 -0
  46. google/cloud/bigtable/policy.py +255 -0
  47. google/cloud/bigtable/row.py +1267 -0
  48. google/cloud/bigtable/row_data.py +380 -0
  49. google/cloud/bigtable/row_filters.py +838 -0
  50. google/cloud/bigtable/row_merger.py +250 -0
  51. google/cloud/bigtable/row_set.py +213 -0
  52. google/cloud/bigtable/table.py +1409 -0
  53. google/cloud/bigtable_admin/__init__.py +410 -0
  54. google/cloud/bigtable_admin/gapic_version.py +16 -0
  55. google/cloud/bigtable_admin/py.typed +2 -0
  56. google/cloud/bigtable_admin_v2/__init__.py +250 -0
  57. google/cloud/bigtable_admin_v2/gapic_metadata.json +962 -0
  58. google/cloud/bigtable_admin_v2/gapic_version.py +16 -0
  59. google/cloud/bigtable_admin_v2/py.typed +2 -0
  60. google/cloud/bigtable_admin_v2/services/__init__.py +15 -0
  61. google/cloud/bigtable_admin_v2/services/bigtable_instance_admin/__init__.py +22 -0
  62. google/cloud/bigtable_admin_v2/services/bigtable_instance_admin/async_client.py +3465 -0
  63. google/cloud/bigtable_admin_v2/services/bigtable_instance_admin/client.py +3944 -0
  64. google/cloud/bigtable_admin_v2/services/bigtable_instance_admin/pagers.py +681 -0
  65. google/cloud/bigtable_admin_v2/services/bigtable_instance_admin/transports/__init__.py +40 -0
  66. google/cloud/bigtable_admin_v2/services/bigtable_instance_admin/transports/base.py +751 -0
  67. google/cloud/bigtable_admin_v2/services/bigtable_instance_admin/transports/grpc.py +1249 -0
  68. google/cloud/bigtable_admin_v2/services/bigtable_instance_admin/transports/grpc_asyncio.py +1579 -0
  69. google/cloud/bigtable_admin_v2/services/bigtable_instance_admin/transports/rest.py +6820 -0
  70. google/cloud/bigtable_admin_v2/services/bigtable_instance_admin/transports/rest_base.py +1746 -0
  71. google/cloud/bigtable_admin_v2/services/bigtable_table_admin/__init__.py +22 -0
  72. google/cloud/bigtable_admin_v2/services/bigtable_table_admin/async_client.py +3472 -0
  73. google/cloud/bigtable_admin_v2/services/bigtable_table_admin/client.py +3949 -0
  74. google/cloud/bigtable_admin_v2/services/bigtable_table_admin/pagers.py +669 -0
  75. google/cloud/bigtable_admin_v2/services/bigtable_table_admin/transports/__init__.py +40 -0
  76. google/cloud/bigtable_admin_v2/services/bigtable_table_admin/transports/base.py +708 -0
  77. google/cloud/bigtable_admin_v2/services/bigtable_table_admin/transports/grpc.py +1257 -0
  78. google/cloud/bigtable_admin_v2/services/bigtable_table_admin/transports/grpc_asyncio.py +1549 -0
  79. google/cloud/bigtable_admin_v2/services/bigtable_table_admin/transports/rest.py +6609 -0
  80. google/cloud/bigtable_admin_v2/services/bigtable_table_admin/transports/rest_base.py +1714 -0
  81. google/cloud/bigtable_admin_v2/types/__init__.py +248 -0
  82. google/cloud/bigtable_admin_v2/types/bigtable_instance_admin.py +1364 -0
  83. google/cloud/bigtable_admin_v2/types/bigtable_table_admin.py +1715 -0
  84. google/cloud/bigtable_admin_v2/types/common.py +81 -0
  85. google/cloud/bigtable_admin_v2/types/instance.py +819 -0
  86. google/cloud/bigtable_admin_v2/types/table.py +1028 -0
  87. google/cloud/bigtable_admin_v2/types/types.py +776 -0
  88. google/cloud/bigtable_v2/__init__.py +136 -0
  89. google/cloud/bigtable_v2/gapic_metadata.json +193 -0
  90. google/cloud/bigtable_v2/gapic_version.py +16 -0
  91. google/cloud/bigtable_v2/py.typed +2 -0
  92. google/cloud/bigtable_v2/services/__init__.py +15 -0
  93. google/cloud/bigtable_v2/services/bigtable/__init__.py +22 -0
  94. google/cloud/bigtable_v2/services/bigtable/async_client.py +1741 -0
  95. google/cloud/bigtable_v2/services/bigtable/client.py +2194 -0
  96. google/cloud/bigtable_v2/services/bigtable/transports/__init__.py +38 -0
  97. google/cloud/bigtable_v2/services/bigtable/transports/base.py +343 -0
  98. google/cloud/bigtable_v2/services/bigtable/transports/grpc.py +660 -0
  99. google/cloud/bigtable_v2/services/bigtable/transports/grpc_asyncio.py +762 -0
  100. google/cloud/bigtable_v2/services/bigtable/transports/rest.py +2489 -0
  101. google/cloud/bigtable_v2/services/bigtable/transports/rest_base.py +711 -0
  102. google/cloud/bigtable_v2/types/__init__.py +138 -0
  103. google/cloud/bigtable_v2/types/bigtable.py +1531 -0
  104. google/cloud/bigtable_v2/types/data.py +1612 -0
  105. google/cloud/bigtable_v2/types/feature_flags.py +119 -0
  106. google/cloud/bigtable_v2/types/request_stats.py +171 -0
  107. google/cloud/bigtable_v2/types/response_params.py +64 -0
  108. google/cloud/bigtable_v2/types/types.py +579 -0
  109. google_cloud_bigtable-2.30.0.data/scripts/fixup_bigtable_admin_v2_keywords.py +233 -0
  110. google_cloud_bigtable-2.30.0.data/scripts/fixup_bigtable_v2_keywords.py +186 -0
  111. google_cloud_bigtable-2.30.0.dist-info/LICENSE +202 -0
  112. google_cloud_bigtable-2.30.0.dist-info/METADATA +154 -0
  113. google_cloud_bigtable-2.30.0.dist-info/RECORD +115 -0
  114. google_cloud_bigtable-2.30.0.dist-info/WHEEL +5 -0
  115. google_cloud_bigtable-2.30.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,1409 @@
1
+ # Copyright 2015 Google LLC
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ """User-friendly container for Google Cloud Bigtable Table."""
16
+
17
+ from typing import Set
18
+ import warnings
19
+
20
+ from google.api_core import timeout
21
+ from google.api_core.exceptions import Aborted
22
+ from google.api_core.exceptions import DeadlineExceeded
23
+ from google.api_core.exceptions import NotFound
24
+ from google.api_core.exceptions import RetryError
25
+ from google.api_core.exceptions import ServiceUnavailable
26
+ from google.api_core.exceptions import InternalServerError
27
+ from google.api_core.gapic_v1.method import DEFAULT
28
+ from google.api_core.retry import if_exception_type
29
+ from google.api_core.retry import Retry
30
+ from google.cloud._helpers import _to_bytes # type: ignore
31
+ from google.cloud.bigtable.backup import Backup
32
+ from google.cloud.bigtable.column_family import _gc_rule_from_pb
33
+ from google.cloud.bigtable.column_family import ColumnFamily
34
+ from google.cloud.bigtable.batcher import MutationsBatcher
35
+ from google.cloud.bigtable.batcher import FLUSH_COUNT, MAX_MUTATION_SIZE
36
+ from google.cloud.bigtable.encryption_info import EncryptionInfo
37
+ from google.cloud.bigtable.policy import Policy
38
+ from google.cloud.bigtable.row import AppendRow
39
+ from google.cloud.bigtable.row import ConditionalRow
40
+ from google.cloud.bigtable.row import DirectRow
41
+ from google.cloud.bigtable.row_data import (
42
+ PartialRowsData,
43
+ _retriable_internal_server_error,
44
+ )
45
+ from google.cloud.bigtable.row_data import DEFAULT_RETRY_READ_ROWS
46
+ from google.cloud.bigtable.row_set import RowSet
47
+ from google.cloud.bigtable.row_set import RowRange
48
+ from google.cloud.bigtable import enums
49
+ from google.cloud.bigtable_v2.types import bigtable as data_messages_v2_pb2
50
+ from google.cloud.bigtable_admin_v2 import BigtableTableAdminClient
51
+ from google.cloud.bigtable_admin_v2.types import table as admin_messages_v2_pb2
52
+ from google.cloud.bigtable_admin_v2.types import (
53
+ bigtable_table_admin as table_admin_messages_v2_pb2,
54
+ )
55
+
56
+ # Maximum number of mutations in bulk (MutateRowsRequest message):
57
+ # (https://cloud.google.com/bigtable/docs/reference/data/rpc/
58
+ # google.bigtable.v2#google.bigtable.v2.MutateRowRequest)
59
+ _MAX_BULK_MUTATIONS = 100000
60
+ VIEW_NAME_ONLY = enums.Table.View.NAME_ONLY
61
+
62
+ RETRYABLE_MUTATION_ERRORS = (
63
+ Aborted,
64
+ DeadlineExceeded,
65
+ ServiceUnavailable,
66
+ InternalServerError,
67
+ )
68
+ """Errors which can be retried during row mutation."""
69
+
70
+
71
+ RETRYABLE_CODES: Set[int] = set()
72
+
73
+ for retryable in RETRYABLE_MUTATION_ERRORS:
74
+ if retryable.grpc_status_code is not None: # pragma: NO COVER
75
+ RETRYABLE_CODES.add(retryable.grpc_status_code.value[0])
76
+
77
+
78
+ class _BigtableRetryableError(Exception):
79
+ """Retry-able error expected by the default retry strategy."""
80
+
81
+
82
+ DEFAULT_RETRY = Retry(
83
+ predicate=if_exception_type(_BigtableRetryableError),
84
+ initial=1.0,
85
+ maximum=15.0,
86
+ multiplier=2.0,
87
+ deadline=120.0, # 2 minutes
88
+ )
89
+ """The default retry strategy to be used on retry-able errors.
90
+
91
+ Used by :meth:`~google.cloud.bigtable.table.Table.mutate_rows`.
92
+ """
93
+
94
+
95
+ class TableMismatchError(ValueError):
96
+ """Row from another table."""
97
+
98
+
99
+ class TooManyMutationsError(ValueError):
100
+ """The number of mutations for bulk request is too big."""
101
+
102
+
103
+ class Table(object):
104
+ """Representation of a Google Cloud Bigtable Table.
105
+
106
+ .. note::
107
+
108
+ We don't define any properties on a table other than the name.
109
+ The only other fields are ``column_families`` and ``granularity``,
110
+ The ``column_families`` are not stored locally and
111
+ ``granularity`` is an enum with only one value.
112
+
113
+ We can use a :class:`Table` to:
114
+
115
+ * :meth:`create` the table
116
+ * :meth:`delete` the table
117
+ * :meth:`list_column_families` in the table
118
+
119
+ :type table_id: str
120
+ :param table_id: The ID of the table.
121
+
122
+ :type instance: :class:`~google.cloud.bigtable.instance.Instance`
123
+ :param instance: The instance that owns the table.
124
+
125
+ :type app_profile_id: str
126
+ :param app_profile_id: (Optional) The unique name of the AppProfile.
127
+ """
128
+
129
+ def __init__(self, table_id, instance, mutation_timeout=None, app_profile_id=None):
130
+ self.table_id = table_id
131
+ self._instance = instance
132
+ self._app_profile_id = app_profile_id
133
+ self.mutation_timeout = mutation_timeout
134
+
135
+ @property
136
+ def name(self):
137
+ """Table name used in requests.
138
+
139
+ For example:
140
+
141
+ .. literalinclude:: snippets_table.py
142
+ :start-after: [START bigtable_api_table_name]
143
+ :end-before: [END bigtable_api_table_name]
144
+ :dedent: 4
145
+
146
+ .. note::
147
+
148
+ This property will not change if ``table_id`` does not, but the
149
+ return value is not cached.
150
+
151
+ The table name is of the form
152
+
153
+ ``"projects/../instances/../tables/{table_id}"``
154
+
155
+ :rtype: str
156
+ :returns: The table name.
157
+ """
158
+ project = self._instance._client.project
159
+ instance_id = self._instance.instance_id
160
+ table_client = self._instance._client.table_data_client
161
+ return table_client.table_path(
162
+ project=project, instance=instance_id, table=self.table_id
163
+ )
164
+
165
+ def get_iam_policy(self):
166
+ """Gets the IAM access control policy for this table.
167
+
168
+ For example:
169
+
170
+ .. literalinclude:: snippets_table.py
171
+ :start-after: [START bigtable_api_table_get_iam_policy]
172
+ :end-before: [END bigtable_api_table_get_iam_policy]
173
+ :dedent: 4
174
+
175
+ :rtype: :class:`google.cloud.bigtable.policy.Policy`
176
+ :returns: The current IAM policy of this table.
177
+ """
178
+ table_client = self._instance._client.table_admin_client
179
+ resp = table_client.get_iam_policy(request={"resource": self.name})
180
+ return Policy.from_pb(resp)
181
+
182
+ def set_iam_policy(self, policy):
183
+ """Sets the IAM access control policy for this table. Replaces any
184
+ existing policy.
185
+
186
+ For more information about policy, please see documentation of
187
+ class `google.cloud.bigtable.policy.Policy`
188
+
189
+ For example:
190
+
191
+ .. literalinclude:: snippets_table.py
192
+ :start-after: [START bigtable_api_table_set_iam_policy]
193
+ :end-before: [END bigtable_api_table_set_iam_policy]
194
+ :dedent: 4
195
+
196
+ :type policy: :class:`google.cloud.bigtable.policy.Policy`
197
+ :param policy: A new IAM policy to replace the current IAM policy
198
+ of this table.
199
+
200
+ :rtype: :class:`google.cloud.bigtable.policy.Policy`
201
+ :returns: The current IAM policy of this table.
202
+ """
203
+ table_client = self._instance._client.table_admin_client
204
+ resp = table_client.set_iam_policy(
205
+ request={"resource": self.name, "policy": policy.to_pb()}
206
+ )
207
+ return Policy.from_pb(resp)
208
+
209
+ def test_iam_permissions(self, permissions):
210
+ """Tests whether the caller has the given permissions for this table.
211
+ Returns the permissions that the caller has.
212
+
213
+ For example:
214
+
215
+ .. literalinclude:: snippets_table.py
216
+ :start-after: [START bigtable_api_table_test_iam_permissions]
217
+ :end-before: [END bigtable_api_table_test_iam_permissions]
218
+ :dedent: 4
219
+
220
+ :type permissions: list
221
+ :param permissions: The set of permissions to check for
222
+ the ``resource``. Permissions with wildcards (such as '*'
223
+ or 'storage.*') are not allowed. For more information see
224
+ `IAM Overview
225
+ <https://cloud.google.com/iam/docs/overview#permissions>`_.
226
+ `Bigtable Permissions
227
+ <https://cloud.google.com/bigtable/docs/access-control>`_.
228
+
229
+ :rtype: list
230
+ :returns: A List(string) of permissions allowed on the table.
231
+ """
232
+ table_client = self._instance._client.table_admin_client
233
+ resp = table_client.test_iam_permissions(
234
+ request={"resource": self.name, "permissions": permissions}
235
+ )
236
+ return list(resp.permissions)
237
+
238
+ def column_family(self, column_family_id, gc_rule=None):
239
+ """Factory to create a column family associated with this table.
240
+
241
+ For example:
242
+
243
+ .. literalinclude:: snippets_table.py
244
+ :start-after: [START bigtable_api_table_column_family]
245
+ :end-before: [END bigtable_api_table_column_family]
246
+ :dedent: 4
247
+
248
+ :type column_family_id: str
249
+ :param column_family_id: The ID of the column family. Must be of the
250
+ form ``[_a-zA-Z0-9][-_.a-zA-Z0-9]*``.
251
+
252
+ :type gc_rule: :class:`.GarbageCollectionRule`
253
+ :param gc_rule: (Optional) The garbage collection settings for this
254
+ column family.
255
+
256
+ :rtype: :class:`.ColumnFamily`
257
+ :returns: A column family owned by this table.
258
+ """
259
+ return ColumnFamily(column_family_id, self, gc_rule=gc_rule)
260
+
261
+ def row(self, row_key, filter_=None, append=False):
262
+ """Factory to create a row associated with this table.
263
+
264
+ For example:
265
+
266
+ .. literalinclude:: snippets_table.py
267
+ :start-after: [START bigtable_api_table_row]
268
+ :end-before: [END bigtable_api_table_row]
269
+ :dedent: 4
270
+
271
+ .. warning::
272
+
273
+ At most one of ``filter_`` and ``append`` can be used in a
274
+ :class:`~google.cloud.bigtable.row.Row`.
275
+
276
+ :type row_key: bytes
277
+ :param row_key: The key for the row being created.
278
+
279
+ :type filter_: :class:`.RowFilter`
280
+ :param filter_: (Optional) Filter to be used for conditional mutations.
281
+ See :class:`.ConditionalRow` for more details.
282
+
283
+ :type append: bool
284
+ :param append: (Optional) Flag to determine if the row should be used
285
+ for append mutations.
286
+
287
+ :rtype: :class:`~google.cloud.bigtable.row.Row`
288
+ :returns: A row owned by this table.
289
+ :raises: :class:`ValueError <exceptions.ValueError>` if both
290
+ ``filter_`` and ``append`` are used.
291
+ """
292
+ warnings.warn(
293
+ "This method will be deprecated in future versions. Please "
294
+ "use Table.append_row(), Table.conditional_row() "
295
+ "and Table.direct_row() methods instead.",
296
+ PendingDeprecationWarning,
297
+ stacklevel=2,
298
+ )
299
+
300
+ if append and filter_ is not None:
301
+ raise ValueError("At most one of filter_ and append can be set")
302
+ if append:
303
+ return AppendRow(row_key, self)
304
+ elif filter_ is not None:
305
+ return ConditionalRow(row_key, self, filter_=filter_)
306
+ else:
307
+ return DirectRow(row_key, self)
308
+
309
+ def append_row(self, row_key):
310
+ """Create a :class:`~google.cloud.bigtable.row.AppendRow` associated with this table.
311
+
312
+ For example:
313
+
314
+ .. literalinclude:: snippets_table.py
315
+ :start-after: [START bigtable_api_table_append_row]
316
+ :end-before: [END bigtable_api_table_append_row]
317
+ :dedent: 4
318
+
319
+ Args:
320
+ row_key (bytes): The key for the row being created.
321
+
322
+ Returns:
323
+ A row owned by this table.
324
+ """
325
+ return AppendRow(row_key, self)
326
+
327
+ def direct_row(self, row_key):
328
+ """Create a :class:`~google.cloud.bigtable.row.DirectRow` associated with this table.
329
+
330
+ For example:
331
+
332
+ .. literalinclude:: snippets_table.py
333
+ :start-after: [START bigtable_api_table_direct_row]
334
+ :end-before: [END bigtable_api_table_direct_row]
335
+ :dedent: 4
336
+
337
+ Args:
338
+ row_key (bytes): The key for the row being created.
339
+
340
+ Returns:
341
+ A row owned by this table.
342
+ """
343
+ return DirectRow(row_key, self)
344
+
345
+ def conditional_row(self, row_key, filter_):
346
+ """Create a :class:`~google.cloud.bigtable.row.ConditionalRow` associated with this table.
347
+
348
+ For example:
349
+
350
+ .. literalinclude:: snippets_table.py
351
+ :start-after: [START bigtable_api_table_conditional_row]
352
+ :end-before: [END bigtable_api_table_conditional_row]
353
+ :dedent: 4
354
+
355
+ Args:
356
+ row_key (bytes): The key for the row being created.
357
+
358
+ filter_ (:class:`.RowFilter`): (Optional) Filter to be used for
359
+ conditional mutations. See :class:`.ConditionalRow` for more details.
360
+
361
+ Returns:
362
+ A row owned by this table.
363
+ """
364
+ return ConditionalRow(row_key, self, filter_=filter_)
365
+
366
+ def __eq__(self, other):
367
+ if not isinstance(other, self.__class__):
368
+ return NotImplemented
369
+ return other.table_id == self.table_id and other._instance == self._instance
370
+
371
+ def __ne__(self, other):
372
+ return not self == other
373
+
374
+ def create(self, initial_split_keys=[], column_families={}):
375
+ """Creates this table.
376
+
377
+ For example:
378
+
379
+ .. literalinclude:: snippets_table.py
380
+ :start-after: [START bigtable_api_create_table]
381
+ :end-before: [END bigtable_api_create_table]
382
+ :dedent: 4
383
+
384
+ .. note::
385
+
386
+ A create request returns a
387
+ :class:`._generated.table.Table` but we don't use
388
+ this response.
389
+
390
+ :type initial_split_keys: list
391
+ :param initial_split_keys: (Optional) list of row keys in bytes that
392
+ will be used to initially split the table
393
+ into several tablets.
394
+
395
+ :type column_families: dict
396
+ :param column_families: (Optional) A map columns to create. The key is
397
+ the column_id str and the value is a
398
+ :class:`GarbageCollectionRule`
399
+ """
400
+ table_client = self._instance._client.table_admin_client
401
+ instance_name = self._instance.name
402
+
403
+ families = {
404
+ id: ColumnFamily(id, self, rule).to_pb()
405
+ for (id, rule) in column_families.items()
406
+ }
407
+ table = admin_messages_v2_pb2.Table(column_families=families)
408
+
409
+ split = table_admin_messages_v2_pb2.CreateTableRequest.Split
410
+ splits = [split(key=_to_bytes(key)) for key in initial_split_keys]
411
+
412
+ table_client.create_table(
413
+ request={
414
+ "parent": instance_name,
415
+ "table_id": self.table_id,
416
+ "table": table,
417
+ "initial_splits": splits,
418
+ }
419
+ )
420
+
421
+ def exists(self):
422
+ """Check whether the table exists.
423
+
424
+ For example:
425
+
426
+ .. literalinclude:: snippets_table.py
427
+ :start-after: [START bigtable_api_check_table_exists]
428
+ :end-before: [END bigtable_api_check_table_exists]
429
+ :dedent: 4
430
+
431
+ :rtype: bool
432
+ :returns: True if the table exists, else False.
433
+ """
434
+ table_client = self._instance._client.table_admin_client
435
+ try:
436
+ table_client.get_table(request={"name": self.name, "view": VIEW_NAME_ONLY})
437
+ return True
438
+ except NotFound:
439
+ return False
440
+
441
+ def delete(self):
442
+ """Delete this table.
443
+
444
+ For example:
445
+
446
+ .. literalinclude:: snippets_table.py
447
+ :start-after: [START bigtable_api_delete_table]
448
+ :end-before: [END bigtable_api_delete_table]
449
+ :dedent: 4
450
+ """
451
+ table_client = self._instance._client.table_admin_client
452
+ table_client.delete_table(request={"name": self.name})
453
+
454
+ def list_column_families(self):
455
+ """List the column families owned by this table.
456
+
457
+ For example:
458
+
459
+ .. literalinclude:: snippets_table.py
460
+ :start-after: [START bigtable_api_list_column_families]
461
+ :end-before: [END bigtable_api_list_column_families]
462
+ :dedent: 4
463
+
464
+ :rtype: dict
465
+ :returns: Dictionary of column families attached to this table. Keys
466
+ are strings (column family names) and values are
467
+ :class:`.ColumnFamily` instances.
468
+ :raises: :class:`ValueError <exceptions.ValueError>` if the column
469
+ family name from the response does not agree with the computed
470
+ name from the column family ID.
471
+ """
472
+ table_client = self._instance._client.table_admin_client
473
+ table_pb = table_client.get_table(request={"name": self.name})
474
+
475
+ result = {}
476
+ for column_family_id, value_pb in table_pb.column_families.items():
477
+ gc_rule = _gc_rule_from_pb(value_pb.gc_rule)
478
+ column_family = self.column_family(column_family_id, gc_rule=gc_rule)
479
+ result[column_family_id] = column_family
480
+ return result
481
+
482
+ def get_cluster_states(self):
483
+ """List the cluster states owned by this table.
484
+
485
+ For example:
486
+
487
+ .. literalinclude:: snippets_table.py
488
+ :start-after: [START bigtable_api_get_cluster_states]
489
+ :end-before: [END bigtable_api_get_cluster_states]
490
+ :dedent: 4
491
+
492
+ :rtype: dict
493
+ :returns: Dictionary of cluster states for this table.
494
+ Keys are cluster ids and values are
495
+ :class: 'ClusterState' instances.
496
+ """
497
+
498
+ REPLICATION_VIEW = enums.Table.View.REPLICATION_VIEW
499
+ table_client = self._instance._client.table_admin_client
500
+ table_pb = table_client.get_table(
501
+ request={"name": self.name, "view": REPLICATION_VIEW}
502
+ )
503
+
504
+ return {
505
+ cluster_id: ClusterState(value_pb.replication_state)
506
+ for cluster_id, value_pb in table_pb.cluster_states.items()
507
+ }
508
+
509
+ def get_encryption_info(self):
510
+ """List the encryption info for each cluster owned by this table.
511
+
512
+ Gets the current encryption info for the table across all of the clusters. The
513
+ returned dict will be keyed by cluster id and contain a status for all of the
514
+ keys in use.
515
+
516
+ :rtype: dict
517
+ :returns: Dictionary of encryption info for this table. Keys are cluster ids and
518
+ values are tuples of :class:`google.cloud.bigtable.encryption.EncryptionInfo` instances.
519
+ """
520
+ ENCRYPTION_VIEW = enums.Table.View.ENCRYPTION_VIEW
521
+ table_client = self._instance._client.table_admin_client
522
+ table_pb = table_client.get_table(
523
+ request={"name": self.name, "view": ENCRYPTION_VIEW}
524
+ )
525
+
526
+ return {
527
+ cluster_id: tuple(
528
+ (
529
+ EncryptionInfo._from_pb(info_pb)
530
+ for info_pb in value_pb.encryption_info
531
+ )
532
+ )
533
+ for cluster_id, value_pb in table_pb.cluster_states.items()
534
+ }
535
+
536
+ def read_row(self, row_key, filter_=None, retry=DEFAULT_RETRY_READ_ROWS):
537
+ """Read a single row from this table.
538
+
539
+ For example:
540
+
541
+ .. literalinclude:: snippets_table.py
542
+ :start-after: [START bigtable_api_read_row]
543
+ :end-before: [END bigtable_api_read_row]
544
+ :dedent: 4
545
+
546
+ :type row_key: bytes
547
+ :param row_key: The key of the row to read from.
548
+
549
+ :type filter_: :class:`.RowFilter`
550
+ :param filter_: (Optional) The filter to apply to the contents of the
551
+ row. If unset, returns the entire row.
552
+
553
+ :type retry: :class:`~google.api_core.retry.Retry`
554
+ :param retry:
555
+ (Optional) Retry delay and deadline arguments. To override, the
556
+ default value :attr:`DEFAULT_RETRY_READ_ROWS` can be used and
557
+ modified with the :meth:`~google.api_core.retry.Retry.with_delay`
558
+ method or the :meth:`~google.api_core.retry.Retry.with_deadline`
559
+ method.
560
+
561
+ :rtype: :class:`.PartialRowData`, :data:`NoneType <types.NoneType>`
562
+ :returns: The contents of the row if any chunks were returned in
563
+ the response, otherwise :data:`None`.
564
+ :raises: :class:`ValueError <exceptions.ValueError>` if a commit row
565
+ chunk is never encountered.
566
+ """
567
+ row_set = RowSet()
568
+ row_set.add_row_key(row_key)
569
+ result_iter = iter(
570
+ self.read_rows(filter_=filter_, row_set=row_set, retry=retry)
571
+ )
572
+ row = next(result_iter, None)
573
+ if next(result_iter, None) is not None:
574
+ raise ValueError("More than one row was returned.")
575
+ return row
576
+
577
+ def read_rows(
578
+ self,
579
+ start_key=None,
580
+ end_key=None,
581
+ limit=None,
582
+ filter_=None,
583
+ end_inclusive=False,
584
+ row_set=None,
585
+ retry=DEFAULT_RETRY_READ_ROWS,
586
+ ):
587
+ """Read rows from this table.
588
+
589
+ For example:
590
+
591
+ .. literalinclude:: snippets_table.py
592
+ :start-after: [START bigtable_api_read_rows]
593
+ :end-before: [END bigtable_api_read_rows]
594
+ :dedent: 4
595
+
596
+ :type start_key: bytes
597
+ :param start_key: (Optional) The beginning of a range of row keys to
598
+ read from. The range will include ``start_key``. If
599
+ left empty, will be interpreted as the empty string.
600
+
601
+ :type end_key: bytes
602
+ :param end_key: (Optional) The end of a range of row keys to read from.
603
+ The range will not include ``end_key``. If left empty,
604
+ will be interpreted as an infinite string.
605
+
606
+ :type limit: int
607
+ :param limit: (Optional) The read will terminate after committing to N
608
+ rows' worth of results. The default (zero) is to return
609
+ all results.
610
+
611
+ :type filter_: :class:`.RowFilter`
612
+ :param filter_: (Optional) The filter to apply to the contents of the
613
+ specified row(s). If unset, reads every column in
614
+ each row.
615
+
616
+ :type end_inclusive: bool
617
+ :param end_inclusive: (Optional) Whether the ``end_key`` should be
618
+ considered inclusive. The default is False (exclusive).
619
+
620
+ :type row_set: :class:`.RowSet`
621
+ :param row_set: (Optional) The row set containing multiple row keys and
622
+ row_ranges.
623
+
624
+ :type retry: :class:`~google.api_core.retry.Retry`
625
+ :param retry:
626
+ (Optional) Retry delay and deadline arguments. To override, the
627
+ default value :attr:`DEFAULT_RETRY_READ_ROWS` can be used and
628
+ modified with the :meth:`~google.api_core.retry.Retry.with_delay`
629
+ method or the :meth:`~google.api_core.retry.Retry.with_deadline`
630
+ method.
631
+
632
+ :rtype: :class:`.PartialRowsData`
633
+ :returns: A :class:`.PartialRowsData` a generator for consuming
634
+ the streamed results.
635
+ """
636
+ request_pb = _create_row_request(
637
+ self.name,
638
+ start_key=start_key,
639
+ end_key=end_key,
640
+ filter_=filter_,
641
+ limit=limit,
642
+ end_inclusive=end_inclusive,
643
+ app_profile_id=self._app_profile_id,
644
+ row_set=row_set,
645
+ )
646
+ data_client = self._instance._client.table_data_client
647
+ return PartialRowsData(data_client.read_rows, request_pb, retry)
648
+
649
+ def yield_rows(self, **kwargs):
650
+ """Read rows from this table.
651
+
652
+ .. warning::
653
+ This method will be removed in future releases. Please use
654
+ ``read_rows`` instead.
655
+
656
+ :type start_key: bytes
657
+ :param start_key: (Optional) The beginning of a range of row keys to
658
+ read from. The range will include ``start_key``. If
659
+ left empty, will be interpreted as the empty string.
660
+
661
+ :type end_key: bytes
662
+ :param end_key: (Optional) The end of a range of row keys to read from.
663
+ The range will not include ``end_key``. If left empty,
664
+ will be interpreted as an infinite string.
665
+
666
+ :type limit: int
667
+ :param limit: (Optional) The read will terminate after committing to N
668
+ rows' worth of results. The default (zero) is to return
669
+ all results.
670
+
671
+ :type filter_: :class:`.RowFilter`
672
+ :param filter_: (Optional) The filter to apply to the contents of the
673
+ specified row(s). If unset, reads every column in
674
+ each row.
675
+
676
+ :type row_set: :class:`.RowSet`
677
+ :param row_set: (Optional) The row set containing multiple row keys and
678
+ row_ranges.
679
+
680
+ :rtype: :class:`.PartialRowData`
681
+ :returns: A :class:`.PartialRowData` for each row returned
682
+ """
683
+ warnings.warn(
684
+ "`yield_rows()` is deprecated; use `read_rows()` instead",
685
+ DeprecationWarning,
686
+ stacklevel=2,
687
+ )
688
+ return self.read_rows(**kwargs)
689
+
690
+ def mutate_rows(self, rows, retry=DEFAULT_RETRY, timeout=DEFAULT):
691
+ """Mutates multiple rows in bulk.
692
+
693
+ For example:
694
+
695
+ .. literalinclude:: snippets_table.py
696
+ :start-after: [START bigtable_api_mutate_rows]
697
+ :end-before: [END bigtable_api_mutate_rows]
698
+ :dedent: 4
699
+
700
+ The method tries to update all specified rows.
701
+ If some of the rows weren't updated, it would not remove mutations.
702
+ They can be applied to the row separately.
703
+ If row mutations finished successfully, they would be cleaned up.
704
+
705
+ Optionally, a ``retry`` strategy can be specified to re-attempt
706
+ mutations on rows that return transient errors. This method will retry
707
+ until all rows succeed or until the request deadline is reached. To
708
+ specify a ``retry`` strategy of "do-nothing", a deadline of ``0.0``
709
+ can be specified.
710
+
711
+ :type rows: list
712
+ :param rows: List or other iterable of :class:`.DirectRow` instances.
713
+
714
+ :type retry: :class:`~google.api_core.retry.Retry`
715
+ :param retry:
716
+ (Optional) Retry delay and deadline arguments. To override, the
717
+ default value :attr:`DEFAULT_RETRY` can be used and modified with
718
+ the :meth:`~google.api_core.retry.Retry.with_delay` method or the
719
+ :meth:`~google.api_core.retry.Retry.with_deadline` method.
720
+
721
+ :type timeout: float
722
+ :param timeout: number of seconds bounding retries for the call
723
+
724
+ :rtype: list
725
+ :returns: A list of response statuses (`google.rpc.status_pb2.Status`)
726
+ corresponding to success or failure of each row mutation
727
+ sent. These will be in the same order as the `rows`.
728
+ """
729
+ if timeout is DEFAULT:
730
+ timeout = self.mutation_timeout
731
+
732
+ retryable_mutate_rows = _RetryableMutateRowsWorker(
733
+ self._instance._client,
734
+ self.name,
735
+ rows,
736
+ app_profile_id=self._app_profile_id,
737
+ timeout=timeout,
738
+ )
739
+ return retryable_mutate_rows(retry=retry)
740
+
741
+ def sample_row_keys(self):
742
+ """Read a sample of row keys in the table.
743
+
744
+ For example:
745
+
746
+ .. literalinclude:: snippets_table.py
747
+ :start-after: [START bigtable_api_sample_row_keys]
748
+ :end-before: [END bigtable_api_sample_row_keys]
749
+ :dedent: 4
750
+
751
+ The returned row keys will delimit contiguous sections of the table of
752
+ approximately equal size, which can be used to break up the data for
753
+ distributed tasks like mapreduces.
754
+
755
+ The elements in the iterator are a SampleRowKeys response and they have
756
+ the properties ``offset_bytes`` and ``row_key``. They occur in sorted
757
+ order. The table might have contents before the first row key in the
758
+ list and after the last one, but a key containing the empty string
759
+ indicates "end of table" and will be the last response given, if
760
+ present.
761
+
762
+ .. note::
763
+
764
+ Row keys in this list may not have ever been written to or read
765
+ from, and users should therefore not make any assumptions about the
766
+ row key structure that are specific to their use case.
767
+
768
+ The ``offset_bytes`` field on a response indicates the approximate
769
+ total storage space used by all rows in the table which precede
770
+ ``row_key``. Buffering the contents of all rows between two subsequent
771
+ samples would require space roughly equal to the difference in their
772
+ ``offset_bytes`` fields.
773
+
774
+ :rtype: :class:`~google.cloud.exceptions.GrpcRendezvous`
775
+ :returns: A cancel-able iterator. Can be consumed by calling ``next()``
776
+ or by casting to a :class:`list` and can be cancelled by
777
+ calling ``cancel()``.
778
+ """
779
+ data_client = self._instance._client.table_data_client
780
+ response_iterator = data_client.sample_row_keys(
781
+ request={"table_name": self.name, "app_profile_id": self._app_profile_id}
782
+ )
783
+
784
+ return response_iterator
785
+
786
+ def truncate(self, timeout=None):
787
+ """Truncate the table
788
+
789
+ For example:
790
+
791
+ .. literalinclude:: snippets_table.py
792
+ :start-after: [START bigtable_api_truncate_table]
793
+ :end-before: [END bigtable_api_truncate_table]
794
+ :dedent: 4
795
+
796
+ :type timeout: float
797
+ :param timeout: (Optional) The amount of time, in seconds, to wait
798
+ for the request to complete.
799
+
800
+ :raise: google.api_core.exceptions.GoogleAPICallError: If the
801
+ request failed for any reason.
802
+ google.api_core.exceptions.RetryError: If the request failed
803
+ due to a retryable error and retry attempts failed.
804
+ ValueError: If the parameters are invalid.
805
+ """
806
+ client = self._instance._client
807
+ table_admin_client = client.table_admin_client
808
+ if timeout:
809
+ table_admin_client.drop_row_range(
810
+ request={"name": self.name, "delete_all_data_from_table": True},
811
+ timeout=timeout,
812
+ )
813
+ else:
814
+ table_admin_client.drop_row_range(
815
+ request={"name": self.name, "delete_all_data_from_table": True}
816
+ )
817
+
818
+ def drop_by_prefix(self, row_key_prefix, timeout=None):
819
+ """
820
+
821
+ For example:
822
+
823
+ .. literalinclude:: snippets_table.py
824
+ :start-after: [START bigtable_api_drop_by_prefix]
825
+ :end-before: [END bigtable_api_drop_by_prefix]
826
+ :dedent: 4
827
+
828
+ :type row_key_prefix: bytes
829
+ :param row_key_prefix: Delete all rows that start with this row key
830
+ prefix. Prefix cannot be zero length.
831
+
832
+ :type timeout: float
833
+ :param timeout: (Optional) The amount of time, in seconds, to wait
834
+ for the request to complete.
835
+
836
+ :raise: google.api_core.exceptions.GoogleAPICallError: If the
837
+ request failed for any reason.
838
+ google.api_core.exceptions.RetryError: If the request failed
839
+ due to a retryable error and retry attempts failed.
840
+ ValueError: If the parameters are invalid.
841
+ """
842
+ client = self._instance._client
843
+ table_admin_client = client.table_admin_client
844
+ if timeout:
845
+ table_admin_client.drop_row_range(
846
+ request={
847
+ "name": self.name,
848
+ "row_key_prefix": _to_bytes(row_key_prefix),
849
+ },
850
+ timeout=timeout,
851
+ )
852
+ else:
853
+ table_admin_client.drop_row_range(
854
+ request={"name": self.name, "row_key_prefix": _to_bytes(row_key_prefix)}
855
+ )
856
+
857
+ def mutations_batcher(
858
+ self, flush_count=FLUSH_COUNT, max_row_bytes=MAX_MUTATION_SIZE
859
+ ):
860
+ """Factory to create a mutation batcher associated with this instance.
861
+
862
+ For example:
863
+
864
+ .. literalinclude:: snippets_table.py
865
+ :start-after: [START bigtable_api_mutations_batcher]
866
+ :end-before: [END bigtable_api_mutations_batcher]
867
+ :dedent: 4
868
+
869
+ :type flush_count: int
870
+ :param flush_count: (Optional) Maximum number of rows per batch. If it
871
+ reaches the max number of rows it calls finish_batch() to
872
+ mutate the current row batch. Default is FLUSH_COUNT (1000
873
+ rows).
874
+
875
+ :type max_row_bytes: int
876
+ :param max_row_bytes: (Optional) Max number of row mutations size to
877
+ flush. If it reaches the max number of row mutations size it
878
+ calls finish_batch() to mutate the current row batch.
879
+ Default is MAX_ROW_BYTES (5 MB).
880
+ """
881
+ return MutationsBatcher(self, flush_count, max_row_bytes)
882
+
883
+ def backup(self, backup_id, cluster_id=None, expire_time=None):
884
+ """Factory to create a Backup linked to this Table.
885
+
886
+ :type backup_id: str
887
+ :param backup_id: The ID of the Backup to be created.
888
+
889
+ :type cluster_id: str
890
+ :param cluster_id: (Optional) The ID of the Cluster. Required for
891
+ calling 'delete', 'exists' etc. methods.
892
+
893
+ :type expire_time: :class:`datetime.datetime`
894
+ :param expire_time: (Optional) The expiration time of this new Backup.
895
+ Required, if the `create` method needs to be called.
896
+ :rtype: :class:`.Backup`
897
+ :returns: A backup linked to this table.
898
+ """
899
+ return Backup(
900
+ backup_id,
901
+ self._instance,
902
+ cluster_id=cluster_id,
903
+ table_id=self.table_id,
904
+ expire_time=expire_time,
905
+ )
906
+
907
+ def list_backups(self, cluster_id=None, filter_=None, order_by=None, page_size=0):
908
+ """List Backups for this Table.
909
+
910
+ :type cluster_id: str
911
+ :param cluster_id: (Optional) Specifies a single cluster to list
912
+ Backups from. If none is specified, the returned list
913
+ contains all the Backups in this Instance.
914
+
915
+ :type filter_: str
916
+ :param filter_: (Optional) A filter expression that filters backups
917
+ listed in the response. The expression must specify
918
+ the field name, a comparison operator, and the value
919
+ that you want to use for filtering. The value must be
920
+ a string, a number, or a boolean. The comparison
921
+ operator must be <, >, <=, >=, !=, =, or :. Colon ':'
922
+ represents a HAS operator which is roughly synonymous
923
+ with equality. Filter rules are case insensitive.
924
+
925
+ The fields eligible for filtering are:
926
+
927
+ - ``name``
928
+ - ``source_table``
929
+ - ``state``
930
+ - ``start_time`` (values of the format YYYY-MM-DDTHH:MM:SSZ)
931
+ - ``end_time`` (values of the format YYYY-MM-DDTHH:MM:SSZ)
932
+ - ``expire_time`` (values of the format YYYY-MM-DDTHH:MM:SSZ)
933
+ - ``size_bytes``
934
+
935
+ To filter on multiple expressions, provide each
936
+ separate expression within parentheses. By default,
937
+ each expression is an AND expression. However, you can
938
+ include AND, OR, and NOT expressions explicitly.
939
+
940
+ Some examples of using filters are:
941
+
942
+ - ``name:"exact"`` --> The Backup name is the string "exact".
943
+ - ``name:howl`` --> The Backup name contains the string "howl"
944
+ - ``source_table:prod`` --> The source table's name contains
945
+ the string "prod".
946
+ - ``state:CREATING`` --> The Backup is pending creation.
947
+ - ``state:READY`` --> The Backup is created and ready for use.
948
+ - ``(name:howl) AND (start_time < \"2020-05-28T14:50:00Z\")``
949
+ --> The Backup name contains the string "howl" and
950
+ the Backup start time is before 2020-05-28T14:50:00Z.
951
+ - ``size_bytes > 10000000000`` --> The Backup size is greater
952
+ than 10GB
953
+
954
+ :type order_by: str
955
+ :param order_by: (Optional) An expression for specifying the sort order
956
+ of the results of the request. The string value should
957
+ specify one or more fields in ``Backup``. The full
958
+ syntax is described at https://aip.dev/132#ordering.
959
+
960
+ Fields supported are: \\* name \\* source_table \\*
961
+ expire_time \\* start_time \\* end_time \\*
962
+ size_bytes \\* state
963
+
964
+ For example, "start_time". The default sorting order
965
+ is ascending. To specify descending order for the
966
+ field, a suffix " desc" should be appended to the
967
+ field name. For example, "start_time desc". Redundant
968
+ space characters in the syntax are insigificant. If
969
+ order_by is empty, results will be sorted by
970
+ ``start_time`` in descending order starting from
971
+ the most recently created backup.
972
+
973
+ :type page_size: int
974
+ :param page_size: (Optional) The maximum number of resources contained
975
+ in the underlying API response. If page streaming is
976
+ performed per-resource, this parameter does not
977
+ affect the return value. If page streaming is
978
+ performed per-page, this determines the maximum
979
+ number of resources in a page.
980
+
981
+ :rtype: :class:`~google.api_core.page_iterator.Iterator`
982
+ :returns: Iterator of :class:`~google.cloud.bigtable.backup.Backup`
983
+ resources within the current Instance.
984
+ :raises: :class:`ValueError <exceptions.ValueError>` if one of the
985
+ returned Backups' name is not of the expected format.
986
+ """
987
+ cluster_id = cluster_id or "-"
988
+
989
+ backups_filter = "source_table:{}".format(self.name)
990
+ if filter_:
991
+ backups_filter = "({}) AND ({})".format(backups_filter, filter_)
992
+
993
+ parent = BigtableTableAdminClient.cluster_path(
994
+ project=self._instance._client.project,
995
+ instance=self._instance.instance_id,
996
+ cluster=cluster_id,
997
+ )
998
+ client = self._instance._client.table_admin_client
999
+ backup_list_pb = client.list_backups(
1000
+ request={
1001
+ "parent": parent,
1002
+ "filter": backups_filter,
1003
+ "order_by": order_by,
1004
+ "page_size": page_size,
1005
+ }
1006
+ )
1007
+
1008
+ result = []
1009
+ for backup_pb in backup_list_pb.backups:
1010
+ result.append(Backup.from_pb(backup_pb, self._instance))
1011
+
1012
+ return result
1013
+
1014
+ def restore(self, new_table_id, cluster_id=None, backup_id=None, backup_name=None):
1015
+ """Creates a new Table by restoring from the Backup specified by either
1016
+ `backup_id` or `backup_name`. The returned ``long-running operation``
1017
+ can be used to track the progress of the operation and to cancel it.
1018
+ The ``response`` type is ``Table``, if successful.
1019
+
1020
+ :type new_table_id: str
1021
+ :param new_table_id: The ID of the Table to create and restore to.
1022
+ This Table must not already exist.
1023
+
1024
+ :type cluster_id: str
1025
+ :param cluster_id: The ID of the Cluster containing the Backup.
1026
+ This parameter gets overriden by `backup_name`, if
1027
+ the latter is provided.
1028
+
1029
+ :type backup_id: str
1030
+ :param backup_id: The ID of the Backup to restore the Table from.
1031
+ This parameter gets overriden by `backup_name`, if
1032
+ the latter is provided.
1033
+
1034
+ :type backup_name: str
1035
+ :param backup_name: (Optional) The full name of the Backup to restore
1036
+ from. If specified, it overrides the `cluster_id`
1037
+ and `backup_id` parameters even of such specified.
1038
+
1039
+ :return: An instance of
1040
+ :class:`~google.cloud.bigtable_admin_v2.types._OperationFuture`.
1041
+
1042
+ :raises: google.api_core.exceptions.AlreadyExists: If the table
1043
+ already exists.
1044
+ :raises: google.api_core.exceptions.GoogleAPICallError: If the request
1045
+ failed for any reason.
1046
+ :raises: google.api_core.exceptions.RetryError: If the request failed
1047
+ due to a retryable error and retry attempts failed.
1048
+ :raises: ValueError: If the parameters are invalid.
1049
+ """
1050
+ api = self._instance._client.table_admin_client
1051
+ if not backup_name:
1052
+ backup_name = BigtableTableAdminClient.backup_path(
1053
+ project=self._instance._client.project,
1054
+ instance=self._instance.instance_id,
1055
+ cluster=cluster_id,
1056
+ backup=backup_id,
1057
+ )
1058
+ return api.restore_table(
1059
+ request={
1060
+ "parent": self._instance.name,
1061
+ "table_id": new_table_id,
1062
+ "backup": backup_name,
1063
+ }
1064
+ )
1065
+
1066
+
1067
+ class _RetryableMutateRowsWorker(object):
1068
+ """A callable worker that can retry to mutate rows with transient errors.
1069
+
1070
+ This class is a callable that can retry mutating rows that result in
1071
+ transient errors. After all rows are successful or none of the rows
1072
+ are retryable, any subsequent call on this callable will be a no-op.
1073
+ """
1074
+
1075
+ def __init__(self, client, table_name, rows, app_profile_id=None, timeout=None):
1076
+ self.client = client
1077
+ self.table_name = table_name
1078
+ self.rows = rows
1079
+ self.app_profile_id = app_profile_id
1080
+ self.responses_statuses = [None] * len(self.rows)
1081
+ self.timeout = timeout
1082
+
1083
+ def __call__(self, retry=DEFAULT_RETRY):
1084
+ """Attempt to mutate all rows and retry rows with transient errors.
1085
+
1086
+ Will retry the rows with transient errors until all rows succeed or
1087
+ ``deadline`` specified in the `retry` is reached.
1088
+
1089
+ :rtype: list
1090
+ :returns: A list of response statuses (`google.rpc.status_pb2.Status`)
1091
+ corresponding to success or failure of each row mutation
1092
+ sent. These will be in the same order as the ``rows``.
1093
+ """
1094
+ mutate_rows = self._do_mutate_retryable_rows
1095
+ if retry:
1096
+ mutate_rows = retry(self._do_mutate_retryable_rows)
1097
+
1098
+ try:
1099
+ mutate_rows()
1100
+ except (_BigtableRetryableError, RetryError):
1101
+ # - _BigtableRetryableError raised when no retry strategy is used
1102
+ # and a retryable error on a mutation occurred.
1103
+ # - RetryError raised when retry deadline is reached.
1104
+ # In both cases, just return current `responses_statuses`.
1105
+ pass
1106
+
1107
+ return self.responses_statuses
1108
+
1109
+ @staticmethod
1110
+ def _is_retryable(status):
1111
+ return status is None or status.code in RETRYABLE_CODES
1112
+
1113
+ def _do_mutate_retryable_rows(self):
1114
+ """Mutate all the rows that are eligible for retry.
1115
+
1116
+ A row is eligible for retry if it has not been tried or if it resulted
1117
+ in a transient error in a previous call.
1118
+
1119
+ :rtype: list
1120
+ :return: The responses statuses, which is a list of
1121
+ :class:`~google.rpc.status_pb2.Status`.
1122
+ :raises: One of the following:
1123
+
1124
+ * :exc:`~.table._BigtableRetryableError` if any
1125
+ row returned a transient error.
1126
+ * :exc:`RuntimeError` if the number of responses doesn't
1127
+ match the number of rows that were retried
1128
+ """
1129
+ retryable_rows = []
1130
+ index_into_all_rows = []
1131
+ for index, status in enumerate(self.responses_statuses):
1132
+ if self._is_retryable(status):
1133
+ retryable_rows.append(self.rows[index])
1134
+ index_into_all_rows.append(index)
1135
+
1136
+ if not retryable_rows:
1137
+ # All mutations are either successful or non-retryable now.
1138
+ return self.responses_statuses
1139
+
1140
+ entries = _compile_mutation_entries(self.table_name, retryable_rows)
1141
+ data_client = self.client.table_data_client
1142
+
1143
+ kwargs = {}
1144
+ if self.timeout is not None:
1145
+ kwargs["timeout"] = timeout.ExponentialTimeout(deadline=self.timeout)
1146
+
1147
+ try:
1148
+ responses = data_client.mutate_rows(
1149
+ table_name=self.table_name,
1150
+ entries=entries,
1151
+ app_profile_id=self.app_profile_id,
1152
+ retry=None,
1153
+ **kwargs
1154
+ )
1155
+ except RETRYABLE_MUTATION_ERRORS as exc:
1156
+ # If an exception, considered retryable by `RETRYABLE_MUTATION_ERRORS`, is
1157
+ # returned from the initial call, consider
1158
+ # it to be retryable. Wrap as a Bigtable Retryable Error.
1159
+ # For InternalServerError, it is only retriable if the message is related to RST Stream messages
1160
+ if _retriable_internal_server_error(exc) or not isinstance(
1161
+ exc, InternalServerError
1162
+ ):
1163
+ raise _BigtableRetryableError
1164
+ else:
1165
+ # re-raise the original exception
1166
+ raise
1167
+
1168
+ num_responses = 0
1169
+ num_retryable_responses = 0
1170
+ for response in responses:
1171
+ for entry in response.entries:
1172
+ num_responses += 1
1173
+ index = index_into_all_rows[entry.index]
1174
+ self.responses_statuses[index] = entry.status
1175
+ if self._is_retryable(entry.status):
1176
+ num_retryable_responses += 1
1177
+ if entry.status.code == 0:
1178
+ self.rows[index].clear()
1179
+
1180
+ if len(retryable_rows) != num_responses:
1181
+ raise RuntimeError(
1182
+ "Unexpected number of responses",
1183
+ num_responses,
1184
+ "Expected",
1185
+ len(retryable_rows),
1186
+ )
1187
+
1188
+ if num_retryable_responses:
1189
+ raise _BigtableRetryableError
1190
+
1191
+ return self.responses_statuses
1192
+
1193
+
1194
+ class ClusterState(object):
1195
+ """Representation of a Cluster State.
1196
+
1197
+ :type replication_state: int
1198
+ :param replication_state: enum value for cluster state
1199
+ Possible replications_state values are
1200
+ 0 for STATE_NOT_KNOWN: The replication state of the table is
1201
+ unknown in this cluster.
1202
+ 1 for INITIALIZING: The cluster was recently created, and the
1203
+ table must finish copying
1204
+ over pre-existing data from other clusters before it can
1205
+ begin receiving live replication updates and serving
1206
+ ``Data API`` requests.
1207
+ 2 for PLANNED_MAINTENANCE: The table is temporarily unable to
1208
+ serve
1209
+ ``Data API`` requests from this
1210
+ cluster due to planned internal maintenance.
1211
+ 3 for UNPLANNED_MAINTENANCE: The table is temporarily unable
1212
+ to serve
1213
+ ``Data API`` requests from this
1214
+ cluster due to unplanned or emergency maintenance.
1215
+ 4 for READY: The table can serve
1216
+ ``Data API`` requests from this
1217
+ cluster. Depending on replication delay, reads may not
1218
+ immediately reflect the state of the table in other clusters.
1219
+ """
1220
+
1221
+ def __init__(self, replication_state):
1222
+ self.replication_state = replication_state
1223
+
1224
+ def __repr__(self):
1225
+ """Representation of cluster state instance as string value
1226
+ for cluster state.
1227
+
1228
+ :rtype: ClusterState instance
1229
+ :returns: ClusterState instance as representation of string
1230
+ value for cluster state.
1231
+ """
1232
+ replication_dict = {
1233
+ enums.Table.ReplicationState.STATE_NOT_KNOWN: "STATE_NOT_KNOWN",
1234
+ enums.Table.ReplicationState.INITIALIZING: "INITIALIZING",
1235
+ enums.Table.ReplicationState.PLANNED_MAINTENANCE: "PLANNED_MAINTENANCE",
1236
+ enums.Table.ReplicationState.UNPLANNED_MAINTENANCE: "UNPLANNED_MAINTENANCE",
1237
+ enums.Table.ReplicationState.READY: "READY",
1238
+ }
1239
+ return replication_dict[self.replication_state]
1240
+
1241
+ def __eq__(self, other):
1242
+ """Checks if two ClusterState instances(self and other) are
1243
+ equal on the basis of instance variable 'replication_state'.
1244
+
1245
+ :type other: ClusterState
1246
+ :param other: ClusterState instance to compare with.
1247
+
1248
+ :rtype: Boolean value
1249
+ :returns: True if two cluster state instances have same
1250
+ replication_state.
1251
+ """
1252
+ if not isinstance(other, self.__class__):
1253
+ return False
1254
+ return self.replication_state == other.replication_state
1255
+
1256
+ def __ne__(self, other):
1257
+ """Checks if two ClusterState instances(self and other) are
1258
+ not equal.
1259
+
1260
+ :type other: ClusterState.
1261
+ :param other: ClusterState instance to compare with.
1262
+
1263
+ :rtype: Boolean value.
1264
+ :returns: True if two cluster state instances are not equal.
1265
+ """
1266
+ return not self == other
1267
+
1268
+
1269
+ def _create_row_request(
1270
+ table_name,
1271
+ start_key=None,
1272
+ end_key=None,
1273
+ filter_=None,
1274
+ limit=None,
1275
+ end_inclusive=False,
1276
+ app_profile_id=None,
1277
+ row_set=None,
1278
+ ):
1279
+ """Creates a request to read rows in a table.
1280
+
1281
+ :type table_name: str
1282
+ :param table_name: The name of the table to read from.
1283
+
1284
+ :type start_key: bytes
1285
+ :param start_key: (Optional) The beginning of a range of row keys to
1286
+ read from. The range will include ``start_key``. If
1287
+ left empty, will be interpreted as the empty string.
1288
+
1289
+ :type end_key: bytes
1290
+ :param end_key: (Optional) The end of a range of row keys to read from.
1291
+ The range will not include ``end_key``. If left empty,
1292
+ will be interpreted as an infinite string.
1293
+
1294
+ :type filter_: :class:`.RowFilter`
1295
+ :param filter_: (Optional) The filter to apply to the contents of the
1296
+ specified row(s). If unset, reads the entire table.
1297
+
1298
+ :type limit: int
1299
+ :param limit: (Optional) The read will terminate after committing to N
1300
+ rows' worth of results. The default (zero) is to return
1301
+ all results.
1302
+
1303
+ :type end_inclusive: bool
1304
+ :param end_inclusive: (Optional) Whether the ``end_key`` should be
1305
+ considered inclusive. The default is False (exclusive).
1306
+
1307
+ :type: app_profile_id: str
1308
+ :param app_profile_id: (Optional) The unique name of the AppProfile.
1309
+
1310
+ :type row_set: :class:`.RowSet`
1311
+ :param row_set: (Optional) The row set containing multiple row keys and
1312
+ row_ranges.
1313
+
1314
+ :rtype: :class:`data_messages_v2_pb2.ReadRowsRequest`
1315
+ :returns: The ``ReadRowsRequest`` protobuf corresponding to the inputs.
1316
+ :raises: :class:`ValueError <exceptions.ValueError>` if both
1317
+ ``row_set`` and one of ``start_key`` or ``end_key`` are set
1318
+ """
1319
+ request_kwargs = {"table_name": table_name}
1320
+ if (start_key is not None or end_key is not None) and row_set is not None:
1321
+ raise ValueError("Row range and row set cannot be " "set simultaneously")
1322
+
1323
+ if filter_ is not None:
1324
+ request_kwargs["filter"] = filter_.to_pb()
1325
+ if limit is not None:
1326
+ request_kwargs["rows_limit"] = limit
1327
+ if app_profile_id is not None:
1328
+ request_kwargs["app_profile_id"] = app_profile_id
1329
+
1330
+ message = data_messages_v2_pb2.ReadRowsRequest(**request_kwargs)
1331
+
1332
+ if start_key is not None or end_key is not None:
1333
+ row_set = RowSet()
1334
+ row_set.add_row_range(RowRange(start_key, end_key, end_inclusive=end_inclusive))
1335
+
1336
+ if row_set is not None:
1337
+ row_set._update_message_request(message)
1338
+
1339
+ return message
1340
+
1341
+
1342
+ def _compile_mutation_entries(table_name, rows):
1343
+ """Create list of mutation entries
1344
+
1345
+ :type table_name: str
1346
+ :param table_name: The name of the table to write to.
1347
+
1348
+ :type rows: list
1349
+ :param rows: List or other iterable of :class:`.DirectRow` instances.
1350
+
1351
+ :rtype: List[:class:`data_messages_v2_pb2.MutateRowsRequest.Entry`]
1352
+ :returns: entries corresponding to the inputs.
1353
+ :raises: :exc:`~.table.TooManyMutationsError` if the number of mutations is
1354
+ greater than the max ({})
1355
+ """.format(
1356
+ _MAX_BULK_MUTATIONS
1357
+ )
1358
+ entries = []
1359
+ mutations_count = 0
1360
+ entry_klass = data_messages_v2_pb2.MutateRowsRequest.Entry
1361
+
1362
+ for row in rows:
1363
+ _check_row_table_name(table_name, row)
1364
+ _check_row_type(row)
1365
+ mutations = row._get_mutations()
1366
+ entries.append(entry_klass(row_key=row.row_key, mutations=mutations))
1367
+ mutations_count += len(mutations)
1368
+
1369
+ if mutations_count > _MAX_BULK_MUTATIONS:
1370
+ raise TooManyMutationsError(
1371
+ "Maximum number of mutations is %s" % (_MAX_BULK_MUTATIONS,)
1372
+ )
1373
+ return entries
1374
+
1375
+
1376
+ def _check_row_table_name(table_name, row):
1377
+ """Checks that a row belongs to a table.
1378
+
1379
+ :type table_name: str
1380
+ :param table_name: The name of the table.
1381
+
1382
+ :type row: :class:`~google.cloud.bigtable.row.Row`
1383
+ :param row: An instance of :class:`~google.cloud.bigtable.row.Row`
1384
+ subclasses.
1385
+
1386
+ :raises: :exc:`~.table.TableMismatchError` if the row does not belong to
1387
+ the table.
1388
+ """
1389
+ if row.table is not None and row.table.name != table_name:
1390
+ raise TableMismatchError(
1391
+ "Row %s is a part of %s table. Current table: %s"
1392
+ % (row.row_key, row.table.name, table_name)
1393
+ )
1394
+
1395
+
1396
+ def _check_row_type(row):
1397
+ """Checks that a row is an instance of :class:`.DirectRow`.
1398
+
1399
+ :type row: :class:`~google.cloud.bigtable.row.Row`
1400
+ :param row: An instance of :class:`~google.cloud.bigtable.row.Row`
1401
+ subclasses.
1402
+
1403
+ :raises: :class:`TypeError <exceptions.TypeError>` if the row is not an
1404
+ instance of DirectRow.
1405
+ """
1406
+ if not isinstance(row, DirectRow):
1407
+ raise TypeError(
1408
+ "Bulk processing can not be applied for " "conditional or append mutations."
1409
+ )