25
25
"""
26
26
27
27
import argparse
28
+ import datetime
28
29
29
30
from google .cloud import bigtable
31
+ from google .cloud .bigtable import column_family
32
+ from google .cloud .bigtable import row_filters
30
33
31
34
32
35
def main (project_id , instance_id , table_id ):
@@ -38,23 +41,25 @@ def main(project_id, instance_id, table_id):
38
41
# [END connecting_to_bigtable]
39
42
40
43
# [START creating_a_table]
41
- print ( 'Creating the {} table.' .format (table_id ) )
44
+ print 'Creating the {} table.' .format (table_id )
42
45
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 )
44
50
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 )
47
56
# [END creating_a_table]
48
57
49
58
# [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'
58
63
for i , value in enumerate (greetings ):
59
64
# Note: This example uses sequential numeric IDs for simplicity,
60
65
# but this can result in poor performance in a production
@@ -68,35 +73,35 @@ def main(project_id, instance_id, table_id):
68
73
# https://cloud.google.com/bigtable/docs/schema-design
69
74
row_key = 'greeting{}' .format (i )
70
75
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 )
76
82
# [END writing_rows]
77
83
78
84
# [START getting_a_row]
79
- print ( 'Getting a single greeting by row key.' )
85
+ print 'Getting a single greeting by row key.'
80
86
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 )
84
92
# [END getting_a_row]
85
93
86
94
# [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' )
96
101
# [END scanning_all_rows]
97
102
98
103
# [START deleting_a_table]
99
- print ( 'Deleting the {} table.' .format (table_id ) )
104
+ print 'Deleting the {} table.' .format (table_id )
100
105
table .delete ()
101
106
# [END deleting_a_table]
102
107
0 commit comments