Skip to content

Commit 6910136

Browse files
tswastJon Wayne Parrott
authored andcommitted
bigtable: Move hello to hello_happybase. [(googleapis#383)](GoogleCloudPlatform/python-docs-samples#383)
0 parents  commit 6910136

File tree

4 files changed

+236
-0
lines changed

4 files changed

+236
-0
lines changed

samples/hello_happybase/README.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
```
43+
44+
## Understanding the code
45+
46+
The [application](main.py) uses the [Google Cloud Bigtable HappyBase
47+
package][Bigtable HappyBase], an implementation of the [HappyBase][HappyBase]
48+
library, to make calls to Cloud Bigtable. It demonstrates several basic
49+
concepts of working with Cloud Bigtable via this API:
50+
51+
[Bigtable HappyBase]: https://googlecloudplatform.github.io/gcloud-python/stable/happybase-package.html
52+
[HappyBase]: http://happybase.readthedocs.io/en/latest/index.html
53+
54+
- Creating a [Connection][HappyBase Connection] to a Cloud Bigtable
55+
[Cluster][Cluster API].
56+
- Using the [Connection][HappyBase Connection] interface to create, disable and
57+
delete a [Table][HappyBase Table].
58+
- Using the Connection to get a Table.
59+
- Using the Table to write rows via a [put][HappyBase Table Put] and scan
60+
across multiple rows using [scan][HappyBase Table Scan].
61+
62+
[Cluster API]: https://googlecloudplatform.github.io/gcloud-python/stable/bigtable-cluster.html
63+
[HappyBase Connection]: https://googlecloudplatform.github.io/gcloud-python/stable/happybase-connection.html
64+
[HappyBase Table]: https://googlecloudplatform.github.io/gcloud-python/stable/happybase-table.html
65+
[HappyBase Table Put]: https://googlecloudplatform.github.io/gcloud-python/stable/happybase-table.html#gcloud.bigtable.happybase.table.Table.put
66+
[HappyBase Table Scan]: https://googlecloudplatform.github.io/gcloud-python/stable/happybase-table.html#gcloud.bigtable.happybase.table.Table.scan
67+

samples/hello_happybase/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+
- Set the GCLOUD_PROJECT environment variable to your project ID.
26+
https://support.google.com/cloud/answer/6158840
27+
"""
28+
29+
import argparse
30+
31+
from gcloud import bigtable
32+
from gcloud.bigtable import happybase
33+
34+
35+
def main(project_id, cluster_id, zone, table_name):
36+
# [START connecting_to_bigtable]
37+
# The client must be created with admin=True because it will create a
38+
# table.
39+
client = bigtable.Client(project=project_id, admin=True)
40+
cluster = client.cluster(zone, cluster_id)
41+
connection = happybase.Connection(cluster=cluster)
42+
# [END connecting_to_bigtable]
43+
44+
try:
45+
# [START creating_a_table]
46+
print('Creating the {} table.'.format(table_name))
47+
column_family_name = 'cf1'
48+
connection.create_table(
49+
table_name,
50+
{
51+
column_family_name: dict() # Use default options.
52+
})
53+
# [END creating_a_table]
54+
55+
# [START writing_rows]
56+
print('Writing some greetings to the table.')
57+
table = connection.table(table_name)
58+
column_name = '{fam}:greeting'.format(fam=column_family_name)
59+
greetings = [
60+
'Hello World!',
61+
'Hello Cloud Bigtable!',
62+
'Hello HappyBase!',
63+
]
64+
for i, value in enumerate(greetings):
65+
# Note: This example uses sequential numeric IDs for simplicity,
66+
# but this can result in poor performance in a production
67+
# application. Since rows are stored in sorted order by key,
68+
# sequential keys can result in poor distribution of operations
69+
# across nodes.
70+
#
71+
# For more information about how to design a Bigtable schema for
72+
# the best performance, see the documentation:
73+
#
74+
# https://cloud.google.com/bigtable/docs/schema-design
75+
row_key = 'greeting{}'.format(i)
76+
table.put(row_key, {column_name: value})
77+
# [END writing_rows]
78+
79+
# [START getting_a_row]
80+
print('Getting a single greeting by row key.')
81+
key = 'greeting0'
82+
row = table.row(key)
83+
print('\t{}: {}'.format(key, row[column_name]))
84+
# [END getting_a_row]
85+
86+
# [START scanning_all_rows]
87+
print('Scanning for all greetings:')
88+
for key, row in table.scan():
89+
print('\t{}: {}'.format(key, row[column_name]))
90+
# [END scanning_all_rows]
91+
92+
# [START deleting_a_table]
93+
print('Deleting the {} table.'.format(table_name))
94+
connection.delete_table(table_name)
95+
# [END deleting_a_table]
96+
finally:
97+
connection.close()
98+
99+
100+
if __name__ == '__main__':
101+
parser = argparse.ArgumentParser(
102+
description='A sample application that connects to Cloud' +
103+
' Bigtable.',
104+
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.')
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)

samples/hello_happybase/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)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
gcloud[grpc]==0.14.0

0 commit comments

Comments
 (0)