2525"""
2626
2727import argparse
28+ import datetime
2829
2930from google .cloud import bigtable
31+ from google .cloud .bigtable import column_family
32+ from google .cloud .bigtable import row_filters
3033
3134
3235def main (project_id , instance_id , table_id ):
@@ -38,23 +41,25 @@ def main(project_id, instance_id, table_id):
3841 # [END connecting_to_bigtable]
3942
4043 # [START creating_a_table]
41- print ( 'Creating the {} table.' .format (table_id ) )
44+ print 'Creating the {} table.' .format (table_id )
4245 table = instance .table (table_id )
43- table .create ()
46+ print 'Creating column family cf1 with Max Version GC rule...'
47+ # Create a column family with GC policy : most recent N versions
48+ # Define the GC policy to retain only the most recent 2 versions
49+ max_versions_rule = column_family .MaxVersionsGCRule (2 )
4450 column_family_id = 'cf1'
45- cf1 = table .column_family (column_family_id )
46- cf1 .create ()
51+ column_families = {column_family_id : max_versions_rule }
52+ if not table .exists ():
53+ table .create (column_families = column_families )
54+ else :
55+ print "Table {} already exists." .format (table_id )
4756 # [END creating_a_table]
4857
4958 # [START writing_rows]
50- print ('Writing some greetings to the table.' )
51- column_id = 'greeting' .encode ('utf-8' )
52- greetings = [
53- 'Hello World!' ,
54- 'Hello Cloud Bigtable!' ,
55- 'Hello Python!' ,
56- ]
57-
59+ print 'Writing some greetings to the table.'
60+ greetings = ['Hello World!' , 'Hello Cloud Bigtable!' , 'Hello Python!' ]
61+ rows = []
62+ column = 'greeting'
5863 for i , value in enumerate (greetings ):
5964 # Note: This example uses sequential numeric IDs for simplicity,
6065 # but this can result in poor performance in a production
@@ -68,35 +73,35 @@ def main(project_id, instance_id, table_id):
6873 # https://cloud.google.com/bigtable/docs/schema-design
6974 row_key = 'greeting{}' .format (i )
7075 row = table .row (row_key )
71- row .set_cell (
72- column_family_id ,
73- column_id ,
74- value .encode ('utf-8' ))
75- row .commit ()
76+ row .set_cell (column_family_id ,
77+ column ,
78+ value ,
79+ timestamp = datetime .datetime .utcnow ())
80+ rows .append (row )
81+ table .mutate_rows (rows )
7682 # [END writing_rows]
7783
7884 # [START getting_a_row]
79- print ( 'Getting a single greeting by row key.' )
85+ print 'Getting a single greeting by row key.'
8086 key = 'greeting0'
81- row = table .read_row (key .encode ('utf-8' ))
82- value = row .cells [column_family_id ][column_id ][0 ].value
83- print ('\t {}: {}' .format (key , value .decode ('utf-8' )))
87+
88+ # Only retrieve the most recent version of the cell.
89+ row_filter = row_filters .CellsColumnLimitFilter (1 )
90+ row = table .read_row (key , row_filter )
91+ print row .cell_value (column_family_id , column )
8492 # [END getting_a_row]
8593
8694 # [START scanning_all_rows]
87- print ('Scanning for all greetings:' )
88- partial_rows = table .read_rows ()
89- partial_rows .consume_all ()
90-
91- for row_key , row in partial_rows .rows .items ():
92- key = row_key .decode ('utf-8' )
93- cell = row .cells [column_family_id ][column_id ][0 ]
94- value = cell .value .decode ('utf-8' )
95- print ('\t {}: {}' .format (key , value ))
95+ print 'Scanning for all greetings:'
96+ partial_rows = table .read_rows (filter_ = row_filter )
97+
98+ for row in partial_rows :
99+ cell = row .cells [column_family_id ][column ][0 ]
100+ print cell .value .decode ('utf-8' )
96101 # [END scanning_all_rows]
97102
98103 # [START deleting_a_table]
99- print ( 'Deleting the {} table.' .format (table_id ) )
104+ print 'Deleting the {} table.' .format (table_id )
100105 table .delete ()
101106 # [END deleting_a_table]
102107
0 commit comments