-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
||
Follow the instructions in the [user documentation](https://cloud.google.com/bigtable/docs/creating-cluster) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
``` |
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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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!', | ||
] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. extra newline between expressions and control statements, please. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
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) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
gcloud[grpc]==0.16.0 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# Cloud Bigtable Hello World | ||
# Cloud Bigtable Hello World (HappyBase) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
gcloud[grpc]==0.14.0 | ||
gcloud[grpc]==0.16.0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instance