Skip to content

bigtable: add raw gcloud-python hello sample. #384

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions bigtable/hello/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Cloud Bigtable Hello World

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

[gcloud-python]: https://github.com/GoogleCloudPlatform/gcloud-python


## Provision a cluster
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instance


Follow the instructions in the [user documentation](https://cloud.google.com/bigtable/docs/creating-cluster)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

creating-instance

to create a Google Cloud Platform project and Cloud Bigtable cluster if necessary.
You'll need to reference your project ID, zone and cluster ID to run the application.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instance ID



## Run the application

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

Install the dependencies with pip.

```
$ pip install -r requirements.txt
```

Run the application. Replace the command-line parameters with values for your cluster.

```
$ python main.py my-project my-cluster us-central1-c
```

You will see output resembling the following:

```
Create table Hello-Bigtable-1234
Write some greetings to the table
Scan for all greetings:
greeting0: Hello World!
greeting1: Hello Cloud Bigtable!
greeting2: Hello HappyBase!
Delete table Hello-Bigtable-1234
```
119 changes: 119 additions & 0 deletions bigtable/hello/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#!/usr/bin/env python

# Copyright 2016 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Demonstrates how to connect to Cloud Bigtable and run some basic operations.

Prerequisites:

- Create a Cloud Bigtable cluster.
https://cloud.google.com/bigtable/docs/creating-cluster
- Set your Google Application Default Credentials.
https://developers.google.com/identity/protocols/application-default-credentials
"""

import argparse

from gcloud import bigtable


def main(project_id, cluster_id, zone, table_id):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not saying this needs to block the merge, but I think it's better to separate these actions into separate functions. 1) because smaller methods are better 2) because it makes it easier to define a CLI , jon suggested it for the logging samples which you can see here, which to me makes the sample more interactive/usable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I the sound of that. Right now the sample is kind of boring, though it does show the basic operations.

There is a Go command-line program for Bigtable, https://godoc.org/google.golang.org/cloud/bigtable/cmd/cbt, though that covers more operations than are probably necessary for a "hello world" app, but I do think it makes sense to do something similar here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another benefit (or maybe downside?) of splitting it into functions and adding a command-line interface is it would almost force us into handling errors properly.

For example, I know an exception is thrown if the table already exists, but with putting everything in main() I thought it would clutter it up too much to catch that exception. Having it in a function can make it easier to show how to handle those errors and explain them in the docs.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably okay for a hello world app, you can write a separate crud sample if you really want to.

I'm ambivalent about error handling - I think as long as our samples don't silence errors and propagate them to the user then that is enough for a sample.

# [START connecting_to_bigtable]
# The client must be created with admin=True because it will create a
# table.
with bigtable.Client(project=project_id, admin=True) as client:
cluster = client.cluster(zone, cluster_id)
# [END connecting_to_bigtable]

# [START creating_a_table]
print('Creating the {} table.'.format(table_id))
table = cluster.table(table_id)
table.create()
column_family_id = 'cf1'
cf1 = table.column_family(column_family_id)
cf1.create()
# [END creating_a_table]

# [START writing_rows]
print('Writing some greetings to the table.')
column_id = 'greeting'.encode('utf-8')
greetings = [
'Hello World!',
'Hello Cloud Bigtable!',
'Hello Python!',
]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extra newline between expressions and control statements, please.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

I hadn't heard that rule before. Seems like this check should be automated and made part of the linter.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/shruggie

It's more a personal nit of mine than something that's been codified.


for i, value in enumerate(greetings):
# Note: This example uses sequential numeric IDs for simplicity,
# but this can result in poor performance in a production
# application. Since rows are stored in sorted order by key,
# sequential keys can result in poor distribution of operations
# across nodes.
#
# For more information about how to design a Bigtable schema for
# the best performance, see the documentation:
#
# https://cloud.google.com/bigtable/docs/schema-design
row_key = 'greeting{}'.format(i)
row = table.row(row_key)
row.set_cell(
column_family_id,
column_id.encode('utf-8'),
value.encode('utf-8'))
row.commit()
# [END writing_rows]

# [START getting_a_row]
print('Getting a single greeting by row key.')
key = 'greeting0'
row = table.read_row(key.encode('utf-8'))
value = row.cells[column_family_id][column_id.encode('utf-8')][0].value
print('\t{}: {}'.format(key, value.decode('utf-8')))
# [END getting_a_row]

# [START scanning_all_rows]
print('Scanning for all greetings:')
partial_rows = table.read_rows()
partial_rows.consume_all()

for row_key, row in partial_rows.rows.items():
key = row_key.decode('utf-8')
cell = row.cells[column_family_id][column_id.encode('utf-8')][0]
value = cell.value.decode('utf-8')
print('\t{}: {}'.format(key, value))
# [END scanning_all_rows]

# [START deleting_a_table]
print('Deleting the {} table.'.format(table_id))
table.delete()
# [END deleting_a_table]


if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('project_id', help='Your Cloud Platform project ID.')
parser.add_argument(
'cluster', help='ID of the Cloud Bigtable cluster to connect to.')
parser.add_argument(
'zone', help='Zone that contains the Cloud Bigtable cluster.')
parser.add_argument(
'--table',
help='Table to create and destroy.',
default='Hello-Bigtable')

args = parser.parse_args()
main(args.project_id, args.cluster, args.zone, args.table)
49 changes: 49 additions & 0 deletions bigtable/hello/main_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Copyright 2016 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import random
import re
import sys

from main import main

import pytest

TABLE_NAME_FORMAT = 'Hello-Bigtable-{}'
TABLE_NAME_RANGE = 10000


@pytest.mark.skipif(
sys.version_info >= (3, 0),
reason=("grpc doesn't yet support python3 "
'https://github.com/grpc/grpc/issues/282'))
def test_main(cloud_config, capsys):
table_name = TABLE_NAME_FORMAT.format(
random.randrange(TABLE_NAME_RANGE))
main(
cloud_config.project,
cloud_config.bigtable_cluster,
cloud_config.bigtable_zone,
table_name)

out, _ = capsys.readouterr()
assert re.search(
re.compile(r'Creating the Hello-Bigtable-[0-9]+ table\.'), out)
assert re.search(re.compile(r'Writing some greetings to the table\.'), out)
assert re.search(re.compile(r'Getting a single greeting by row key.'), out)
assert re.search(re.compile(r'greeting0: Hello World!'), out)
assert re.search(re.compile(r'Scanning for all greetings'), out)
assert re.search(re.compile(r'greeting1: Hello Cloud Bigtable!'), out)
assert re.search(
re.compile(r'Deleting the Hello-Bigtable-[0-9]+ table\.'), out)
1 change: 1 addition & 0 deletions bigtable/hello/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gcloud[grpc]==0.16.0
2 changes: 1 addition & 1 deletion bigtable/hello_happybase/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Cloud Bigtable Hello World
# Cloud Bigtable Hello World (HappyBase)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems a bit lite on content.


This is a simple application that demonstrates using the [Google Cloud Client
Library][gcloud-python] to connect to and interact with Cloud Bigtable.
Expand Down
13 changes: 5 additions & 8 deletions bigtable/hello_happybase/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
https://cloud.google.com/bigtable/docs/creating-cluster
- Set your Google Application Default Credentials.
https://developers.google.com/identity/protocols/application-default-credentials
- Set the GCLOUD_PROJECT environment variable to your project ID.
https://support.google.com/cloud/answer/6158840
"""

import argparse
Expand Down Expand Up @@ -61,6 +59,7 @@ def main(project_id, cluster_id, zone, table_name):
'Hello Cloud Bigtable!',
'Hello HappyBase!',
]

for i, value in enumerate(greetings):
# Note: This example uses sequential numeric IDs for simplicity,
# but this can result in poor performance in a production
Expand All @@ -85,6 +84,7 @@ def main(project_id, cluster_id, zone, table_name):

# [START scanning_all_rows]
print('Scanning for all greetings:')

for key, row in table.scan():
print('\t{}: {}'.format(key, row[column_name]))
# [END scanning_all_rows]
Expand All @@ -93,19 +93,16 @@ def main(project_id, cluster_id, zone, table_name):
print('Deleting the {} table.'.format(table_name))
connection.delete_table(table_name)
# [END deleting_a_table]

finally:
connection.close()


if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='A sample application that connects to Cloud' +
' Bigtable.',
description=__doc__,
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument(
'project_id',
help='Google Cloud Platform project ID that contains the Cloud' +
' Bigtable cluster.')
parser.add_argument('project_id', help='Your Cloud Platform project ID.')
parser.add_argument(
'cluster', help='ID of the Cloud Bigtable cluster to connect to.')
parser.add_argument(
Expand Down
2 changes: 1 addition & 1 deletion bigtable/hello_happybase/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
gcloud[grpc]==0.14.0
gcloud[grpc]==0.16.0