From b7f1a690e8a6c8685c467c625b8f1615c75b7685 Mon Sep 17 00:00:00 2001 From: Billy Jacobson Date: Thu, 14 Feb 2019 14:41:07 -0500 Subject: [PATCH 1/3] Updating the region tags to be consistent across Cloud Bigtable. Need to figure out filtering for happybase or rename --- bigtable/hello/main.py | 32 ++++++++++++++--------------- bigtable/hello_happybase/main.py | 35 +++++++++++++++++++------------- 2 files changed, 37 insertions(+), 30 deletions(-) diff --git a/bigtable/hello/main.py b/bigtable/hello/main.py index 879b70c52f3..6020cf99222 100644 --- a/bigtable/hello/main.py +++ b/bigtable/hello/main.py @@ -25,24 +25,24 @@ """ import argparse -# [START dependencies] +# [START bigtable_hw_imports] import datetime from google.cloud import bigtable from google.cloud.bigtable import column_family from google.cloud.bigtable import row_filters -# [END dependencies] +# [END bigtable_hw_imports] def main(project_id, instance_id, table_id): - # [START connecting_to_bigtable] + # [START bigtable_hw_connect] # The client must be created with admin=True because it will create a # table. client = bigtable.Client(project=project_id, admin=True) instance = client.instance(instance_id) - # [END connecting_to_bigtable] + # [END bigtable_hw_connect] - # [START creating_a_table] + # [START bigtable_hw_create_table] print('Creating the {} table.'.format(table_id)) table = instance.table(table_id) @@ -56,9 +56,9 @@ def main(project_id, instance_id, table_id): table.create(column_families=column_families) else: print("Table {} already exists.".format(table_id)) - # [END creating_a_table] + # [END bigtable_hw_create_table] - # [START writing_rows] + # [START bigtable_hw_write_rows] print('Writing some greetings to the table.') greetings = ['Hello World!', 'Hello Cloud Bigtable!', 'Hello Python!'] rows = [] @@ -82,36 +82,36 @@ def main(project_id, instance_id, table_id): timestamp=datetime.datetime.utcnow()) rows.append(row) table.mutate_rows(rows) - # [END writing_rows] + # [END bigtable_hw_write_rows] - # [START creating_a_filter] + # [START bigtable_hw_create_filter] # Create a filter to only retrieve the most recent version of the cell # for each column accross entire row. row_filter = row_filters.CellsColumnLimitFilter(1) - # [END creating_a_filter] + # [END bigtable_hw_create_filter] - # [START getting_a_row] + # [START bigtable_hw_get_with_filter] print('Getting a single greeting by row key.') key = 'greeting0'.encode() row = table.read_row(key, row_filter) cell = row.cells[column_family_id][column][0] print(cell.value.decode('utf-8')) - # [END getting_a_row] + # [END bigtable_hw_get_with_filter] - # [START scanning_all_rows] + # [START bigtable_hw_scan_with_filter] print('Scanning for all greetings:') partial_rows = table.read_rows(filter_=row_filter) for row in partial_rows: cell = row.cells[column_family_id][column][0] print(cell.value.decode('utf-8')) - # [END scanning_all_rows] + # [END bigtable_hw_scan_with_filter] - # [START deleting_a_table] + # [START bigtable_hw_delete_table] print('Deleting the {} table.'.format(table_id)) table.delete() - # [END deleting_a_table] + # [END bigtable_hw_delete_table] if __name__ == '__main__': diff --git a/bigtable/hello_happybase/main.py b/bigtable/hello_happybase/main.py index 0668402f895..b26da1b0042 100644 --- a/bigtable/hello_happybase/main.py +++ b/bigtable/hello_happybase/main.py @@ -25,22 +25,23 @@ """ import argparse - +# [START bigtable_hw_imports_happybase] from google.cloud import bigtable from google.cloud import happybase - +from google.cloud.bigtable import row_filters +# [END bigtable_hw_imports_happybase] def main(project_id, instance_id, table_name): - # [START connecting_to_bigtable] + # [START bigtable_hw_connect_happybase] # The client must be created with admin=True because it will create a # table. client = bigtable.Client(project=project_id, admin=True) instance = client.instance(instance_id) connection = happybase.Connection(instance=instance) - # [END connecting_to_bigtable] + # [END bigtable_hw_connect_happybase] try: - # [START creating_a_table] + # [START bigtable_hw_create_table_happybase] print('Creating the {} table.'.format(table_name)) column_family_name = 'cf1' connection.create_table( @@ -48,9 +49,9 @@ def main(project_id, instance_id, table_name): { column_family_name: dict() # Use default options. }) - # [END creating_a_table] + # [END bigtable_hw_create_table_happybase] - # [START writing_rows] + # [START bigtable_hw_write_rows_happybase] print('Writing some greetings to the table.') table = connection.table(table_name) column_name = '{fam}:greeting'.format(fam=column_family_name) @@ -73,26 +74,32 @@ def main(project_id, instance_id, table_name): # https://cloud.google.com/bigtable/docs/schema-design row_key = 'greeting{}'.format(i) table.put(row_key, {column_name: value}) - # [END writing_rows] + # [END bigtable_hw_write_rows_happybase] + + # [START bigtable_hw_create_filter_happybase] + # Create a filter to only retrieve the most recent version of the cell + # for each column accross entire row. + row_filter = row_filters.CellsColumnLimitFilter(1) + # [END bigtable_hw_create_filter_happybase] - # [START getting_a_row] + # [START bigtable_hw_get_with_filter_happybase] print('Getting a single greeting by row key.') key = 'greeting0'.encode('utf-8') row = table.row(key) print('\t{}: {}'.format(key, row[column_name.encode('utf-8')])) - # [END getting_a_row] + # [END bigtable_hw_get_with_filter_happybase] - # [START scanning_all_rows] + # [START bigtable_hw_scan_with_filter_happybase] print('Scanning for all greetings:') for key, row in table.scan(): print('\t{}: {}'.format(key, row[column_name.encode('utf-8')])) - # [END scanning_all_rows] + # [END bigtable_hw_scan_with_filter_happybase] - # [START deleting_a_table] + # [START bigtable_hw_delete_table_happybase] print('Deleting the {} table.'.format(table_name)) connection.delete_table(table_name) - # [END deleting_a_table] + # [END bigtable_hw_delete_table_happybase] finally: connection.close() From 014bd2a4612ca5cc898f6835daa052ca27d24b13 Mon Sep 17 00:00:00 2001 From: Billy Jacobson Date: Wed, 20 Feb 2019 14:17:31 -0500 Subject: [PATCH 2/3] Remove happybase filter --- bigtable/hello_happybase/main.py | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/bigtable/hello_happybase/main.py b/bigtable/hello_happybase/main.py index b26da1b0042..288a1354199 100644 --- a/bigtable/hello_happybase/main.py +++ b/bigtable/hello_happybase/main.py @@ -28,7 +28,6 @@ # [START bigtable_hw_imports_happybase] from google.cloud import bigtable from google.cloud import happybase -from google.cloud.bigtable import row_filters # [END bigtable_hw_imports_happybase] def main(project_id, instance_id, table_name): @@ -76,25 +75,19 @@ def main(project_id, instance_id, table_name): table.put(row_key, {column_name: value}) # [END bigtable_hw_write_rows_happybase] - # [START bigtable_hw_create_filter_happybase] - # Create a filter to only retrieve the most recent version of the cell - # for each column accross entire row. - row_filter = row_filters.CellsColumnLimitFilter(1) - # [END bigtable_hw_create_filter_happybase] - - # [START bigtable_hw_get_with_filter_happybase] + # [START bigtable_hw_get_by_key_happybase] print('Getting a single greeting by row key.') key = 'greeting0'.encode('utf-8') row = table.row(key) print('\t{}: {}'.format(key, row[column_name.encode('utf-8')])) - # [END bigtable_hw_get_with_filter_happybase] + # [END bigtable_hw_get_by_key_happybase] - # [START bigtable_hw_scan_with_filter_happybase] + # [START bigtable_hw_scan_all_happybase] print('Scanning for all greetings:') for key, row in table.scan(): print('\t{}: {}'.format(key, row[column_name.encode('utf-8')])) - # [END bigtable_hw_scan_with_filter_happybase] + # [END bigtable_hw_scan_all_happybase] # [START bigtable_hw_delete_table_happybase] print('Deleting the {} table.'.format(table_name)) From f16677aff9a35c00754009f13a298aafeeb750b3 Mon Sep 17 00:00:00 2001 From: Billy Jacobson Date: Mon, 22 Apr 2019 13:54:07 -0400 Subject: [PATCH 3/3] Linting --- bigtable/hello_happybase/main.py | 1 + 1 file changed, 1 insertion(+) diff --git a/bigtable/hello_happybase/main.py b/bigtable/hello_happybase/main.py index a99e5c87ce8..e4e68493448 100644 --- a/bigtable/hello_happybase/main.py +++ b/bigtable/hello_happybase/main.py @@ -30,6 +30,7 @@ from google.cloud import happybase # [END bigtable_hw_imports_happybase] + def main(project_id, instance_id, table_name): # [START bigtable_hw_connect_happybase] # The client must be created with admin=True because it will create a