Skip to content

Commit 4e8f7c5

Browse files
author
Jon Wayne Parrott
authored
0 parents  commit 4e8f7c5

File tree

7 files changed

+907
-0
lines changed

7 files changed

+907
-0
lines changed

samples/samples/README.rst

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
.. This file is automatically generated. Do not edit this file directly.
2+
3+
Google Cloud Spanner Python Samples
4+
===============================================================================
5+
6+
This directory contains samples for Google Cloud Spanner. `Google Cloud Spanner`_ is a highly scalable, transactional, managed, NewSQL database service. Cloud Spanner solves the need for a horizontally-scaling database with consistent global transactions and SQL semantics.
7+
8+
9+
10+
11+
.. _Google Cloud Spanner: https://cloud.google.com/spanner/docs
12+
13+
Setup
14+
-------------------------------------------------------------------------------
15+
16+
17+
Authentication
18+
++++++++++++++
19+
20+
Authentication is typically done through `Application Default Credentials`_,
21+
which means you do not have to change the code to authenticate as long as
22+
your environment has credentials. You have a few options for setting up
23+
authentication:
24+
25+
#. When running locally, use the `Google Cloud SDK`_
26+
27+
.. code-block:: bash
28+
29+
gcloud beta auth application-default login
30+
31+
32+
#. When running on App Engine or Compute Engine, credentials are already
33+
set-up. However, you may need to configure your Compute Engine instance
34+
with `additional scopes`_.
35+
36+
#. You can create a `Service Account key file`_. This file can be used to
37+
authenticate to Google Cloud Platform services from any environment. To use
38+
the file, set the ``GOOGLE_APPLICATION_CREDENTIALS`` environment variable to
39+
the path to the key file, for example:
40+
41+
.. code-block:: bash
42+
43+
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account.json
44+
45+
.. _Application Default Credentials: https://cloud.google.com/docs/authentication#getting_credentials_for_server-centric_flow
46+
.. _additional scopes: https://cloud.google.com/compute/docs/authentication#using
47+
.. _Service Account key file: https://developers.google.com/identity/protocols/OAuth2ServiceAccount#creatinganaccount
48+
49+
Install Dependencies
50+
++++++++++++++++++++
51+
52+
#. Install `pip`_ and `virtualenv`_ if you do not already have them.
53+
54+
#. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+.
55+
56+
.. code-block:: bash
57+
58+
$ virtualenv env
59+
$ source env/bin/activate
60+
61+
#. Install the dependencies needed to run the samples.
62+
63+
.. code-block:: bash
64+
65+
$ pip install -r requirements.txt
66+
67+
.. _pip: https://pip.pypa.io/
68+
.. _virtualenv: https://virtualenv.pypa.io/
69+
70+
Samples
71+
-------------------------------------------------------------------------------
72+
73+
Snippets
74+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
75+
76+
77+
78+
To run this sample:
79+
80+
.. code-block:: bash
81+
82+
$ python snippets.py
83+
84+
usage: snippets.py [-h] [--database-name DATABASE_NAME]
85+
instance_name
86+
{insert_data,query_data,read_data,update_data,read_write_transaction,query_data_with_index,read_data_with_index,read_data_with_storing_index}
87+
...
88+
89+
This application demonstrates how to do basic operations using Cloud
90+
Spanner.
91+
92+
For more information, see the README.rst under /spanner.
93+
94+
positional arguments:
95+
instance_name Your Cloud Spanner instance name.
96+
{insert_data,query_data,read_data,update_data,read_write_transaction,query_data_with_index,read_data_with_index,read_data_with_storing_index}
97+
insert_data Inserts sample data into the given database. The
98+
database and table must already exist and can be
99+
created using `create_database`.
100+
query_data Queries sample data from the database using SQL.
101+
read_data Reads sample data from the database.
102+
update_data Updates sample data in the database. This updates the
103+
`MarketingBudget` column which must be created before
104+
running this sample. Run the following query on your
105+
database to create the column: ALTER TABLE Albums ADD
106+
COLUMN MarketingBudget INT64
107+
read_write_transaction
108+
Performs a read-write transaction to update two sample
109+
records in the database. This will transfer 200,000
110+
from the `MarketingBudget` field for the first Album
111+
to the second Album. If the `MarketingBudget` is too
112+
low, it will raise an exception. Before running this
113+
sample, you will need to run the `update_data` sample
114+
to populate the fields.
115+
query_data_with_index
116+
Inserts sample data into the given database. The
117+
database and table must already exist and can be
118+
created using `create_database`.
119+
read_data_with_index
120+
Inserts sample data into the given database. The
121+
database and table must already exist and can be
122+
created using `create_database`.
123+
read_data_with_storing_index
124+
Inserts sample data into the given database. The
125+
database and table must already exist and can be
126+
created using `create_database`.
127+
128+
optional arguments:
129+
-h, --help show this help message and exit
130+
--database-name DATABASE_NAME
131+
Your Cloud Spanner database name.
132+
133+
134+
135+
136+
The client library
137+
-------------------------------------------------------------------------------
138+
139+
This sample uses the `Google Cloud Client Library for Python`_.
140+
You can read the documentation for more details on API usage and use GitHub
141+
to `browse the source`_ and `report issues`_.
142+
143+
.. Google Cloud Client Library for Python:
144+
https://googlecloudplatform.github.io/google-cloud-python/
145+
.. browse the source:
146+
https://github.com/GoogleCloudPlatform/google-cloud-python
147+
.. report issues:
148+
https://github.com/GoogleCloudPlatform/google-cloud-python/issues
149+
150+
151+
.. _Google Cloud SDK: https://cloud.google.com/sdk/

