Skip to content

Commit 2fa20c0

Browse files
authored
Merge pull request #392 from GoogleCloudPlatform/tswast-bigtable-v2
Update Bigtable samples to v2.
2 parents 56466d1 + a981dcd commit 2fa20c0

File tree

11 files changed

+174
-68
lines changed

11 files changed

+174
-68
lines changed

bigtable/hello/README.md

Lines changed: 76 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,75 @@
11
# Cloud Bigtable Hello World
22

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

6-
[gcloud-python]: https://github.com/GoogleCloudPlatform/gcloud-python
6+
<!-- auto-doc-link -->
7+
These samples are used on the following documentation page:
78

9+
> https://cloud.google.com/bigtable/docs/samples-python-hello
810
9-
## Provision a cluster
11+
<!-- end-auto-doc-link -->
1012

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.
13+
[gcloud-python-bigtable]: https://googlecloudplatform.github.io/gcloud-python/stable/bigtable-usage.html
14+
[sample-docs]: https://cloud.google.com/bigtable/docs/samples-python-hello
1415

1516

16-
## Run the application
17+
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
18+
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
19+
**Table of Contents**
20+
21+
- [Downloading the sample](#downloading-the-sample)
22+
- [Costs](#costs)
23+
- [Provisioning an instance](#provisioning-an-instance)
24+
- [Running the application](#running-the-application)
25+
- [Cleaning up](#cleaning-up)
26+
27+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
28+
29+
30+
## Downloading the sample
31+
32+
Download the sample app and navigate into the app directory:
33+
34+
1. Clone the [Python samples
35+
repository](https://github.com/GoogleCloudPlatform/python-docs-samples), to
36+
your local machine:
37+
38+
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
39+
40+
Alternatively, you can [download the
41+
sample](https://github.com/GoogleCloudPlatform/python-docs-samples/archive/master.zip)
42+
as a zip file and extract it.
43+
44+
2. Change to the sample directory.
45+
46+
cd python-docs-samples/bigtable/hello
47+
48+
49+
## Costs
50+
51+
This sample uses billable components of Cloud Platform, including:
52+
53+
+ Google Cloud Bigtable
54+
55+
Use the [Pricing Calculator][bigtable-pricing] to generate a cost estimate
56+
based on your projected usage. New Cloud Platform users might be eligible for
57+
a [free trial][free-trial].
58+
59+
[bigtable-pricing]: https://cloud.google.com/products/calculator/#id=1eb47664-13a2-4be1-9d16-6722902a7572
60+
[free-trial]: https://cloud.google.com/free-trial
61+
62+
63+
## Provisioning an instance
64+
65+
Follow the instructions in the [user
66+
documentation](https://cloud.google.com/bigtable/docs/creating-instance) to
67+
create a Google Cloud Platform project and Cloud Bigtable instance if necessary.
68+
You'll need to reference your project id and instance id to run the
69+
application.
70+
71+
72+
## Running the application
1773

1874
First, set your [Google Application Default Credentials](https://developers.google.com/identity/protocols/application-default-credentials)
1975

@@ -23,20 +79,29 @@ Install the dependencies with pip.
2379
$ pip install -r requirements.txt
2480
```
2581

26-
Run the application. Replace the command-line parameters with values for your cluster.
82+
Run the application. Replace the command-line parameters with values for your instance.
2783

2884
```
29-
$ python main.py my-project my-cluster us-central1-c
85+
$ python main.py my-project my-instance
3086
```
3187

3288
You will see output resembling the following:
3389

3490
```
35-
Create table Hello-Bigtable-1234
91+
Create table Hello-Bigtable
3692
Write some greetings to the table
3793
Scan for all greetings:
3894
greeting0: Hello World!
3995
greeting1: Hello Cloud Bigtable!
4096
greeting2: Hello HappyBase!
41-
Delete table Hello-Bigtable-1234
97+
Delete table Hello-Bigtable
4298
```
99+
100+
101+
## Cleaning up
102+
103+
To avoid incurring extra charges to your Google Cloud Platform account, remove
104+
the resources created for this sample.
105+
106+
- [Delete the Cloud Bigtable
107+
instance](https://cloud.google.com/bigtable/docs/deleting-instance).

bigtable/hello/main.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,17 @@
2929
from gcloud import bigtable
3030

3131

32-
def main(project_id, cluster_id, zone, table_id):
32+
def main(project_id, instance_id, table_id):
3333
# [START connecting_to_bigtable]
3434
# The client must be created with admin=True because it will create a
3535
# table.
3636
with bigtable.Client(project=project_id, admin=True) as client:
37-
cluster = client.cluster(zone, cluster_id)
37+
instance = client.instance(instance_id)
3838
# [END connecting_to_bigtable]
3939

4040
# [START creating_a_table]
4141
print('Creating the {} table.'.format(table_id))
42-
table = cluster.table(table_id)
42+
table = instance.table(table_id)
4343
table.create()
4444
column_family_id = 'cf1'
4545
cf1 = table.column_family(column_family_id)
@@ -107,13 +107,11 @@ def main(project_id, cluster_id, zone, table_id):
107107
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
108108
parser.add_argument('project_id', help='Your Cloud Platform project ID.')
109109
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.')
110+
'instance_id', help='ID of the Cloud Bigtable instance to connect to.')
113111
parser.add_argument(
114112
'--table',
115113
help='Table to create and destroy.',
116114
default='Hello-Bigtable')
117115

118116
args = parser.parse_args()
119-
main(args.project_id, args.cluster, args.zone, args.table)
117+
main(args.project_id, args.instance_id, args.table)

bigtable/hello/main_test.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ def test_main(cloud_config, capsys):
3333
random.randrange(TABLE_NAME_RANGE))
3434
main(
3535
cloud_config.project,
36-
cloud_config.bigtable_cluster,
37-
cloud_config.bigtable_zone,
36+
cloud_config.bigtable_instance,
3837
table_name)
3938

4039
out, _ = capsys.readouterr()

bigtable/hello/requirements.txt

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

bigtable/hello_happybase/README.md

Lines changed: 75 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,77 @@
1-
# Cloud Bigtable Hello World (HappyBase)
1+
# Cloud Bigtable Hello World via the HappyBase API
22

33
This is a simple application that demonstrates using the [Google Cloud Client
4-
Library][gcloud-python] to connect to and interact with Cloud Bigtable.
4+
Library HappyBase package][gcloud-python-happybase], an implementation of the [HappyBase
5+
API][happybase] to connect to and interact with Cloud Bigtable.
56

6-
[gcloud-python]: https://github.com/GoogleCloudPlatform/gcloud-python
7+
<!-- auto-doc-link -->
8+
These samples are used on the following documentation page:
79

10+
> https://cloud.google.com/bigtable/docs/samples-python-hello-happybase
811
9-
## Provision a cluster
12+
<!-- end-auto-doc-link -->
1013

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+
[gcloud-python-happybase]: https://googlecloudplatform.github.io/gcloud-python/stable/happybase-package.html
15+
[happybase]: http://happybase.readthedocs.io/en/stable/
16+
[sample-docs]: https://cloud.google.com/bigtable/docs/samples-python-hello-happybase
1417

1518

16-
## Run the application
19+
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
20+
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
21+
**Table of Contents**
22+
23+
- [Downloading the sample](#downloading-the-sample)
24+
- [Costs](#costs)
25+
- [Provisioning an instance](#provisioning-an-instance)
26+
- [Running the application](#running-the-application)
27+
- [Cleaning up](#cleaning-up)
28+
29+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
30+
31+
32+
## Downloading the sample
33+
34+
Download the sample app and navigate into the app directory:
35+
36+
1. Clone the [Python samples
37+
repository](https://github.com/GoogleCloudPlatform/python-docs-samples), to
38+
your local machine:
39+
40+
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
41+
42+
Alternatively, you can [download the
43+
sample](https://github.com/GoogleCloudPlatform/python-docs-samples/archive/master.zip)
44+
as a zip file and extract it.
45+
46+
2. Change to the sample directory.
47+
48+
cd python-docs-samples/bigtable/hello_happybase
49+
50+
51+
## Costs
52+
53+
This sample uses billable components of Cloud Platform, including:
54+
55+
+ Google Cloud Bigtable
56+
57+
Use the [Pricing Calculator][bigtable-pricing] to generate a cost estimate
58+
based on your projected usage. New Cloud Platform users might be eligible for
59+
a [free trial][free-trial].
60+
61+
[bigtable-pricing]: https://cloud.google.com/products/calculator/#id=1eb47664-13a2-4be1-9d16-6722902a7572
62+
[free-trial]: https://cloud.google.com/free-trial
63+
64+
65+
## Provisioning an instance
66+
67+
Follow the instructions in the [user
68+
documentation](https://cloud.google.com/bigtable/docs/creating-instance) to
69+
create a Google Cloud Platform project and Cloud Bigtable instance if necessary.
70+
You'll need to reference your project id and instance id to run the
71+
application.
72+
73+
74+
## Running the application
1775

1876
First, set your [Google Application Default Credentials](https://developers.google.com/identity/protocols/application-default-credentials)
1977

@@ -23,45 +81,29 @@ Install the dependencies with pip.
2381
$ pip install -r requirements.txt
2482
```
2583

26-
Run the application. Replace the command-line parameters with values for your cluster.
84+
Run the application. Replace the command-line parameters with values for your instance.
2785

2886
```
29-
$ python main.py my-project my-cluster us-central1-c
87+
$ python main.py my-project my-instance
3088
```
3189

3290
You will see output resembling the following:
3391

3492
```
35-
Create table Hello-Bigtable-1234
93+
Create table Hello-Bigtable
3694
Write some greetings to the table
3795
Scan for all greetings:
3896
greeting0: Hello World!
3997
greeting1: Hello Cloud Bigtable!
4098
greeting2: Hello HappyBase!
41-
Delete table Hello-Bigtable-1234
99+
Delete table Hello-Bigtable
42100
```
43101

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
53102

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].
103+
## Cleaning up
61104

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
105+
To avoid incurring extra charges to your Google Cloud Platform account, remove
106+
the resources created for this sample.
67107

108+
- [Delete the Cloud Bigtable
109+
instance](https://cloud.google.com/bigtable/docs/deleting-instance).

bigtable/hello_happybase/main.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@
3030
from gcloud.bigtable import happybase
3131

3232

33-
def main(project_id, cluster_id, zone, table_name):
33+
def main(project_id, instance_id, table_name):
3434
# [START connecting_to_bigtable]
3535
# The client must be created with admin=True because it will create a
3636
# table.
3737
client = bigtable.Client(project=project_id, admin=True)
38-
cluster = client.cluster(zone, cluster_id)
39-
connection = happybase.Connection(cluster=cluster)
38+
instance = client.instance(instance_id)
39+
connection = happybase.Connection(instance=instance)
4040
# [END connecting_to_bigtable]
4141

4242
try:
@@ -104,13 +104,11 @@ def main(project_id, cluster_id, zone, table_name):
104104
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
105105
parser.add_argument('project_id', help='Your Cloud Platform project ID.')
106106
parser.add_argument(
107-
'cluster', help='ID of the Cloud Bigtable cluster to connect to.')
108-
parser.add_argument(
109-
'zone', help='Zone that contains the Cloud Bigtable cluster.')
107+
'instance_id', help='ID of the Cloud Bigtable instance to connect to.')
110108
parser.add_argument(
111109
'--table',
112110
help='Table to create and destroy.',
113111
default='Hello-Bigtable')
114112

115113
args = parser.parse_args()
116-
main(args.project_id, args.cluster, args.zone, args.table)
114+
main(args.project_id, args.instance_id, args.table)

bigtable/hello_happybase/main_test.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ def test_main(cloud_config, capsys):
3333
random.randrange(TABLE_NAME_RANGE))
3434
main(
3535
cloud_config.project,
36-
cloud_config.bigtable_cluster,
37-
cloud_config.bigtable_zone,
36+
cloud_config.bigtable_instance,
3837
table_name)
3938

4039
out, _ = capsys.readouterr()
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
gcloud[grpc]==0.16.0
1+
gcloud[grpc]==0.17.0

conftest.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ def cloud_config():
2929
project=os.environ.get('GCLOUD_PROJECT'),
3030
storage_bucket=os.environ.get('CLOUD_STORAGE_BUCKET'),
3131
client_secrets=os.environ.get('GOOGLE_CLIENT_SECRETS'),
32-
bigtable_cluster=os.environ.get('BIGTABLE_CLUSTER'),
33-
bigtable_zone=os.environ.get('BIGTABLE_ZONE'))
32+
bigtable_instance=os.environ.get('BIGTABLE_CLUSTER'))
3433

3534

3635
def get_resource_path(resource, local_path):

scripts/prepare-testing-project.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@ echo "Creating pubsub resources."
4444
gcloud alpha pubsub topics create gae-mvm-pubsub-topic
4545

4646
echo "To finish setup, follow this link to enable APIs."
47-
echo "https://console.cloud.google.com/flows/enableapi?project=${GCLOUD_PROJECT}&apiid=bigtable,bigtableclusteradmin,bigtabletableadmin,bigquery,cloudmonitoring,compute_component,datastore,datastore.googleapis.com,dataproc,dns,plus,pubsub,logging,storage_api,vision.googleapis.com"
47+
echo "https://console.cloud.google.com/flows/enableapi?project=${GCLOUD_PROJECT}&apiid=bigtable.googleapis.com,bigtableadmin.googleapis.com,bigquery,cloudmonitoring,compute_component,datastore,datastore.googleapis.com,dataproc,dns,plus,pubsub,logging,storage_api,vision.googleapis.com"

scripts/resources/docs-links.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,5 +364,11 @@
364364
"/appengine/docs/python/memcache/examples": [
365365
"appengine/memcache/snippets/snippets.py",
366366
"appengine/memcache/guestbook/main.py"
367+
],
368+
"/bigtable/docs/samples-python-hello": [
369+
"bigtable/hello/main.py"
370+
],
371+
"/bigtable/docs/samples-python-hello-happybase": [
372+
"bigtable/hello_happybase/main.py"
367373
]
368-
}
374+
}

0 commit comments

Comments
 (0)