Skip to content

Commit 811c2bf

Browse files
committed
Add tableadmin sample
tableadmin sample for gcloud python samples.
1 parent 5e5ea6d commit 811c2bf

File tree

5 files changed

+284
-0
lines changed

5 files changed

+284
-0
lines changed

.project

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>python-docs-samples</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
</buildSpec>
9+
<natures>
10+
</natures>
11+
</projectDescription>

bigtable/.DS_Store

6 KB
Binary file not shown.

bigtable/tableadmin/.DS_Store

6 KB
Binary file not shown.

bigtable/tableadmin/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
google-cloud-bigtable==0.29.0

bigtable/tableadmin/tableadmin.py

Lines changed: 272 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,272 @@
1+
# Copyright 2016 Google Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
"""Demonstrates how to connect to Cloud Bigtable and run some basic operations.
15+
# http://www.apache.org/licenses/LICENSE-2.0
16+
Prerequisites:
17+
- Create a Cloud Bigtable cluster.
18+
https://cloud.google.com/bigtable/docs/creating-cluster
19+
- Set your Google Application Default Credentials.
20+
https://developers.google.com/identity/protocols/application-default-credentials
21+
"""
22+
23+
import datetime
24+
import argparse
25+
from google.cloud import bigtable
26+
27+
def run_table_operations(project_id, instance_id, table_id):
28+
''' Create bigtable and perform different operations on it.
29+
:project_id: Project id of the client
30+
:instance_id: Instance of the client
31+
:table_id: Table id to create table.
32+
'''
33+
34+
client = bigtable.Client(project=project_id, admin=True)
35+
instance = client.instance(instance_id)
36+
table = instance.table(table_id)
37+
38+
table_exists = exists()
39+
print('Checking if table {} exists...'.format(table_id))
40+
try:
41+
42+
if table_exists:
43+
print('Table {} already exists.'.format(table_id))
44+
except :
45+
print('Error checking table exists.'.format(table_id))
46+
47+
if not table_exists:
48+
print('Creating table {}.'.format(table_id))
49+
try:
50+
table.create()
51+
except :
52+
print('Error creating table {}.'.format(table_id))
53+
else:
54+
print('Created table {}.'.format(table_id))
55+
56+
try:
57+
tables = instance.list_tables() #table.get_tables()
58+
59+
except:
60+
print('Error listing tables.')
61+
62+
print('Listing tables in current project...')
63+
if tables != []:
64+
for t in tables:
65+
print t.name
66+
else:
67+
print('No table exists in current project...')
68+
69+
70+
print('Printing table metadata...')
71+
try:
72+
print(table.name)
73+
except :
74+
print('Error retrieving table metadata.')
75+
76+
return 1
77+
print('Creating column family cf1 with with MaxAge GC Rule...')
78+
# [START bigtable_create_family_gc_max_age]
79+
# Create a column family with GC policy : maximum age
80+
# where age = current time minus cell timestamp
81+
column_family_id1 = 'cf1'
82+
try:
83+
# Define the GC rule to retain data with max age of 5 days
84+
max_age = datetime.timedelta(days=5)
85+
max_age_rule = table.max_age_gc_rule(max_age)
86+
print('Created column MaxAge GC rule')
87+
except:
88+
print('Error creating MaxAge GC Rule.')
89+
90+
try:
91+
cf1 = table.column_family(column_family_id1, max_age_rule)
92+
cf1.create()
93+
print('Created column family cf1 with MaxAge GC Rule.')
94+
except:
95+
print('Error creating column family with MaxAge GC Rule.')
96+
# [End bigtable_create_family_gc_max_age]
97+
98+
99+
print('Creating column family cf2 with max versions GC rule...')
100+
# [START bigtable_create_family_gc_max_versions]
101+
# Create a column family with GC policy : most recent N versions
102+
# where 1 = most recent version
103+
column_family_id2 = 'cf2'
104+
try:
105+
# Define the GC policy to retain only the most recent 2 versions
106+
max_versions = 2
107+
max_versions_rule = table.max_versions_gc_rule(max_versions)
108+
print('Created column Max Versions GC Rule.')
109+
except:
110+
print('Error creating Max Versions GC Rule.')
111+
112+
try:
113+
cf2 = table.column_family(column_family_id2, max_versions_rule)
114+
cf2 .create()
115+
print('Created column family cf2 with Max Versions GC Rule.')
116+
except:
117+
print('Error creating column family with Max Versions GC Rule.')
118+
# [END bigtable_create_family_gc_max_versions]
119+
120+
121+
print('Creating column family cf3 with union GC rule...')
122+
# [START bigtable_create_family_gc_union]
123+
# Create a column family with GC policy to drop data that matches at least one condition.
124+
column_family_id3 = 'cf3'
125+
try:
126+
# GC rule : Drop max age rule OR the most recent version rule.
127+
union = [max_age_rule, max_versions_rule]
128+
union_rule = table.gc_rule_union(union)
129+
print('Created column Union GC Rule.')
130+
except:
131+
print('Error creating Union GC Rule.')
132+
133+
try:
134+
cf3 = table.column_family(column_family_id3, union_rule)
135+
cf3.create()
136+
print('Created column family cf3 with Union GC rule')
137+
except:
138+
print('Error creating column family with Union GC rule')
139+
# [END bigtable_create_family_gc_union]
140+
141+
142+
143+
print('Creating column family cf4 with Intersection GC rule...')
144+
# [START bigtable_create_family_gc_intersection]
145+
# Create a column family with GC policy to drop data that matches all conditions
146+
column_family_id4 = 'cf4'
147+
148+
try:
149+
# GC rule: Drop cells older than 5 days AND older than the most recent 2 versions
150+
intersection = [max_age_rule, max_versions_rule]
151+
intersection_rule = table.gc_rule_intersection(union)
152+
print('Created column Intersection GC Rule.')
153+
except:
154+
print('Error creating Intersection GC Rule.')
155+
156+
try:
157+
cf4 = table.column_family(column_family_id4, intersection_rule)
158+
cf4.create()
159+
print('Created column family cf4 with Intersection GC rule.')
160+
except:
161+
print('Error creating column family with Intersection GC rule.')
162+
# [END bigtable_create_family_gc_intersection]
163+
164+
165+
print('Creating column family cf5 with a Nested GC rule...')
166+
# [START bigtable_create_family_gc_nested]
167+
# Create a column family with nested GC policys.
168+
169+
# Create a nested GC rule:
170+
# Drop cells that are either older than the 10 recent versions
171+
# OR
172+
# Drop cells that are older than a month AND older than the 2 recent versions
173+
column_family_id5 = 'cf5'
174+
try:
175+
# Drop cells that are either older than the 10 recent versions
176+
max_versions_rule1 = table.max_versions_gc_rule(10)
177+
178+
# Drop cells that are older than a month AND older than the 2 recent versions
179+
max_age = datetime.timedelta(days=30)
180+
max_age_rule = table.max_age_gc_rule(max_age)
181+
max_versions_rule2 = table.max_versions_gc_rule(2)
182+
intersection = [max_age_rule, max_versions_rule2]
183+
intersection_rule = table.gc_rule_intersection(intersection)
184+
185+
# This nesting is done with union rule since it is OR between the selected rules.
186+
nest = [max_versions_rule1, intersection_rule]
187+
nested_rule = table.gc_rule_union(nest)
188+
print('Created column Nested GC Rule.')
189+
except:
190+
print('Error creating Nested GC Rule.')
191+
192+
try:
193+
cf5 = table.column_family(column_family_id5, nested_rule)
194+
cf5.create()
195+
print('Created column family cf5 with a Nested GC rule.')
196+
except:
197+
print('Error creating column family with a Nested GC rule.')
198+
# [END bigtable_create_family_gc_nested]
199+
200+
201+
print('Printing ID and GC Rule for all column families...')
202+
cf = table.list_column_families()
203+
column_families = [key for key,val in cf.items()]
204+
for cf,gc_rule in sorted(cf.items()):
205+
print "Column Family:", cf
206+
print "GC Rule:"
207+
print list_gc_rules(gc_rule)
208+
209+
def list_gc_rules(column_family):
210+
return column_family.gc_rule.to_pb()
211+
212+
def exists():
213+
return False
214+
215+
def delete_table(project_id, instance_id, table_id):
216+
''' Delete bigtable.
217+
:project_id: Project id of the client
218+
:instance_id: Instance of the client
219+
:table_id: Table id to create table.
220+
'''
221+
222+
client = bigtable.Client(project=project_id, admin=True)
223+
instance = client.instance(instance_id)
224+
table = instance.table(table_id)
225+
table_exists = False
226+
227+
print('Checking if {} table exists...'.format(table_id))
228+
try:
229+
table_exists = table.exists()
230+
except :
231+
print('Error checking table exists.'.format(table_id))
232+
233+
#if not table_exists:
234+
if False:
235+
print('Table {} does not exists.'.format(table_id))
236+
else:
237+
try:
238+
print('Deleting {} table.'.format(table_id))
239+
table.delete()
240+
except:
241+
print('Error deleting {} table.'.format(table_id))
242+
else:
243+
print('Deleted {} table.'.format(table_id))
244+
245+
246+
if __name__ == '__main__':
247+
parser = argparse.ArgumentParser(
248+
description=__doc__,
249+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
250+
251+
parser.add_argument('command',
252+
help='run or delete. Operation to perform on table.')
253+
parser.add_argument(
254+
'--table',
255+
help='Cloud Bigtable Table name.',
256+
default='Hello-Bigtable')
257+
258+
parser.add_argument('project_id', help='Your Cloud Platform project ID.')
259+
parser.add_argument(
260+
'instance_id', help='ID of the Cloud Bigtable instance to connect to.')
261+
262+
args = parser.parse_args()
263+
264+
if args.command.lower() == 'run':
265+
run_table_operations(args.project_id, args.instance_id, args.table)
266+
delete_table(args.project_id, args.instance_id, args.table)
267+
elif args.command.lower() == 'delete':
268+
delete_table(args.project_id, args.instance_id, args.table)
269+
else:
270+
print ('Command should be either run or delete.\n Use argument -h, --help to show help and exit.')
271+
272+

0 commit comments

Comments
 (0)