@@ -63,31 +63,25 @@ def run_table_operations(project_id, instance_id, table_id):
63
63
table .create ()
64
64
print 'Created table {}.' .format (table_id )
65
65
66
- # [START List existing tables in the instance. ]
66
+ # [START bigtable_list_tables ]
67
67
tables = instance .list_tables ()
68
68
print 'Listing tables in current project...'
69
69
if tables != []:
70
70
for tbl in tables :
71
71
print tbl .table_id
72
72
else :
73
73
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]
79
75
80
76
# [START bigtable_create_family_gc_max_age]
81
77
print 'Creating column family cf1 with with MaxAge GC Rule...'
82
78
# Create a column family with GC policy : maximum age
83
79
# where age = current time minus cell timestamp
84
- column_family_id1 = 'cf1'
80
+
85
81
# 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 ))
89
83
90
- cf1 = table .column_family (column_family_id1 , max_age_rule )
84
+ cf1 = table .column_family ('cf1' , max_age_rule )
91
85
cf1 .create ()
92
86
print 'Created column family cf1 with MaxAge GC Rule.'
93
87
# [END bigtable_create_family_gc_max_age]
@@ -96,13 +90,11 @@ def run_table_operations(project_id, instance_id, table_id):
96
90
print 'Creating column family cf2 with max versions GC rule...'
97
91
# Create a column family with GC policy : most recent N versions
98
92
# where 1 = most recent version
99
- column_family_id2 = 'cf2'
93
+
100
94
# 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 )
104
96
105
- cf2 = table .column_family (column_family_id2 , max_versions_rule )
97
+ cf2 = table .column_family ('cf2' , max_versions_rule )
106
98
cf2 .create ()
107
99
print 'Created column family cf2 with Max Versions GC Rule.'
108
100
# [END bigtable_create_family_gc_max_versions]
@@ -111,13 +103,13 @@ def run_table_operations(project_id, instance_id, table_id):
111
103
print 'Creating column family cf3 with union GC rule...'
112
104
# Create a column family with GC policy to drop data that matches
113
105
# 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 )])
119
111
120
- cf3 = table .column_family (column_family_id3 , union_rule )
112
+ cf3 = table .column_family ('cf3' , union_rule )
121
113
cf3 .create ()
122
114
print 'Created column family cf3 with Union GC rule'
123
115
# [END bigtable_create_family_gc_union]
@@ -126,45 +118,33 @@ def run_table_operations(project_id, instance_id, table_id):
126
118
print 'Creating column family cf4 with Intersection GC rule...'
127
119
# Create a column family with GC policy to drop data that matches
128
120
# all conditions
129
- column_family_id4 = 'cf4'
130
121
# GC rule: Drop cells older than 5 days AND older than the most
131
122
# 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 )])
135
126
136
- cf4 = table .column_family (column_family_id4 , intersection_rule )
127
+ cf4 = table .column_family ('cf4' , intersection_rule )
137
128
cf4 .create ()
138
129
print 'Created column family cf4 with Intersection GC rule.'
139
130
# [END bigtable_create_family_gc_intersection]
140
131
141
132
# [START bigtable_create_family_gc_nested]
142
133
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 .
144
135
# Create a nested GC rule:
145
136
# Drop cells that are either older than the 10 recent versions
146
137
# OR
147
138
# Drop cells that are older than a month AND older than the
148
139
# 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 )
168
148
cf5 .create ()
169
149
print 'Created column family cf5 with a Nested GC rule.'
170
150
# [END bigtable_create_family_gc_nested]
@@ -193,23 +173,21 @@ def run_table_operations(project_id, instance_id, table_id):
193
173
# }
194
174
# [END bigtable_list_column_families]
195
175
176
+ print 'Print column family cf1 GC rule before update...'
177
+ print "Column Family: cf1"
178
+ print cf1 .to_pb ()
179
+
196
180
# [START bigtable_update_gc_rule]
197
181
print 'Updating column family cf1 GC rule...'
198
182
# 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 ))
203
184
cf1 .update ()
204
- print 'Updated column family cf1 GC rule'
185
+ print 'Updated column family cf1 GC rule\n '
205
186
# [END bigtable_update_gc_rule]
206
187
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"
211
190
print cf1 .to_pb ()
212
- # [END bigtable_family_get_gc_rule]
213
191
214
192
# [START bigtable_delete_family]
215
193
print 'Delete a column family cf2...'
@@ -218,8 +196,8 @@ def run_table_operations(project_id, instance_id, table_id):
218
196
print 'Column family cf2 deleted successfully.'
219
197
# [END bigtable_delete_family]
220
198
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.'
223
201
224
202
225
203
def exists (instance_obj , table_id ):
@@ -234,7 +212,8 @@ def exists(instance_obj, table_id):
234
212
:param instance_obj: Instance object.
235
213
236
214
:type table_id: str
237
- :param table_id: Table id to create table.
215
+ :param table_id: Table id to identify table.
216
+
238
217
Returns bool
239
218
"""
240
219
for table in instance_obj .list_tables ():
@@ -260,6 +239,9 @@ def delete_table(project_id, instance_id, table_id):
260
239
instance = client .instance (instance_id )
261
240
table = instance .table (table_id )
262
241
242
+ # [START bigtable_delete_table]
243
+ # Delete the entire table
244
+
263
245
print 'Checking if table {} exists...' .format (table_id )
264
246
if exists (instance , table_id ):
265
247
print 'Table {} exists.' .format (table_id )
@@ -268,6 +250,7 @@ def delete_table(project_id, instance_id, table_id):
268
250
print 'Deleted {} table.' .format (table_id )
269
251
else :
270
252
print 'Table {} does not exists.' .format (table_id )
253
+ # [END bigtable_delete_table]
271
254
272
255
273
256
if __name__ == '__main__' :
0 commit comments