3434import datetime
3535
3636from google .cloud import bigtable
37+ from google .cloud .bigtable import column_family
3738
3839
3940def run_table_operations (project_id , instance_id , table_id ):
@@ -56,7 +57,7 @@ def run_table_operations(project_id, instance_id, table_id):
5657 # Check whether table exists in an instance.
5758 # Create table if it does not exists.
5859 print 'Checking if table {} exists...' .format (table_id )
59- if exists (instance , table_id ):
60+ if table . exists ():
6061 print 'Table {} already exists.' .format (table_id )
6162 else :
6263 print 'Creating the {} table.' .format (table_id )
@@ -79,10 +80,10 @@ def run_table_operations(project_id, instance_id, table_id):
7980 # where age = current time minus cell timestamp
8081
8182 # Define the GC rule to retain data with max age of 5 days
82- max_age_rule = table . max_age_gc_rule (datetime .timedelta (days = 5 ))
83+ max_age_rule = column_family . MaxAgeGCRule (datetime .timedelta (days = 5 ))
8384
84- cf1 = table .column_family ('cf1' , max_age_rule )
85- cf1 .create ()
85+ column_family1 = table .column_family ('cf1' , max_age_rule )
86+ column_family1 .create ()
8687 print 'Created column family cf1 with MaxAge GC Rule.'
8788 # [END bigtable_create_family_gc_max_age]
8889
@@ -92,10 +93,10 @@ def run_table_operations(project_id, instance_id, table_id):
9293 # where 1 = most recent version
9394
9495 # Define the GC policy to retain only the most recent 2 versions
95- max_versions_rule = table . max_versions_gc_rule (2 )
96+ max_versions_rule = column_family . MaxVersionsGCRule (2 )
9697
97- cf2 = table .column_family ('cf2' , max_versions_rule )
98- cf2 .create ()
98+ column_family2 = table .column_family ('cf2' , max_versions_rule )
99+ column_family2 .create ()
99100 print 'Created column family cf2 with Max Versions GC Rule.'
100101 # [END bigtable_create_family_gc_max_versions]
101102
@@ -105,12 +106,12 @@ def run_table_operations(project_id, instance_id, table_id):
105106 # at least one condition.
106107 # Define a GC rule to drop cells older than 5 days or not the
107108 # 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 )])
109+ union_rule = column_family . GCRuleUnion ([
110+ column_family . MaxAgeGCRule (datetime .timedelta (days = 5 )),
111+ column_family . MaxVersionsGCRule (2 )])
111112
112- cf3 = table .column_family ('cf3' , union_rule )
113- cf3 .create ()
113+ column_family3 = table .column_family ('cf3' , union_rule )
114+ column_family3 .create ()
114115 print 'Created column family cf3 with Union GC rule'
115116 # [END bigtable_create_family_gc_union]
116117
@@ -120,12 +121,12 @@ def run_table_operations(project_id, instance_id, table_id):
120121 # all conditions
121122 # GC rule: Drop cells older than 5 days AND older than the most
122123 # recent 2 versions
123- intersection_rule = table . gc_rule_intersection ([
124- table . max_age_gc_rule (datetime .timedelta (days = 5 )),
125- table . max_versions_gc_rule (2 )])
124+ intersection_rule = column_family . GCRuleIntersection ([
125+ column_family . MaxAgeGCRule (datetime .timedelta (days = 5 )),
126+ column_family . MaxVersionsGCRule (2 )])
126127
127- cf4 = table .column_family ('cf4' , intersection_rule )
128- cf4 .create ()
128+ column_family4 = table .column_family ('cf4' , intersection_rule )
129+ column_family4 .create ()
129130 print 'Created column family cf4 with Intersection GC rule.'
130131 # [END bigtable_create_family_gc_intersection]
131132
@@ -137,24 +138,24 @@ def run_table_operations(project_id, instance_id, table_id):
137138 # OR
138139 # Drop cells that are older than a month AND older than the
139140 # 2 recent versions
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 )])
141+ rule1 = column_family . MaxVersionsGCRule (10 )
142+ rule2 = column_family . GCRuleIntersection ([
143+ column_family . MaxAgeGCRule (datetime .timedelta (days = 30 )),
144+ column_family . MaxVersionsGCRule (2 )])
144145
145- nested_rule = table . gc_rule_union ([rule1 , rule2 ])
146+ nested_rule = column_family . GCRuleUnion ([rule1 , rule2 ])
146147
147- cf5 = table .column_family ('cf5' , nested_rule )
148- cf5 .create ()
148+ column_family5 = table .column_family ('cf5' , nested_rule )
149+ column_family5 .create ()
149150 print 'Created column family cf5 with a Nested GC rule.'
150151 # [END bigtable_create_family_gc_nested]
151152
152153 # [START bigtable_list_column_families]
153154 print 'Printing Column Family and GC Rule for all column families...'
154155 column_families = table .list_column_families ()
155- for column_family , gc_rule in sorted (column_families .items ()):
156- print " Column Family:" , column_family
157- print " GC Rule:"
156+ for column_family_name , gc_rule in sorted (column_families .items ()):
157+ print ' Column Family:' , column_family_name
158+ print ' GC Rule:'
158159 print gc_rule .to_pb ()
159160 # Sample output:
160161 # Column Family: cf4
@@ -174,54 +175,34 @@ def run_table_operations(project_id, instance_id, table_id):
174175 # [END bigtable_list_column_families]
175176
176177 print 'Print column family cf1 GC rule before update...'
177- print " Column Family: cf1"
178- print cf1 .to_pb ()
178+ print ' Column Family: cf1'
179+ print column_family1 .to_pb ()
179180
180181 # [START bigtable_update_gc_rule]
181182 print 'Updating column family cf1 GC rule...'
182183 # Update the column family cf1 to update the GC rule
183- cf1 = table .column_family ('cf1' , table .max_versions_gc_rule (1 ))
184- cf1 .update ()
184+ column_family1 = table .column_family (
185+ 'cf1' ,
186+ column_family .MaxVersionsGCRule (1 ))
187+ column_family1 .update ()
185188 print 'Updated column family cf1 GC rule\n '
186189 # [END bigtable_update_gc_rule]
187190
188191 print 'Print column family cf1 GC rule after update...'
189- print " Column Family: cf1"
190- print cf1 .to_pb ()
192+ print ' Column Family: cf1'
193+ print column_family1 .to_pb ()
191194
192195 # [START bigtable_delete_family]
193196 print 'Delete a column family cf2...'
194197 # Delete a column family
195- cf2 .delete ()
198+ column_family2 .delete ()
196199 print 'Column family cf2 deleted successfully.'
197200 # [END bigtable_delete_family]
198201
199202 print 'execute command "python tableadmin.py delete [project_id] \
200203 [instance_id] --table [tableName]" to delete the table.'
201204
202205
203- def exists (instance_obj , table_id ):
204- """ Check whether table exists or not.
205-
206- .. note::
207-
208- This is temporary function as code for this is under development.
209- Once complete, this function would be removed.
210-
211- :type instance_obj: Instance Class.
212- :param instance_obj: Instance object.
213-
214- :type table_id: str
215- :param table_id: Table id to identify table.
216-
217- Returns bool
218- """
219- for table in instance_obj .list_tables ():
220- if table_id == table .table_id :
221- return True
222- return False
223-
224-
225206def delete_table (project_id , instance_id , table_id ):
226207 ''' Delete bigtable.
227208
@@ -243,7 +224,7 @@ def delete_table(project_id, instance_id, table_id):
243224 # Delete the entire table
244225
245226 print 'Checking if table {} exists...' .format (table_id )
246- if exists (instance , table_id ):
227+ if table . exists ():
247228 print 'Table {} exists.' .format (table_id )
248229 print 'Deleting {} table.' .format (table_id )
249230 table .delete ()
@@ -259,22 +240,26 @@ def delete_table(project_id, instance_id, table_id):
259240 formatter_class = argparse .ArgumentDefaultsHelpFormatter )
260241
261242 parser .add_argument ('command' ,
262- help = 'run or delete. Operation to perform on table.' )
243+ help = 'run or delete. \
244+ Operation to perform on table.' )
263245 parser .add_argument (
264246 '--table' ,
265247 help = 'Cloud Bigtable Table name.' ,
266248 default = 'Hello-Bigtable' )
267249
268- parser .add_argument ('project_id' , help = 'Your Cloud Platform project ID.' )
250+ parser .add_argument ('project_id' ,
251+ help = 'Your Cloud Platform project ID.' )
269252 parser .add_argument (
270- 'instance_id' , help = 'ID of the Cloud Bigtable instance to connect to.' )
253+ 'instance_id' ,
254+ help = 'ID of the Cloud Bigtable instance to connect to.' )
271255
272256 args = parser .parse_args ()
273257
274258 if args .command .lower () == 'run' :
275- run_table_operations (args .project_id , args .instance_id , args .table )
259+ run_table_operations (args .project_id , args .instance_id ,
260+ args .table )
276261 elif args .command .lower () == 'delete' :
277262 delete_table (args .project_id , args .instance_id , args .table )
278263 else :
279- print 'Command should be either run or delete.\n Use argument -h, \
264+ print 'Command should be either run or delete.\n Use argument -h,\
280265 --help to show help and exit.'
0 commit comments