samples/samples/README.rst.in

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# This file is used to generate README.rst
2+
3+
product:
4+
name: Google Cloud Spanner
5+
short_name: Cloud Spanner
6+
url: https://cloud.google.com/spanner/docs
7+
description: >
8+
`Google Cloud Spanner`_ is a highly scalable, transactional, managed,
9+
NewSQL database service. Cloud Spanner solves the need for a
10+
horizontally-scaling database with consistent global transactions and
11+
SQL semantics.
12+
13+
setup:
14+
- auth
15+
- install_deps
16+
17+
samples:
18+
- name: Snippets
19+
file: snippets.py
20+
show_help: true
21+
22+
cloud_client_library: true

samples/samples/quickstart.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2016 Google Inc. All Rights Reserved.
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+
17+
18+
def run_quickstart():
19+
# [START spanner_quickstart]
20+
# Imports the Google Cloud Client Library.
21+
from google.cloud import spanner
22+
23+
# Instantiate a client.
24+
spanner_client = spanner.Client()
25+
26+
# Your Cloud Spanner instance ID.
27+
instance_id = 'my-instance-id'
28+
29+
# Get a Cloud Spanner instance by ID.
30+
instance = spanner_client.instance(instance_id)
31+
32+
# Your Cloud Spanner database ID.
33+
database_id = 'my-database-id'
34+
35+
# Get a Cloud Spanner database by ID.
36+
database = instance.database(database_id)
37+
38+
# Execute a simple SQL statement.
39+
results = database.execute_sql('SELECT 1')
40+
41+
for row in results:
42+
print(row)
43+
# [END spanner_quickstart]
44+
45+
46+
if __name__ == '__main__':
47+
run_quickstart()

samples/samples/quickstart_test.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Copyright 2016 Google Inc. All Rights Reserved.
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+
from google.cloud import spanner
16+
import google.cloud.exceptions
17+
import google.cloud.spanner.client
18+
import mock
19+
import pytest
20+
21+
import quickstart
22+
23+
24+
@pytest.fixture
25+
def patch_instance(cloud_config):
26+
original_instance = google.cloud.spanner.client.Client.instance
27+
28+
def new_instance(self, unused_instance_name):
29+
return original_instance(self, cloud_config.spanner_instance)
30+
31+
instance_patch = mock.patch(
32+
'google.cloud.spanner.client.Client.instance',
33+
side_effect=new_instance,
34+
autospec=True)
35+
36+
with instance_patch:
37+
yield
38+
39+
40+
@pytest.fixture
41+
def example_database(cloud_config):
42+
spanner_client = spanner.Client()
43+
instance = spanner_client.instance(cloud_config.spanner_instance)
44+
database = instance.database('my-database-id')
45+
46+
if not database.exists():
47+
database.create()
48+
49+
yield
50+
51+
52+
def test_quickstart(capsys, patch_instance, example_database):
53+
quickstart.run_quickstart()
54+
out, _ = capsys.readouterr()
55+
assert '[1]' in out

samples/samples/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
google-cloud-spanner==0.23.0

0 commit comments

Comments
 (0)