8000 Cloud Bigtable Quickstarts (#1616) · lesliepound/python-docs-samples@c7821fb · GitHub
[go: up one dir, main page]

Skip to content

Commit c7821fb

Browse files
Cloud Bigtable Quickstarts (GoogleCloudPlatform#1616)
* quickstart * quickstart happybase * linting and making tests workˆ * Tidying up * Trigger * Fixes for Python3 * Showing default values for the quickstart functions * Fix lint issue with indentation
1 parent 277d267 commit c7821fb

File tree

10 files changed

+440
-0
lines changed

10 files changed

+440
-0
lines changed

bigtable/quickstart/README.rst

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
.. This file is automatically generated. Do not edit this file directly.
2+
3+
Google Cloud Bigtable Python Samples
4+
===============================================================================
5+
6+
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
7+
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=bigtable/quickstart/README.rst
8+
9+
10+
This directory contains samples for Google Cloud Bigtable. `Google Cloud Bigtable`_ is Google's NoSQL Big Data database service. It's the same database that powers many core Google services, including Search, Analytics, Maps, and Gmail.
11+
12+
13+
14+
15+
.. _Google Cloud Bigtable: https://cloud.google.com/bigtable/docs
16+
17+
Setup
18+
-------------------------------------------------------------------------------
19+
20+
21+
Authentication
22+
++++++++++++++
23+
24+
This sample requires you to have authentication setup. Refer to the
25+
`Authentication Getting Started Guide`_ for instructions on setting up
26+
credentials for applications.
27+
28+
.. _Authentication Getting Started Guide:
29+
https://cloud.google.com/docs/authentication/getting-started
30+
31+
Install Dependencies
32+
++++++++++++++++++++
33+
34+
#. Clone python-docs-samples and change directory to the sample directory you want to use.
35+
36+
.. code-block:: bash
37+
38+
$ git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
39+
40+
#. Install `pip`_ and `virtualenv`_ if you do not already have them. You may want to refer to the `Python Development Environment Setup Guide`_ for Google Cloud Platform for instructions.
41+
42+
.. _Python Development Environment Setup Guide:
43+
https://cloud.google.com/python/setup
44+
45+
#. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+.
46+
47+
.. code-block:: bash
48+
49+
$ virtualenv env
50+
$ source env/bin/activate
51+
52+
#. Install the dependencies needed to run the samples.
53+
54+
.. code-block:: bash
55+
56+
$ pip install -r requirements.txt
57+
58+
.. _pip: https://pip.pypa.io/
59+
.. _virtualenv: https://virtualenv.pypa.io/
60+
61+
Samples
62+
-------------------------------------------------------------------------------
63+
64+
Quickstart
65+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
66+
67+
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
68+
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=bigtable/quickstart/main.py,bigtable/quickstart/README.rst
69+
70+
71+
72+
73+
To run this sample:
74+
75+
.. code-block:: bash
76+
77+
$ python main.py
78+
79+
usage: main.py [-h] [--table TABLE] project_id instance_id
80+
81+
positional arguments:
82+
project_id Your Cloud Platform project ID.
83+
instance_id ID of the Cloud Bigtable instance to connect to.
84+
85+
optional arguments:
86+
-h, --help show this help message and exit
87+
--table TABLE Existing table used in the quickstart. (default: my-table)
88+
89+
90+
91+
92+
93+
The client library
94+
-------------------------------------------------------------------------------
95+
96+
This sample uses the `Google Cloud Client Library for Python`_.
97+
You can read the documentation for more details on API usage and use GitHub
98+
to `browse the source`_ and `report issues`_.
99+
100+
.. _Google Cloud Client Library for Python:
101+
https://googlecloudplatform.github.io/google-cloud-python/
102+
.. _browse the source:
103+
https://github.com/GoogleCloudPlatform/google-cloud-python
104+
.. _report issues:
105+
https://github.com/GoogleCloudPlatform/google-cloud-python/issues
106+
107+
108+
.. _Google Cloud SDK: https://cloud.google.com/sdk/

bigtable/quickstart/README.rst.in

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# This file is used to generate README.rst
2+
3+
product:
4+
name: Google Cloud Bigtable
5+
short_name: Cloud Bigtable
6+
url: https://cloud.google.com/bigtable/docs
7+
description: >
8+
`Google Cloud Bigtable`_ is Google's NoSQL Big Data database service. It's
9+
the same database that powers many core Google services, including Search,
10+
Analytics, Maps, and Gmail.
11+
12+
setup:
13+
- auth
14+
- install_deps
15+
16+
samples:
17+
- name: Quickstart
18+
file: main.py
19+
show_help: true
20+
21+
cloud_client_library: true
22+
23+
folder: bigtable/quickstart

bigtable/quickstart/main.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2018 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+
# [START bigtable_quickstart]
17+
import argparse
18+
19+
from google.cloud import bigtable
20+
21+
22+
def main(project_id="project-id", instance_id="instance-id",
23+
table_id="my-table"):
24+
# Creates a Bigtable client
25+
client = bigtable.Client(project=project_id)
26+
27+
# Connect to an existing instance:my-bigtable-instance
28+
instance = client.instance(instance_id)
29+
30+
# Connect to an existing table:my-table
31+
table = instance.table(table_id)
32+
33+
key = 'r1'
34+
row = table.read_row(key.encode('utf-8'))
35+
36+
column_family_id = 'cf1'
37+
column_id = 'c1'.encode('utf-8')
38+
value = row.cells[column_family_id][column_id][0].value.decode('utf-8')
39+
40+
print('Row key: {}\nData: {}'.format(key, value))
41+
42+
43+
if __name__ == '__main__':
44+
parser = argparse.ArgumentParser(
45+
description=__doc__,
46+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
47+
parser.add_argument('project_id', help='Your Cloud Platform project ID.')
48+
parser.add_argument(
49+
'instance_id', help='ID of the Cloud Bigtable instance to connect to.')
50+
parser.add_argument(
51+
'--table',
52+
help='Existing table used in the quickstart.',
53+
default='my-table')
54+
55+
args = parser.parse_args()
56+
main(args.project_id, args.instance_id, args.table)
57+
# [END bigtable_quickstart]

bigtable/quickstart/main_test.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Copyright 2018 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 os
16+
17+
from main import main
18+
19+
PROJECT = os.environ['GCLOUD_PROJECT']
20+
BIGTABLE_CLUSTER = os.environ['BIGTABLE_CLUSTER']
21+
TABLE_NAME = 'my-table'
22+
23+
24+
def test_main(capsys):
25+
main(PROJECT, BIGTABLE_CLUSTER, TABLE_NAME)
26+
27+
out, _ = capsys.readouterr()
28+
assert 'Row key: r1\nData: test-value\n' in out

bigtable/quickstart/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
google-cloud-bigtable==0.29.0
2+
google-cloud-core==0.28.1
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
.. This file is automatically generated. Do not edit this file directly.
2+
3+
Google Cloud Bigtable Python Samples
4+
===============================================================================
5+
6+
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
7+
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=bigtable/quickstart_happybase/README.rst
8+
9+
10+
This directory contains samples for Google Cloud Bigtable. `Google Cloud Bigtable`_ is Google's NoSQL Big Data database service. It's the same database that powers many core Google services, including Search, Analytics, Maps, and Gmail.
11+
12+
13+
14+
15+
.. _Google Cloud Bigtable: https://cloud.google.com/bigtable/docs
16+
17+
Setup
18+
-------------------------------------------------------------------------------
19+
20+
21+
Authentication
22+
++++++++++++++
23+
24+
This sample requires you to have authentication setup. Refer to the
25+
`Authentication Getting Started Guide`_ for instructions on setting up
26+
credentials for applications.
27+
28+
.. _Authentication Getting Started Guide:
29+
https://cloud.google.com/docs/authentication/getting-started
30+
31+
Install Dependencies
32+
++++++++++++++++++++
33+
34+
#. Clone python-docs-samples and change directory to the sample directory you want to use.
35+
36+
.. code-block:: bash
37+
38+
$ git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
39+
40+
#. Install `pip`_ and `virtualenv`_ if you do not already have them. You may want to refer to the `Python Development Environment Setup Guide`_ for Google Cloud Platform for instructions.
41+
42+
.. _Python Development Environment Setup Guide:
43+
https://cloud.google.com/python/setup
44+
45+
#. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+.
46+
47+
.. code-block:: bash
48+
49+
$ virtualenv env
50+
$ source env/bin/activate
51+
52+
#. Install the dependencies needed to run the samples.
53+
54+
.. code-block:: bash
55+
56+
$ pip install -r requirements.txt
57+
58+
.. _pip: https://pip.pypa.io/
59+
.. _virtualenv: https://virtualenv.pypa.io/
60+
61+
Samples
62+
-------------------------------------------------------------------------------
63+
64+
Quickstart
65+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
66+
67+
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
68+
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=bigtable/quickstart_happybase/main.py,bigtable/quickstart_happybase/README.rst
69+
70+
71+
72+
73+
To run this sample:
74+
75+
.. code-block:: bash
76+
77+
$ python main.py
78+
79+
usage: main.py [-h] [--table TABLE] project_id instance_id
80+
81+
positional arguments:
82+
project_id Your Cloud Platform project ID.
83+
instance_id ID of the Cloud Bigtable instance to connect to.
84+
85+
optional arguments:
86+
-h, --help show this help message and exit
87+
--table TABLE Existing table used in the quickstart. (default: my-table)
88+
89+
90+
91+
92+
93+
The client library
94+
-------------------------------------------------------------------------------
95+
96+
This sample uses the `Google Cloud Client Library for Python`_.
97+
You can read the documentation for more details on API usage and use GitHub
98+
to `browse the source`_ and `report issues`_.
99+
100+
.. _Google Cloud Client Library for Python:
101+
https://googlecloudplatform.github.io/google-cloud-python/
102+
.. _browse the source:
103+
https://github.com/GoogleCloudPlatform/google-cloud-python
104+
.. _report issues:
105+
https://github.com/GoogleCloudPlatform/google-cloud-python/issues
106+
107+
108+
.. _Google Cloud SDK: https://cloud.google.com/sdk/
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# This file is used to generate README.rst
2+
3+
product:
4+
name: Google Cloud Bigtable
5+
short_name: Cloud Bigtable
6+
url: https://cloud.google.com/bigtable/docs
7+
description: >
8+
`Google Cloud Bigtable`_ is Google's NoSQL Big Data database service. It's
9+
the same database that powers many core Google services, including Search,
10+
Analytics, Maps, and Gmail.
11+
12+
setup:
13+
- auth
14+
- install_deps
15+
16+
samples:
17+
- name: Quickstart
18+
file: main.py
19+
show_help: true
20+
21+
cloud_client_library: true
22+
23+
folder: bigtable/quickstart_happybase

0 commit comments

Comments
 (0)
0