34
34
import datetime
35
35
36
36
from google .cloud import bigtable
37
+ from google .cloud .bigtable import column_family
37
38
38
39
39
40
def run_table_operations (project_id , instance_id , table_id ):
@@ -56,7 +57,7 @@ def run_table_operations(project_id, instance_id, table_id):
56
57
# Check whether table exists in an instance.
57
58
# Create table if it does not exists.
58
59
print 'Checking if table {} exists...' .format (table_id )
59
- if exists (instance , table_id ):
60
+ if table . exists ():
60
61
print 'Table {} already exists.' .format (table_id )
61
62
else :
62
63
print 'Creating the {} table.' .format (table_id )
@@ -79,10 +80,10 @@ def run_table_operations(project_id, instance_id, table_id):
79
80
# where age = current time minus cell timestamp
80
81
81
82
# 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 ))
83
84
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 ()
86
87
print 'Created column family cf1 with MaxAge GC Rule.'
87
88
# [END bigtable_create_family_gc_max_age]
88
89
@@ -92,10 +93,10 @@ def run_table_operations(project_id, instance_id, table_id):
92
93
# where 1 = most recent version
93
94
94
95
# 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 )
96
97
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 ()
99
100
print 'Created column family cf2 with Max Versions GC Rule.'
100
101
# [END bigtable_create_family_gc_max_versions]
101
102
@@ -105,12 +106,12 @@ def run_table_operations(project_id, instance_id, table_id):
105
106
# at least one condition.
106
107
# Define a GC rule to drop cells older than 5 days or not the
107
108
# 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 )])
111
112
112
- cf3 = table .column_family ('cf3' , union_rule )
113
- cf3 .create ()
113
+ column_family3 = table .column_family ('cf3' , union_rule )
114
+ column_family3 .create ()
114
115
print 'Created column family cf3 with Union GC rule'
115
116
# [END bigtable_create_family_gc_union]
116
117
@@ -120,12 +121,12 @@ def run_table_operations(project_id, instance_id, table_id):
120
121
# all conditions
121
122
# GC rule: Drop cells older than 5 days AND older than the most
122
123
# 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 )])
126
127
127
- cf4 = table .column_family ('cf4' , intersection_rule )
128
- cf4 .create ()
128
+ column_family4 = table .column_family ('cf4' , intersection_rule )
129
+ column_family4 .create ()
129
130
print 'Created column family cf4 with Intersection GC rule.'
130
131
# [END bigtable_create_family_gc_intersection]
131
132
@@ -137,24 +138,24 @@ def run_table_operations(project_id, instance_id, table_id):
137
138
# OR
138
139
# Drop cells that are older than a month AND older than the
139
140
# 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 )])
144
145
145
- nested_rule = table . gc_rule_union ([rule1 , rule2 ])
146
+ nested_rule = column_family . GCRuleUnion ([rule1 , rule2 ])
146
147
147
- cf5 = table .column_family ('cf5' , nested_rule )
148
- cf5 .create ()
148
+ column_family5 = table .column_family ('cf5' , nested_rule )
149
+ column_family5 .create ()
149
150
print 'Created column family cf5 with a Nested GC rule.'
150
151
# [END bigtable_create_family_gc_nested]
151
152
152
153
# [START bigtable_list_column_families]
153
154
print 'Printing Column Family and GC Rule for all column families...'
154
155
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:'
158
159
print gc_rule .to_pb ()
159
160
# Sample output:
160
161
# Column Family: cf4
@@ -174,54 +175,34 @@ def run_table_operations(project_id, instance_id, table_id):
174
175
# [END bigtable_list_column_families]
175
176
176
177
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 ()
179
180
180
181
# [START bigtable_update_gc_rule]
181
182
print 'Updating column family cf1 GC rule...'
182
183
# 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 ()
185
188
print 'Updated column family cf1 GC rule\n '
186
189
# [END bigtable_update_gc_rule]
187
190
188
191
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 ()
191
194
192
195
# [START bigtable_delete_family]
193
196
print 'Delete a column family cf2...'
194
197
# Delete a column family
195
- cf2 .delete ()
198
+ column_family2 .delete ()
196
199
print 'Column family cf2 deleted successfully.'
197
200
# [END bigtable_delete_family]
198
201
199
202
print 'execute command "python tableadmin.py delete [project_id] \
200
203
[instance_id] --table [tableName]" to delete the table.'
201
204
202
205
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
-
225
206
def delete_table (project_id , instance_id , table_id ):
226
207
''' Delete bigtable.
227
208
@@ -243,7 +224,7 @@ def delete_table(project_id, instance_id, table_id):
243
224
# Delete the entire table
244
225
245
226
print 'Checking if table {} exists...' .format (table_id )
246
- if exists (instance , table_id ):
227
+ if table . exists ():
247
228
print 'Table {} exists.' .format (table_id )
248
229
print 'Deleting {} table.' .format (table_id )
249
230
table .delete ()
@@ -259,22 +240,26 @@ def delete_table(project_id, instance_id, table_id):
259
240
formatter_class = argparse .ArgumentDefaultsHelpFormatter )
260
241
261
242
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.' )
263
245
parser .add_argument (
264
246
'--table' ,
265
247
help = 'Cloud Bigtable Table name.' ,
266
248
default = 'Hello-Bigtable' )
267
249
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.' )
269
252
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.' )
271
255
272
256
args = parser .parse_args ()
273
257
274
258
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 )
276
261
elif args .command .lower () == 'delete' :
277
262
delete_table (args .project_id , args .instance_id , args .table )
278
263
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,\
280
265
--help to show help and exit.'
0 commit comments