From 039ad02aac084453989926307f14adbf3a5570c0 Mon Sep 17 00:00:00 2001 From: Daniel Sanche Date: Mon, 21 Oct 2024 17:14:20 -0700 Subject: [PATCH 1/2] chore(docs): added imports into snippets --- samples/snippets/deletes/deletes_snippets.py | 37 ++++++++----- .../deletes/deletes_snippets_async.py | 34 +++++++----- samples/snippets/filters/filter_snippets.py | 55 +++++++++++++++++++ .../snippets/filters/filter_snippets_async.py | 7 +-- samples/snippets/reads/read_snippets.py | 20 +++++++ 5 files changed, 121 insertions(+), 32 deletions(-) diff --git a/samples/snippets/deletes/deletes_snippets.py b/samples/snippets/deletes/deletes_snippets.py index 72f812ca2..6cdbf33a6 100644 --- a/samples/snippets/deletes/deletes_snippets.py +++ b/samples/snippets/deletes/deletes_snippets.py @@ -14,14 +14,11 @@ # limitations under the License. -from google.cloud import bigtable - -# Write your code here. - - # [START bigtable_delete_from_column] def delete_from_column(project_id, instance_id, table_id): - client = bigtable.Client(project=project_id, admin=True) + from google.cloud.bigtable import Client + + client = Client(project=project_id, admin=True) instance = client.instance(instance_id) table = instance.table(table_id) row = table.row("phone#4c410523#20190501") @@ -33,7 +30,9 @@ def delete_from_column(project_id, instance_id, table_id): # [START bigtable_delete_from_column_family] def delete_from_column_family(project_id, instance_id, table_id): - client = bigtable.Client(project=project_id, admin=True) + from google.cloud.bigtable import Client + + client = Client(project=project_id, admin=True) instance = client.instance(instance_id) table = instance.table(table_id) row = table.row("phone#4c410523#20190501") @@ -46,7 +45,9 @@ def delete_from_column_family(project_id, instance_id, table_id): # [START bigtable_delete_from_row] def delete_from_row(project_id, instance_id, table_id): - client = bigtable.Client(project=project_id, admin=True) + from google.cloud.bigtable import Client + + client = Client(project=project_id, admin=True) instance = client.instance(instance_id) table = instance.table(table_id) row = table.row("phone#4c410523#20190501") @@ -58,7 +59,9 @@ def delete_from_row(project_id, instance_id, table_id): # [START bigtable_streaming_and_batching] def streaming_and_batching(project_id, instance_id, table_id): - client = bigtable.Client(project=project_id, admin=True) + from google.cloud.bigtable import Client + + client = Client(project=project_id, admin=True) instance = client.instance(instance_id) table = instance.table(table_id) batcher = table.mutations_batcher(flush_count=2) @@ -74,7 +77,9 @@ def streaming_and_batching(project_id, instance_id, table_id): # [START bigtable_check_and_mutate] def check_and_mutate(project_id, instance_id, table_id): - client = bigtable.Client(project=project_id, admin=True) + from google.cloud.bigtable import Client + + client = Client(project=project_id, admin=True) instance = client.instance(instance_id) table = instance.table(table_id) row = table.row("phone#4c410523#20190501") @@ -88,7 +93,9 @@ def check_and_mutate(project_id, instance_id, table_id): # [START bigtable_drop_row_range] def drop_row_range(project_id, instance_id, table_id): - client = bigtable.Client(project=project_id, admin=True) + from google.cloud.bigtable import Client + + client = Client(project=project_id, admin=True) instance = client.instance(instance_id) table = instance.table(table_id) row_key_prefix = "phone#4c410523" @@ -99,7 +106,9 @@ def drop_row_range(project_id, instance_id, table_id): # [START bigtable_delete_column_family] def delete_column_family(project_id, instance_id, table_id): - client = bigtable.Client(project=project_id, admin=True) + from google.cloud.bigtable import Client + + client = Client(project=project_id, admin=True) instance = client.instance(instance_id) table = instance.table(table_id) column_family_id = "stats_summary" @@ -111,7 +120,9 @@ def delete_column_family(project_id, instance_id, table_id): # [START bigtable_delete_table] def delete_table(project_id, instance_id, table_id): - client = bigtable.Client(project=project_id, admin=True) + from google.cloud.bigtable import Client + + client = Client(project=project_id, admin=True) instance = client.instance(instance_id) table = instance.table(table_id) table.delete() diff --git a/samples/snippets/deletes/deletes_snippets_async.py b/samples/snippets/deletes/deletes_snippets_async.py index 8f3711e06..2241fab4a 100644 --- a/samples/snippets/deletes/deletes_snippets_async.py +++ b/samples/snippets/deletes/deletes_snippets_async.py @@ -14,22 +14,11 @@ # limitations under the License. -from google.cloud.bigtable.data import ( - BigtableDataClientAsync, - DeleteRangeFromColumn, - DeleteAllFromFamily, - DeleteAllFromRow, - RowMutationEntry, - row_filters, - ReadRowsQuery, -) - - -# Write your code here. - - # [START bigtable_delete_from_column_asyncio] async def delete_from_column(project_id, instance_id, table_id): + from google.cloud.bigtable.data import BigtableDataClientAsync + from google.cloud.bigtable.data import DeleteRangeFromColumn + client = BigtableDataClientAsync(project=project_id) table = client.get_table(instance_id, table_id) @@ -46,6 +35,9 @@ async def delete_from_column(project_id, instance_id, table_id): # [START bigtable_delete_from_column_family_asyncio] async def delete_from_column_family(project_id, instance_id, table_id): + from google.cloud.bigtable.data import BigtableDataClientAsync + from google.cloud.bigtable.data import DeleteAllFromFamily + client = BigtableDataClientAsync(project=project_id) table = client.get_table(instance_id, table_id) @@ -60,6 +52,9 @@ async def delete_from_column_family(project_id, instance_id, table_id): # [START bigtable_delete_from_row_asyncio] async def delete_from_row(project_id, instance_id, table_id): + from google.cloud.bigtable.data import BigtableDataClientAsync + from google.cloud.bigtable.data import DeleteAllFromRow + client = BigtableDataClientAsync(project=project_id) table = client.get_table(instance_id, table_id) @@ -73,6 +68,11 @@ async def delete_from_row(project_id, instance_id, table_id): # [START bigtable_streaming_and_batching_asyncio] async def streaming_and_batching(project_id, instance_id, table_id): + from google.cloud.bigtable.data import BigtableDataClientAsync + from google.cloud.bigtable.data import DeleteRangeFromColumn + from google.cloud.bigtable.data import RowMutationEntry + from google.cloud.bigtable.data import ReadRowsQuery + client = BigtableDataClientAsync(project=project_id) table = client.get_table(instance_id, table_id) @@ -95,12 +95,16 @@ async def streaming_and_batching(project_id, instance_id, table_id): # [START bigtable_check_and_mutate_asyncio] async def check_and_mutate(project_id, instance_id, table_id): + from google.cloud.bigtable.data import BigtableDataClientAsync + from google.cloud.bigtable.data import DeleteRangeFromColumn + from google.cloud.bigtable.data.row_filters import LiteralValueFilter + client = BigtableDataClientAsync(project=project_id) table = client.get_table(instance_id, table_id) await table.check_and_mutate_row( "phone#4c410523#20190501", - predicate=row_filters.LiteralValueFilter("PQ2A.190405.003"), + predicate=LiteralValueFilter("PQ2A.190405.003"), true_case_mutations=DeleteRangeFromColumn( family="cell_plan", qualifier=b"data_plan_01gb" ), diff --git a/samples/snippets/filters/filter_snippets.py b/samples/snippets/filters/filter_snippets.py index 4211378f3..54c6af0d9 100644 --- a/samples/snippets/filters/filter_snippets.py +++ b/samples/snippets/filters/filter_snippets.py @@ -25,6 +25,9 @@ # [START bigtable_filters_limit_row_sample] def filter_limit_row_sample(project_id, instance_id, table_id): + from google.cloud import bigtable + from google.cloud.bigtable import row_filters + client = bigtable.Client(project=project_id, admin=True) instance = client.instance(instance_id) table = instance.table(table_id) @@ -37,6 +40,9 @@ def filter_limit_row_sample(project_id, instance_id, table_id): # [END bigtable_filters_limit_row_sample] # [START bigtable_filters_limit_row_regex] def filter_limit_row_regex(project_id, instance_id, table_id): + from google.cloud import bigtable + from google.cloud.bigtable import row_filters + client = bigtable.Client(project=project_id, admin=True) instance = client.instance(instance_id) table = instance.table(table_id) @@ -51,6 +57,9 @@ def filter_limit_row_regex(project_id, instance_id, table_id): # [END bigtable_filters_limit_row_regex] # [START bigtable_filters_limit_cells_per_col] def filter_limit_cells_per_col(project_id, instance_id, table_id): + from google.cloud import bigtable + from google.cloud.bigtable import row_filters + client = bigtable.Client(project=project_id, admin=True) instance = client.instance(instance_id) table = instance.table(table_id) @@ -63,6 +72,9 @@ def filter_limit_cells_per_col(project_id, instance_id, table_id): # [END bigtable_filters_limit_cells_per_col] # [START bigtable_filters_limit_cells_per_row] def filter_limit_cells_per_row(project_id, instance_id, table_id): + from google.cloud import bigtable + from google.cloud.bigtable import row_filters + client = bigtable.Client(project=project_id, admin=True) instance = client.instance(instance_id) table = instance.table(table_id) @@ -75,6 +87,9 @@ def filter_limit_cells_per_row(project_id, instance_id, table_id): # [END bigtable_filters_limit_cells_per_row] # [START bigtable_filters_limit_cells_per_row_offset] def filter_limit_cells_per_row_offset(project_id, instance_id, table_id): + from google.cloud import bigtable + from google.cloud.bigtable import row_filters + client = bigtable.Client(project=project_id, admin=True) instance = client.instance(instance_id) table = instance.table(table_id) @@ -87,6 +102,9 @@ def filter_limit_cells_per_row_offset(project_id, instance_id, table_id): # [END bigtable_filters_limit_cells_per_row_offset] # [START bigtable_filters_limit_col_family_regex] def filter_limit_col_family_regex(project_id, instance_id, table_id): + from google.cloud import bigtable + from google.cloud.bigtable import row_filters + client = bigtable.Client(project=project_id, admin=True) instance = client.instance(instance_id) table = instance.table(table_id) @@ -101,6 +119,9 @@ def filter_limit_col_family_regex(project_id, instance_id, table_id): # [END bigtable_filters_limit_col_family_regex] # [START bigtable_filters_limit_col_qualifier_regex] def filter_limit_col_qualifier_regex(project_id, instance_id, table_id): + from google.cloud import bigtable + from google.cloud.bigtable import row_filters + client = bigtable.Client(project=project_id, admin=True) instance = client.instance(instance_id) table = instance.table(table_id) @@ -115,6 +136,9 @@ def filter_limit_col_qualifier_regex(project_id, instance_id, table_id): # [END bigtable_filters_limit_col_qualifier_regex] # [START bigtable_filters_limit_col_range] def filter_limit_col_range(project_id, instance_id, table_id): + from google.cloud import bigtable + from google.cloud.bigtable import row_filters + client = bigtable.Client(project=project_id, admin=True) instance = client.instance(instance_id) table = instance.table(table_id) @@ -131,6 +155,9 @@ def filter_limit_col_range(project_id, instance_id, table_id): # [END bigtable_filters_limit_col_range] # [START bigtable_filters_limit_value_range] def filter_limit_value_range(project_id, instance_id, table_id): + from google.cloud import bigtable + from google.cloud.bigtable import row_filters + client = bigtable.Client(project=project_id, admin=True) instance = client.instance(instance_id) table = instance.table(table_id) @@ -148,6 +175,9 @@ def filter_limit_value_range(project_id, instance_id, table_id): def filter_limit_value_regex(project_id, instance_id, table_id): + from google.cloud import bigtable + from google.cloud.bigtable import row_filters + client = bigtable.Client(project=project_id, admin=True) instance = client.instance(instance_id) table = instance.table(table_id) @@ -162,6 +192,10 @@ def filter_limit_value_regex(project_id, instance_id, table_id): # [END bigtable_filters_limit_value_regex] # [START bigtable_filters_limit_timestamp_range] def filter_limit_timestamp_range(project_id, instance_id, table_id): + from google.cloud import bigtable + from google.cloud.bigtable import row_filters + import datetime + client = bigtable.Client(project=project_id, admin=True) instance = client.instance(instance_id) table = instance.table(table_id) @@ -178,6 +212,9 @@ def filter_limit_timestamp_range(project_id, instance_id, table_id): # [END bigtable_filters_limit_timestamp_range] # [START bigtable_filters_limit_block_all] def filter_limit_block_all(project_id, instance_id, table_id): + from google.cloud import bigtable + from google.cloud.bigtable import row_filters + client = bigtable.Client(project=project_id, admin=True) instance = client.instance(instance_id) table = instance.table(table_id) @@ -190,6 +227,9 @@ def filter_limit_block_all(project_id, instance_id, table_id): # [END bigtable_filters_limit_block_all] # [START bigtable_filters_limit_pass_all] def filter_limit_pass_all(project_id, instance_id, table_id): + from google.cloud import bigtable + from google.cloud.bigtable import row_filters + client = bigtable.Client(project=project_id, admin=True) instance = client.instance(instance_id) table = instance.table(table_id) @@ -202,6 +242,9 @@ def filter_limit_pass_all(project_id, instance_id, table_id): # [END bigtable_filters_limit_pass_all] # [START bigtable_filters_modify_strip_value] def filter_modify_strip_value(project_id, instance_id, table_id): + from google.cloud import bigtable + from google.cloud.bigtable import row_filters + client = bigtable.Client(project=project_id, admin=True) instance = client.instance(instance_id) table = instance.table(table_id) @@ -214,6 +257,9 @@ def filter_modify_strip_value(project_id, instance_id, table_id): # [END bigtable_filters_modify_strip_value] # [START bigtable_filters_modify_apply_label] def filter_modify_apply_label(project_id, instance_id, table_id): + from google.cloud import bigtable + from google.cloud.bigtable import row_filters + client = bigtable.Client(project=project_id, admin=True) instance = client.instance(instance_id) table = instance.table(table_id) @@ -226,6 +272,9 @@ def filter_modify_apply_label(project_id, instance_id, table_id): # [END bigtable_filters_modify_apply_label] # [START bigtable_filters_composing_chain] def filter_composing_chain(project_id, instance_id, table_id): + from google.cloud import bigtable + from google.cloud.bigtable import row_filters + client = bigtable.Client(project=project_id, admin=True) instance = client.instance(instance_id) table = instance.table(table_id) @@ -245,6 +294,9 @@ def filter_composing_chain(project_id, instance_id, table_id): # [END bigtable_filters_composing_chain] # [START bigtable_filters_composing_interleave] def filter_composing_interleave(project_id, instance_id, table_id): + from google.cloud import bigtable + from google.cloud.bigtable import row_filters + client = bigtable.Client(project=project_id, admin=True) instance = client.instance(instance_id) table = instance.table(table_id) @@ -264,6 +316,9 @@ def filter_composing_interleave(project_id, instance_id, table_id): # [END bigtable_filters_composing_interleave] # [START bigtable_filters_composing_condition] def filter_composing_condition(project_id, instance_id, table_id): + from google.cloud import bigtable + from google.cloud.bigtable import row_filters + client = bigtable.Client(project=project_id, admin=True) instance = client.instance(instance_id) table = instance.table(table_id) diff --git a/samples/snippets/filters/filter_snippets_async.py b/samples/snippets/filters/filter_snippets_async.py index e47bbb3fb..6a21f44fb 100644 --- a/samples/snippets/filters/filter_snippets_async.py +++ b/samples/snippets/filters/filter_snippets_async.py @@ -11,9 +11,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -from google.cloud._helpers import _datetime_from_microseconds -from google.cloud.bigtable.data import Row - # [START bigtable_filters_limit_row_sample_asyncio] async def filter_limit_row_sample(project_id, instance_id, table_id): @@ -371,7 +368,9 @@ async def filter_composing_condition(project_id, instance_id, table_id): # [END_EXCLUDE] -def print_row(row: Row): +def print_row(row): + from google.cloud._helpers import _datetime_from_microseconds + print("Reading data for {}:".format(row.row_key.decode("utf-8"))) last_family = None for cell in row.cells: diff --git a/samples/snippets/reads/read_snippets.py b/samples/snippets/reads/read_snippets.py index afd0955b8..d49966922 100644 --- a/samples/snippets/reads/read_snippets.py +++ b/samples/snippets/reads/read_snippets.py @@ -24,6 +24,8 @@ # [START bigtable_reads_row] def read_row(project_id, instance_id, table_id): + from google.cloud import bigtable + client = bigtable.Client(project=project_id, admin=True) instance = client.instance(instance_id) table = instance.table(table_id) @@ -38,6 +40,9 @@ def read_row(project_id, instance_id, table_id): # [START bigtable_reads_row_partial] def read_row_partial(project_id, instance_id, table_id): + from google.cloud import bigtable + from google.cloud.bigtable import row_filters + client = bigtable.Client(project=project_id, admin=True) instance = client.instance(instance_id) table = instance.table(table_id) @@ -52,6 +57,9 @@ def read_row_partial(project_id, instance_id, table_id): # [END bigtable_reads_row_partial] # [START bigtable_reads_rows] def read_rows(project_id, instance_id, table_id): + from google.cloud import bigtable + from google.cloud.bigtable.row_set import RowSet + client = bigtable.Client(project=project_id, admin=True) instance = client.instance(instance_id) table = instance.table(table_id) @@ -68,6 +76,9 @@ def read_rows(project_id, instance_id, table_id): # [END bigtable_reads_rows] # [START bigtable_reads_row_range] def read_row_range(project_id, instance_id, table_id): + from google.cloud import bigtable + from google.cloud.bigtable.row_set import RowSet + client = bigtable.Client(project=project_id, admin=True) instance = client.instance(instance_id) table = instance.table(table_id) @@ -85,6 +96,9 @@ def read_row_range(project_id, instance_id, table_id): # [END bigtable_reads_row_range] # [START bigtable_reads_row_ranges] def read_row_ranges(project_id, instance_id, table_id): + from google.cloud import bigtable + from google.cloud.bigtable.row_set import RowSet + client = bigtable.Client(project=project_id, admin=True) instance = client.instance(instance_id) table = instance.table(table_id) @@ -105,6 +119,9 @@ def read_row_ranges(project_id, instance_id, table_id): # [END bigtable_reads_row_ranges] # [START bigtable_reads_prefix] def read_prefix(project_id, instance_id, table_id): + from google.cloud import bigtable + from google.cloud.bigtable.row_set import RowSet + client = bigtable.Client(project=project_id, admin=True) instance = client.instance(instance_id) table = instance.table(table_id) @@ -122,6 +139,9 @@ def read_prefix(project_id, instance_id, table_id): # [END bigtable_reads_prefix] # [START bigtable_reads_filter] def read_filter(project_id, instance_id, table_id): + from google.cloud import bigtable + from google.cloud.bigtable import row_filters + client = bigtable.Client(project=project_id, admin=True) instance = client.instance(instance_id) table = instance.table(table_id) From e3e4825f39ae73f9349c8d493bb273c3a7148216 Mon Sep 17 00:00:00 2001 From: Daniel Sanche Date: Mon, 21 Oct 2024 17:29:54 -0700 Subject: [PATCH 2/2] simplify print regions --- samples/snippets/filters/filter_snippets.py | 12 +----------- samples/snippets/filters/filter_snippets_async.py | 1 - samples/snippets/reads/read_snippets.py | 12 +----------- 3 files changed, 2 insertions(+), 23 deletions(-) diff --git a/samples/snippets/filters/filter_snippets.py b/samples/snippets/filters/filter_snippets.py index 54c6af0d9..d17c773a4 100644 --- a/samples/snippets/filters/filter_snippets.py +++ b/samples/snippets/filters/filter_snippets.py @@ -13,15 +13,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -# [START bigtable_filters_print] -import datetime - -from google.cloud import bigtable -import google.cloud.bigtable.row_filters as row_filters - -# Write your code here. -# [START_EXCLUDE] - # [START bigtable_filters_limit_row_sample] def filter_limit_row_sample(project_id, instance_id, table_id): @@ -340,9 +331,8 @@ def filter_composing_condition(project_id, instance_id, table_id): # [END bigtable_filters_composing_condition] -# [END_EXCLUDE] - +# [START bigtable_filters_print] def print_row(row): print("Reading data for {}:".format(row.row_key.decode("utf-8"))) for cf, cols in sorted(row.cells.items()): diff --git a/samples/snippets/filters/filter_snippets_async.py b/samples/snippets/filters/filter_snippets_async.py index 6a21f44fb..899d4c5c7 100644 --- a/samples/snippets/filters/filter_snippets_async.py +++ b/samples/snippets/filters/filter_snippets_async.py @@ -365,7 +365,6 @@ async def filter_composing_condition(project_id, instance_id, table_id): # [END bigtable_filters_composing_condition_asyncio] -# [END_EXCLUDE] def print_row(row): diff --git a/samples/snippets/reads/read_snippets.py b/samples/snippets/reads/read_snippets.py index d49966922..210ca73a7 100644 --- a/samples/snippets/reads/read_snippets.py +++ b/samples/snippets/reads/read_snippets.py @@ -13,15 +13,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -# [START bigtable_reads_print] -from google.cloud import bigtable -import google.cloud.bigtable.row_filters as row_filters -from google.cloud.bigtable.row_set import RowSet - -# Write your code here. -# [START_EXCLUDE] - - # [START bigtable_reads_row] def read_row(project_id, instance_id, table_id): from google.cloud import bigtable @@ -152,9 +143,8 @@ def read_filter(project_id, instance_id, table_id): # [END bigtable_reads_filter] -# [END_EXCLUDE] - +# [START bigtable_reads_print] def print_row(row): print("Reading data for {}:".format(row.row_key.decode("utf-8"))) for cf, cols in sorted(row.cells.items()):