-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathe2e.py
More file actions
74 lines (62 loc) · 2.99 KB
/
e2e.py
File metadata and controls
74 lines (62 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""Simple Bigtable client demonstrating auto-generated veneer.
"""
import argparse
import logging
from google.bigtable.v1 import bigtable_service_api
from google.bigtable.admin.cluster.v1 import bigtable_cluster_service_api
from google.bigtable.admin.table.v1 import bigtable_table_service_api
from google.bigtable.v1 import bigtable_data_pb2 as data
from google.bigtable.admin.cluster.v1 import bigtable_cluster_data_pb2 as cluster_data
from google.bigtable.admin.table.v1 import bigtable_table_data_pb2 as table_data
def run(project_id):
with bigtable_service_api.BigtableServiceApi() as bigtable_api, \
bigtable_cluster_service_api.BigtableClusterServiceApi() as cluster_api, \
bigtable_table_service_api.BigtableTableServiceApi() as table_api:
disp_name = 'my-cluster'
zone_name = 'projects/{0}/zones/{1}'.format(project_id, 'us-central1-c')
employee_id='employee1'
try:
print 'Creating a cluster.'
cluster = cluster_data.Cluster(display_name=disp_name, serve_nodes=3)
cluster_name = cluster_api.create_cluster(
name=zone_name, cluster_id=disp_name, cluster=cluster).name
print 'Successfully created a cluster named {0}'.format(cluster_name)
print 'Creating a bigtable.'
table_name = table_api.create_table(
table=table_data.Table(), name=cluster_name, table_id='my-table').name
name_column_family = table_api.create_column_family(
name=table_name, column_family_id='Name',
column_family=table_data.ColumnFamily())
bday_column_family = table_api.create_column_family(
name=table_name, column_family_id='Birthday',
column_family=table_data.ColumnFamily())
print 'Successfully created a table named {0}'.format(table_name)
print 'Writing some data to the table.'
rule1 = data.ReadModifyWriteRule(
family_name='Name', column_qualifier='First Name',
append_value='Jane')
rule2 = data.ReadModifyWriteRule(
family_name='Name', column_qualifier='Last Name', append_value='Doe')
rule3 = data.ReadModifyWriteRule(
family_name='Birthday', column_qualifier='date',
append_value='Feb. 29')
bigtable_api.read_modify_write_row(
table_name=table_name, row_key=employee_id,
rules=[rule1, rule2, rule3])
print 'Reading the data we wrote to the table.'
for response in bigtable_api.read_rows(
table_name=table_name, row_key=employee_id):
print response
print 'Deleting the table and cluster.'
table_api.delete_table(name=table_name)
cluster_api.delete_cluster(name=cluster_name)
except Exception as exception:
logging.exception(exception)
print 'failed with {0}:{1}'.format(exception.code, exception.details)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'--project_id', help='The numerical id of the project to create bigtable in.',
required=True)
args = parser.parse_args()
run(args.project_id)