@@ -63,31 +63,25 @@ def run_table_operations(project_id, instance_id, table_id):
6363 table .create ()
6464 print 'Created table {}.' .format (table_id )
6565
66- # [START List existing tables in the instance. ]
66+ # [START bigtable_list_tables ]
6767 tables = instance .list_tables ()
6868 print 'Listing tables in current project...'
6969 if tables != []:
7070 for tbl in tables :
7171 print tbl .table_id
7272 else :
7373 print 'No table exists in current project...'
74- # [END List existing tables in the instance.]
75-
76- # Display name of the table.
77- print 'Printing table metadata...'
78- print table .table_id
74+ # [END bigtable_list_tables]
7975
8076 # [START bigtable_create_family_gc_max_age]
8177 print 'Creating column family cf1 with with MaxAge GC Rule...'
8278 # Create a column family with GC policy : maximum age
8379 # where age = current time minus cell timestamp
84- column_family_id1 = 'cf1'
80+
8581 # Define the GC rule to retain data with max age of 5 days
86- max_age = datetime .timedelta (days = 5 )
87- max_age_rule = table .max_age_gc_rule (max_age )
88- print 'Created MaxAge GC rule.'
82+ max_age_rule = table .max_age_gc_rule (datetime .timedelta (days = 5 ))
8983
90- cf1 = table .column_family (column_family_id1 , max_age_rule )
84+ cf1 = table .column_family ('cf1' , max_age_rule )
9185 cf1 .create ()
9286 print 'Created column family cf1 with MaxAge GC Rule.'
9387 # [END bigtable_create_family_gc_max_age]
@@ -96,13 +90,11 @@ def run_table_operations(project_id, instance_id, table_id):
9690 print 'Creating column family cf2 with max versions GC rule...'
9791 # Create a column family with GC policy : most recent N versions
9892 # where 1 = most recent version
99- column_family_id2 = 'cf2'
93+
10094 # Define the GC policy to retain only the most recent 2 versions
101- max_versions = 2
102- max_versions_rule = table .max_versions_gc_rule (max_versions )
103- print 'Created Max Versions GC Rule.'
95+ max_versions_rule = table .max_versions_gc_rule (2 )
10496
105- cf2 = table .column_family (column_family_id2 , max_versions_rule )
97+ cf2 = table .column_family ('cf2' , max_versions_rule )
10698 cf2 .create ()
10799 print 'Created column family cf2 with Max Versions GC Rule.'
108100 # [END bigtable_create_family_gc_max_versions]
@@ -111,13 +103,13 @@ def run_table_operations(project_id, instance_id, table_id):
111103 print 'Creating column family cf3 with union GC rule...'
112104 # Create a column family with GC policy to drop data that matches
113105 # at least one condition.
114- column_family_id3 = 'cf3'
115- # GC rule : Drop max age rule OR the most recent version rule.
116- union = [ max_age_rule , max_versions_rule ]
117- union_rule = table .gc_rule_union ( union )
118- print 'Created Union GC Rule.'
106+ # Define a GC rule to drop cells older than 5 days or not the
107+ # most recent version
108+ union_rule = table . gc_rule_union ([
109+ table .max_age_gc_rule ( datetime . timedelta ( days = 5 )),
110+ table . max_versions_gc_rule ( 2 )])
119111
120- cf3 = table .column_family (column_family_id3 , union_rule )
112+ cf3 = table .column_family ('cf3' , union_rule )
121113 cf3 .create ()
122114 print 'Created column family cf3 with Union GC rule'
123115 # [END bigtable_create_family_gc_union]
@@ -126,45 +118,33 @@ def run_table_operations(project_id, instance_id, table_id):
126118 print 'Creating column family cf4 with Intersection GC rule...'
127119 # Create a column family with GC policy to drop data that matches
128120 # all conditions
129- column_family_id4 = 'cf4'
130121 # GC rule: Drop cells older than 5 days AND older than the most
131122 # recent 2 versions
132- intersection = [ max_age_rule , max_versions_rule ]
133- intersection_rule = table .gc_rule_intersection ( union )
134- print 'Created Intersection GC Rule.'
123+ intersection_rule = table . gc_rule_intersection ([
124+ table .max_age_gc_rule ( datetime . timedelta ( days = 5 )),
125+ table . max_versions_gc_rule ( 2 )])
135126
136- cf4 = table .column_family (column_family_id4 , intersection_rule )
127+ cf4 = table .column_family ('cf4' , intersection_rule )
137128 cf4 .create ()
138129 print 'Created column family cf4 with Intersection GC rule.'
139130 # [END bigtable_create_family_gc_intersection]
140131
141132 # [START bigtable_create_family_gc_nested]
142133 print 'Creating column family cf5 with a Nested GC rule...'
143- # Create a column family with nested GC policys .
134+ # Create a column family with nested GC policies .
144135 # Create a nested GC rule:
145136 # Drop cells that are either older than the 10 recent versions
146137 # OR
147138 # Drop cells that are older than a month AND older than the
148139 # 2 recent versions
149- column_family_id5 = 'cf5'
150- # Drop cells that are either older than the 10 recent versions
151- max_versions_rule1 = table .max_versions_gc_rule (10 )
152-
153- # Drop cells that are older than a month AND older than
154- # the 2 recent versions
155- max_age = datetime .timedelta (days = 30 )
156- max_age_rule = table .max_age_gc_rule (max_age )
157- max_versions_rule2 = table .max_versions_gc_rule (2 )
158- intersection = [max_age_rule , max_versions_rule2 ]
159- intersection_rule = table .gc_rule_intersection (intersection )
160-
161- # This nesting is done with union rule since it is OR between
162- # the selected rules.
163- nest = [max_versions_rule1 , intersection_rule ]
164- nested_rule = table .gc_rule_union (nest )
165- print 'Created Nested GC Rule.'
166-
167- cf5 = table .column_family (column_family_id5 , nested_rule )
140+ rule1 = table .max_versions_gc_rule (10 )
141+ rule2 = table .gc_rule_intersection ([
142+ table .max_age_gc_rule (datetime .timedelta (days = 30 )),
143+ table .max_versions_gc_rule (2 )])
144+
145+ nested_rule = table .gc_rule_union ([rule1 , rule2 ])
146+
147+ cf5 = table .column_family ('cf5' , nested_rule )
168148 cf5 .create ()
169149 print 'Created column family cf5 with a Nested GC rule.'
170150 # [END bigtable_create_family_gc_nested]
@@ -193,23 +173,21 @@ def run_table_operations(project_id, instance_id, table_id):
193173 # }
194174 # [END bigtable_list_column_families]
195175
176+ print 'Print column family cf1 GC rule before update...'
177+ print "Column Family: cf1"
178+ print cf1 .to_pb ()
179+
196180 # [START bigtable_update_gc_rule]
197181 print 'Updating column family cf1 GC rule...'
198182 # Update the column family cf1 to update the GC rule
199- max_versions = 1
200- max_versions_rule = table .max_versions_gc_rule (max_versions )
201- # Create a reference to the column family with GC rule
202- cf1 = table .column_family (column_family_id1 , max_versions_rule )
183+ cf1 = table .column_family ('cf1' , table .max_versions_gc_rule (1 ))
203184 cf1 .update ()
204- print 'Updated column family cf1 GC rule'
185+ print 'Updated column family cf1 GC rule\n '
205186 # [END bigtable_update_gc_rule]
206187
207- # [START bigtable_family_get_gc_rule]
208- print 'Print updated column family cf1 GC rule...'
209- print 'Column Family:' , column_family_id1
210- print 'GC Rule:'
188+ print 'Print column family cf1 GC rule after update...'
189+ print "Column Family: cf1"
211190 print cf1 .to_pb ()
212- # [END bigtable_family_get_gc_rule]
213191
214192 # [START bigtable_delete_family]
215193 print 'Delete a column family cf2...'
@@ -218,8 +196,8 @@ def run_table_operations(project_id, instance_id, table_id):
218196 print 'Column family cf2 deleted successfully.'
219197 # [END bigtable_delete_family]
220198
221- print 'execute command python tableadmin.py delete [project_id] \
222- [instance_id] --table [tableName] to delete the table.'
199+ print 'execute command " python tableadmin.py delete [project_id] \
200+ [instance_id] --table [tableName]" to delete the table.'
223201
224202
225203def exists (instance_obj , table_id ):
@@ -234,7 +212,8 @@ def exists(instance_obj, table_id):
234212 :param instance_obj: Instance object.
235213
236214 :type table_id: str
237- :param table_id: Table id to create table.
215+ :param table_id: Table id to identify table.
216+
238217 Returns bool
239218 """
240219 for table in instance_obj .list_tables ():
@@ -260,6 +239,9 @@ def delete_table(project_id, instance_id, table_id):
260239 instance = client .instance (instance_id )
261240 table = instance .table (table_id )
262241
242+ # [START bigtable_delete_table]
243+ # Delete the entire table
244+
263245 print 'Checking if table {} exists...' .format (table_id )
264246 if exists (instance , table_id ):
265247 print 'Table {} exists.' .format (table_id )
@@ -268,6 +250,7 @@ def delete_table(project_id, instance_id, table_id):
268250 print 'Deleted {} table.' .format (table_id )
269251 else :
270252 print 'Table {} does not exists.' .format (table_id )
253+ # [END bigtable_delete_table]
271254
272255
273256if __name__ == '__main__' :
0 commit comments