GeezerVison is showing github.com — Update Bigtable samples to v2. · fullstackguy/python-docs-samples@a981dcd
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +76-11Lines changed: 76 additions & 11 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | # Cloud Bigtable Hello World | 2 | 2 |
| 3 | 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. | | 4 | +Library][gcloud-python-bigtable] to connect to and interact with Cloud Bigtable. | 5 | 5 |
| 6 | | -[gcloud-python]: https://github.com/GoogleCloudPlatform/gcloud-python | | 6 | +<!-- auto-doc-link --> | | 7 | +These samples are used on the following documentation page: | 7 | 8 |
| | 9 | +> https://cloud.google.com/bigtable/docs/samples-python-hello | 8 | 10 |
| 9 | | -## Provision a cluster | | 11 | +<!-- end-auto-doc-link --> | 10 | 12 |
| 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 | 14 | 15 |
| 15 | 16 |
| 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 | 17 | 73 |
| 18 | 74 | First, set your [Google Application Default Credentials](https://developers.google.com/identity/protocols/application-default-credentials) | 19 | 75 |
| @@ -23,20 +79,29 @@ Install the dependencies with pip. | 23 | 79 | $ pip install -r requirements.txt | 24 | 80 | ``` | 25 | 81 |
| 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. | 27 | 83 |
| 28 | 84 | ``` | 29 | | -$ python main.py my-project my-cluster us-central1-c | | 85 | +$ python main.py my-project my-instance | 30 | 86 | ``` | 31 | 87 |
| 32 | 88 | You will see output resembling the following: | 33 | 89 |
| 34 | 90 | ``` | 35 | | -Create table Hello-Bigtable-1234 | | 91 | +Create table Hello-Bigtable | 36 | 92 | Write some greetings to the table | 37 | 93 | Scan for all greetings: | 38 | 94 | greeting0: Hello World! | 39 | 95 | greeting1: Hello Cloud Bigtable! | 40 | 96 | greeting2: Hello HappyBase! | 41 | | -Delete table Hello-Bigtable-1234 | | 97 | +Delete table Hello-Bigtable | 42 | 98 | ``` | | 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). |
Collapse file Copy file name to clipboardExpand all lines: bigtable/hello/main.py +5-7Lines changed: 5 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 29 | 29 | from gcloud import bigtable | 30 | 30 |
| 31 | 31 |
| 32 | | -def main(project_id, cluster_id, zone, table_id): | | 32 | +def main(project_id, instance_id, table_id): | 33 | 33 | # [START connecting_to_bigtable] | 34 | 34 | # The client must be created with admin=True because it will create a | 35 | 35 | # table. | 36 | 36 | with bigtable.Client(project=project_id, admin=True) as client: | 37 | | - cluster = client.cluster(zone, cluster_id) | | 37 | + instance = client.instance(instance_id) | 38 | 38 | # [END connecting_to_bigtable] | 39 | 39 |
| 40 | 40 | # [START creating_a_table] | 41 | 41 | print('Creating the {} table.'.format(table_id)) | 42 | | - table = cluster.table(table_id) | | 42 | + table = instance.table(table_id) | 43 | 43 | table.create() | 44 | 44 | column_family_id = 'cf1' | 45 | 45 | cf1 = table.column_family(column_family_id) | @@ -107,13 +107,11 @@ def main(project_id, cluster_id, zone, table_id): | 107 | 107 | formatter_class=argparse.ArgumentDefaultsHelpFormatter) | 108 | 108 | parser.add_argument('project_id', help='Your Cloud Platform project ID.') | 109 | 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.') | | 110 | + 'instance_id', help='ID of the Cloud Bigtable instance to connect to.') | 113 | 111 | parser.add_argument( | 114 | 112 | '--table', | 115 | 113 | help='Table to create and destroy.', | 116 | 114 | default='Hello-Bigtable') | 117 | 115 |
| 118 | 116 | 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) |
Collapse file Copy file name to clipboardExpand all lines: bigtable/hello/main_test.py +1-2Lines changed: 1 addition & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -33,8 +33,7 @@ def test_main(cloud_config, capsys): | 33 | 33 | random.randrange(TABLE_NAME_RANGE)) | 34 | 34 | main( | 35 | 35 | cloud_config.project, | 36 | | - cloud_config.bigtable_cluster, | 37 | | - cloud_config.bigtable_zone, | | 36 | + cloud_config.bigtable_instance, | 38 | 37 | table_name) | 39 | 38 |
| 40 | 39 | out, _ = capsys.readouterr() | |
Collapse file Copy file name to clipboard +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -gcloud[grpc]==0.16.0 | | 1 | +gcloud[grpc]==0.17.0 |
Collapse file Copy file name to clipboard +75-33Lines changed: 75 additions & 33 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -# Cloud Bigtable Hello World (HappyBase) | | 1 | +# Cloud Bigtable Hello World via the HappyBase API | 2 | 2 |
| 3 | 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. | | 4 | +Library HappyBase package][gcloud-python-happybase], an implementation of the [HappyBase | | 5 | +API][happybase] to connect to and interact with Cloud Bigtable. | 5 | 6 |
| 6 | | -[gcloud-python]: https://github.com/GoogleCloudPlatform/gcloud-python | | 7 | +<!-- auto-doc-link --> | | 8 | +These samples are used on the following documentation page: | 7 | 9 |
| | 10 | +> https://cloud.google.com/bigtable/docs/samples-python-hello-happybase | 8 | 11 |
| 9 | | -## Provision a cluster | | 12 | +<!-- end-auto-doc-link --> | 10 | 13 |
| 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 | 14 | 17 |
| 15 | 18 |
| 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 | 17 | 75 |
| 18 | 76 | First, set your [Google Application Default Credentials](https://developers.google.com/identity/protocols/application-default-credentials) | 19 | 77 |
| @@ -23,45 +81,29 @@ Install the dependencies with pip. | 23 | 81 | $ pip install -r requirements.txt | 24 | 82 | ``` | 25 | 83 |
| 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. | 27 | 85 |
| 28 | 86 | ``` | 29 | | -$ python main.py my-project my-cluster us-central1-c | | 87 | +$ python main.py my-project my-instance | 30 | 88 | ``` | 31 | 89 |
| 32 | 90 | You will see output resembling the following: | 33 | 91 |
| 34 | 92 | ``` | 35 | | -Create table Hello-Bigtable-1234 | | 93 | +Create table Hello-Bigtable | 36 | 94 | Write some greetings to the table | 37 | 95 | Scan for all greetings: | 38 | 96 | greeting0: Hello World! | 39 | 97 | greeting1: Hello Cloud Bigtable! | 40 | 98 | greeting2: Hello HappyBase! | 41 | | -Delete table Hello-Bigtable-1234 | | 99 | +Delete table Hello-Bigtable | 42 | 100 | ``` | 43 | 101 |
| 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 | 102 |
| 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 | 61 | 104 |
| 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. | 67 | 107 |
| | 108 | +- [Delete the Cloud Bigtable | | 109 | + instance](https://cloud.google.com/bigtable/docs/deleting-instance). |
Collapse file Copy file name to clipboardExpand all lines: bigtable/hello_happybase/main.py +5-7Lines changed: 5 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 30 | 30 | from gcloud.bigtable import happybase | 31 | 31 |
| 32 | 32 |
| 33 | | -def main(project_id, cluster_id, zone, table_name): | | 33 | +def main(project_id, instance_id, table_name): | 34 | 34 | # [START connecting_to_bigtable] | 35 | 35 | # The client must be created with admin=True because it will create a | 36 | 36 | # table. | 37 | 37 | 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) | 40 | 40 | # [END connecting_to_bigtable] | 41 | 41 |
| 42 | 42 | try: | @@ -104,13 +104,11 @@ def main(project_id, cluster_id, zone, table_name): | 104 | 104 | formatter_class=argparse.ArgumentDefaultsHelpFormatter) | 105 | 105 | parser.add_argument('project_id', help='Your Cloud Platform project ID.') | 106 | 106 | 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.') | 110 | 108 | parser.add_argument( | 111 | 109 | '--table', | 112 | 110 | help='Table to create and destroy.', | 113 | 111 | default='Hello-Bigtable') | 114 | 112 |
| 115 | 113 | 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) |
Collapse file Copy file name to clipboardExpand all lines: bigtable/hello_happybase/main_test.py +1-2Lines changed: 1 addition & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -33,8 +33,7 @@ def test_main(cloud_config, capsys): | 33 | 33 | random.randrange(TABLE_NAME_RANGE)) | 34 | 34 | main( | 35 | 35 | cloud_config.project, | 36 | | - cloud_config.bigtable_cluster, | 37 | | - cloud_config.bigtable_zone, | | 36 | + cloud_config.bigtable_instance, | 38 | 37 | table_name) | 39 | 38 |
| 40 | 39 | out, _ = capsys.readouterr() | |
Collapse file Copy file name to clipboard +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -gcloud[grpc]==0.16.0 | | 1 | +gcloud[grpc]==0.17.0 |
Collapse file Copy file name to clipboardExpand all lines: conftest.py +1-2Lines changed: 1 addition & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -29,8 +29,7 @@ def cloud_config(): | 29 | 29 | project=os.environ.get('GCLOUD_PROJECT'), | 30 | 30 | storage_bucket=os.environ.get('CLOUD_STORAGE_BUCKET'), | 31 | 31 | 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')) | 34 | 33 |
| 35 | 34 |
| 36 | 35 | def get_resource_path(resource, local_path): | |
Collapse file Copy file name to clipboardExpand all lines: scripts/prepare-testing-project.sh +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -44,4 +44,4 @@ echo "Creating pubsub resources." | 44 | 44 | gcloud alpha pubsub topics create gae-mvm-pubsub-topic | 45 | 45 |
| 46 | 46 | 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" |
|