From 7f41f1d52d30f5e573fceca21cb6f84a48238300 Mon Sep 17 00:00:00 2001 From: sangramql <39852271+sangramql@users.noreply.github.com> Date: Fri, 31 Aug 2018 21:47:31 +0530 Subject: [PATCH] Bigtable: update helloworld example [(#1670)](https://github.com/GoogleCloudPlatform/python-docs-samples/issues/1670) * Update helloworld example * Use iterable PartialRowsData --- samples/hello/main.py | 67 ++++++++++++++++++---------------- samples/hello/requirements.txt | 2 +- 2 files changed, 37 insertions(+), 32 deletions(-) diff --git a/samples/hello/main.py b/samples/hello/main.py index bd4408020..c0c610ca2 100644 --- a/samples/hello/main.py +++ b/samples/hello/main.py @@ -25,8 +25,11 @@ """ import argparse +import datetime from google.cloud import bigtable +from google.cloud.bigtable import column_family +from google.cloud.bigtable import row_filters def main(project_id, instance_id, table_id): @@ -38,23 +41,25 @@ def main(project_id, instance_id, table_id): # [END connecting_to_bigtable] # [START creating_a_table] - print('Creating the {} table.'.format(table_id)) + print 'Creating the {} table.'.format(table_id) table = instance.table(table_id) - table.create() + print 'Creating column family cf1 with Max Version GC rule...' + # Create a column family with GC policy : most recent N versions + # Define the GC policy to retain only the most recent 2 versions + max_versions_rule = column_family.MaxVersionsGCRule(2) column_family_id = 'cf1' - cf1 = table.column_family(column_family_id) - cf1.create() + column_families = {column_family_id: max_versions_rule} + if not table.exists(): + table.create(column_families=column_families) + else: + print "Table {} already exists.".format(table_id) # [END creating_a_table] # [START writing_rows] - print('Writing some greetings to the table.') - column_id = 'greeting'.encode('utf-8') - greetings = [ - 'Hello World!', - 'Hello Cloud Bigtable!', - 'Hello Python!', - ] - + print 'Writing some greetings to the table.' + greetings = ['Hello World!', 'Hello Cloud Bigtable!', 'Hello Python!'] + rows = [] + column = 'greeting' for i, value in enumerate(greetings): # Note: This example uses sequential numeric IDs for simplicity, # but this can result in poor performance in a production @@ -68,35 +73,35 @@ def main(project_id, instance_id, table_id): # https://cloud.google.com/bigtable/docs/schema-design row_key = 'greeting{}'.format(i) row = table.row(row_key) - row.set_cell( - column_family_id, - column_id, - value.encode('utf-8')) - row.commit() + row.set_cell(column_family_id, + column, + value, + timestamp=datetime.datetime.utcnow()) + rows.append(row) + table.mutate_rows(rows) # [END writing_rows] # [START getting_a_row] - print('Getting a single greeting by row key.') + print 'Getting a single greeting by row key.' key = 'greeting0' - row = table.read_row(key.encode('utf-8')) - value = row.cells[column_family_id][column_id][0].value - print('\t{}: {}'.format(key, value.decode('utf-8'))) + + # Only retrieve the most recent version of the cell. + row_filter = row_filters.CellsColumnLimitFilter(1) + row = table.read_row(key, row_filter) + print row.cell_value(column_family_id, column) # [END getting_a_row] # [START scanning_all_rows] - print('Scanning for all greetings:') - partial_rows = table.read_rows() - partial_rows.consume_all() - - for row_key, row in partial_rows.rows.items(): - key = row_key.decode('utf-8') - cell = row.cells[column_family_id][column_id][0] - value = cell.value.decode('utf-8') - print('\t{}: {}'.format(key, value)) + 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] # [START deleting_a_table] - print('Deleting the {} table.'.format(table_id)) + print 'Deleting the {} table.'.format(table_id) table.delete() # [END deleting_a_table] diff --git a/samples/hello/requirements.txt b/samples/hello/requirements.txt index bc925979d..41080ce53 100644 --- a/samples/hello/requirements.txt +++ b/samples/hello/requirements.txt @@ -1,2 +1,2 @@ -google-cloud-bigtable==0.29.0 +google-cloud-bigtable==0.30.0 google-cloud-core==0.28.1