Skip to content

Commit 2e3ab14

Browse files
authored
Merge pull request #384 from GoogleCloudPlatform/tswast-bigtable
bigtable: add raw gcloud-python hello sample.
2 parents 30bdf81 + 4fb0425 commit 2e3ab14

File tree

7 files changed

+218
-10
lines changed

7 files changed

+218
-10
lines changed

bigtable/hello/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Cloud Bigtable Hello World
2+
3+
This is a simple application that demonstrates using the [Google Cloud Client
4+
Library][gcloud-python] to connect to and interact with Cloud Bigtable.
5+
6+
[gcloud-python]: https://github.com/GoogleCloudPlatform/gcloud-python
7+
8+
9+
## Provision a cluster
10+
11+
Follow the instructions in the [user documentation](https://cloud.google.com/bigtable/docs/creating-cluster)
12+
to create a Google Cloud Platform project and Cloud Bigtable cluster if necessary.
13+
You'll need to reference your project ID, zone and cluster ID to run the application.
14+
15+
16+
## Run the application
17+
18+
First, set your [Google Application Default Credentials](https://developers.google.com/identity/protocols/application-default-credentials)
19+
20+
Install the dependencies with pip.
21+
22+
```
23+
$ pip install -r requirements.txt
24+
```
25+
26+
Run the application. Replace the command-line parameters with values for your cluster.
27+
28+
```
29+
$ python main.py my-project my-cluster us-central1-c
30+
```
31+
32+
You will see output resembling the following:
33+
34+
```
35+
Create table Hello-Bigtable-1234
36+
Write some greetings to the table
37+
Scan for all greetings:
38+
greeting0: Hello World!
39+
greeting1: Hello Cloud Bigtable!
40+
greeting2: Hello HappyBase!
41+
Delete table Hello-Bigtable-1234
42+
```

bigtable/hello/main.py

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2016 Google Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""Demonstrates how to connect to Cloud Bigtable and run some basic operations.
18+
19+
Prerequisites:
20+
21+
- Create a Cloud Bigtable cluster.
22+
https://cloud.google.com/bigtable/docs/creating-cluster
23+
- Set your Google Application Default Credentials.
24+
https://developers.google.com/identity/protocols/application-default-credentials
25+
"""
26+
27+
import argparse
28+
29+
from gcloud import bigtable
30+
31+
32+
def main(project_id, cluster_id, zone, table_id):
33+
# [START connecting_to_bigtable]
34+
# The client must be created with admin=True because it will create a
35+
# table.
36+
with bigtable.Client(project=project_id, admin=True) as client:
37+
cluster = client.cluster(zone, cluster_id)
38+
# [END connecting_to_bigtable]
39+
40+
# [START creating_a_table]
41+
print('Creating the {} table.'.format(table_id))
42+
table = cluster.table(table_id)
43+
table.create()
44+
column_family_id = 'cf1'
45+
cf1 = table.column_family(column_family_id)
46+
cf1.create()
47+
# [END creating_a_table]
48+
49+
# [START writing_rows]
50+
print('Writing some greetings to the table.')
51+
column_id = 'greeting'.encode('utf-8')
52+
greetings = [
53+
'Hello World!',
54+
'Hello Cloud Bigtable!',
55+
'Hello Python!',
56+
]
57+
58+
for i, value in enumerate(greetings):
59+
# Note: This example uses sequential numeric IDs for simplicity,
60+
# but this can result in poor performance in a production
61+
# application. Since rows are stored in sorted order by key,
62+
# sequential keys can result in poor distribution of operations
63+
# across nodes.
64+
#
65+
# For more information about how to design a Bigtable schema for
66+
# the best performance, see the documentation:
67+
#
68+
# https://cloud.google.com/bigtable/docs/schema-design
69+
row_key = 'greeting{}'.format(i)
70+
row = table.row(row_key)
71+
row.set_cell(
72+
column_family_id,
73+
column_id.encode('utf-8'),
74+
value.encode('utf-8'))
75+
row.commit()
76+
# [END writing_rows]
77+
78+
# [START getting_a_row]
79+
print('Getting a single greeting by row key.')
80+
key = 'greeting0'
81+
row = table.read_row(key.encode('utf-8'))
82+
value = row.cells[column_family_id][column_id.encode('utf-8')][0].value
83+
print('\t{}: {}'.format(key, value.decode('utf-8')))
84+
# [END getting_a_row]
85+
86+
# [START scanning_all_rows]
87+
print('Scanning for all greetings:')
88+
partial_rows = table.read_rows()
89+
partial_rows.consume_all()
90+
91+
for row_key, row in partial_rows.rows.items():
92+
key = row_key.decode('utf-8')
93+
cell = row.cells[column_family_id][column_id.encode('utf-8')][0]
94+
value = cell.value.decode('utf-8')
95+
print('\t{}: {}'.format(key, value))
96+
# [END scanning_all_rows]
97+
98+
# [START deleting_a_table]
99+
print('Deleting the {} table.'.format(table_id))
100+
table.delete()
101+
# [END deleting_a_table]
102+
103+
104+
if __name__ == '__main__':
105+
parser = argparse.ArgumentParser(
106+
description=__doc__,
107+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
108+
parser.add_argument('project_id', help='Your Cloud Platform project ID.')
109+
parser.add_argument(
110+
'cluster', help='ID of the Cloud Bigtable cluster to connect to.')
111+
parser.add_argument(
112+
'zone', help='Zone that contains the Cloud Bigtable cluster.')
113+
parser.add_argument(
114+
'--table',
115+
help='Table to create and destroy.',
116+
default='Hello-Bigtable')
117+
118+
args = parser.parse_args()
119+
main(args.project_id, args.cluster, args.zone, args.table)

bigtable/hello/main_test.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import random
16+
import re
17+
import sys
18+
19+
from main import main
20+
21+
import pytest
22+
23+
TABLE_NAME_FORMAT = 'Hello-Bigtable-{}'
24+
TABLE_NAME_RANGE = 10000
25+
26+
27+
@pytest.mark.skipif(
28+
sys.version_info >= (3, 0),
29+
reason=("grpc doesn't yet support python3 "
30+
'https://github.com/grpc/grpc/issues/282'))
31+
def test_main(cloud_config, capsys):
32+
table_name = TABLE_NAME_FORMAT.format(
33+
random.randrange(TABLE_NAME_RANGE))
34+
main(
35+
cloud_config.project,
36+
cloud_config.bigtable_cluster,
37+
cloud_config.bigtable_zone,
38+
table_name)
39+
40+
out, _ = capsys.readouterr()
41+
assert re.search(
42+
re.compile(r'Creating the Hello-Bigtable-[0-9]+ table\.'), out)
43+
assert re.search(re.compile(r'Writing some greetings to the table\.'), out)
44+
assert re.search(re.compile(r'Getting a single greeting by row key.'), out)
45+
assert re.search(re.compile(r'greeting0: Hello World!'), out)
46+
assert re.search(re.compile(r'Scanning for all greetings'), out)
47+
assert re.search(re.compile(r'greeting1: Hello Cloud Bigtable!'), out)
48+
assert re.search(
49+
re.compile(r'Deleting the Hello-Bigtable-[0-9]+ table\.'), out)

bigtable/hello/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
gcloud[grpc]==0.16.0

bigtable/hello_happybase/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Cloud Bigtable Hello World
1+
# Cloud Bigtable Hello World (HappyBase)
22

33
This is a simple application that demonstrates using the [Google Cloud Client
44
Library][gcloud-python] to connect to and interact with Cloud Bigtable.

bigtable/hello_happybase/main.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
https://cloud.google.com/bigtable/docs/creating-cluster
2323
- Set your Google Application Default Credentials.
2424
https://developers.google.com/identity/protocols/application-default-credentials
25-
- Set the GCLOUD_PROJECT environment variable to your project ID.
26-
https://support.google.com/cloud/answer/6158840
2725
"""
2826

2927
import argparse
@@ -61,6 +59,7 @@ def main(project_id, cluster_id, zone, table_name):
6159
'Hello Cloud Bigtable!',
6260
'Hello HappyBase!',
6361
]
62+
6463
for i, value in enumerate(greetings):
6564
# Note: This example uses sequential numeric IDs for simplicity,
6665
# but this can result in poor performance in a production
@@ -85,6 +84,7 @@ def main(project_id, cluster_id, zone, table_name):
8584

8685
# [START scanning_all_rows]
8786
print('Scanning for all greetings:')
87+
8888
for key, row in table.scan():
8989
print('\t{}: {}'.format(key, row[column_name]))
9090
# [END scanning_all_rows]
@@ -93,19 +93,16 @@ def main(project_id, cluster_id, zone, table_name):
9393
print('Deleting the {} table.'.format(table_name))
9494
connection.delete_table(table_name)
9595
# [END deleting_a_table]
96+
9697
finally:
9798
connection.close()
9899

99100

100101
if __name__ == '__main__':
101102
parser = argparse.ArgumentParser(
102-
description='A sample application that connects to Cloud' +
103-
' Bigtable.',
103+
description=__doc__,
104104
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
105-
parser.add_argument(
106-
'project_id',
107-
help='Google Cloud Platform project ID that contains the Cloud' +
108-
' Bigtable cluster.')
105+
parser.add_argument('project_id', help='Your Cloud Platform project ID.')
109106
parser.add_argument(
110107
'cluster', help='ID of the Cloud Bigtable cluster to connect to.')
111108
parser.add_argument(
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
gcloud[grpc]==0.14.0
1+
gcloud[grpc]==0.16.0

0 commit comments

Comments
 (0)