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.
Authentication is typically done through Application Default Credentials, which means you do not have to change the code to authenticate as long as your environment has credentials. You have a few options for setting up authentication:
When running locally, use the Google Cloud SDK
gcloud beta auth application-default login
When running on App Engine or Compute Engine, credentials are already set-up. However, you may need to configure your Compute Engine instance with additional scopes.
You can create a Service Account key file. This file can be used to authenticate to Google Cloud Platform services from any environment. To use the file, set the
GOOGLE_APPLICATION_CREDENTIALS
environment variable to the path to the key file, for example:export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account.json
Install pip and virtualenv if you do not already have them.
Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+.
$ virtualenv env $ source env/bin/activate
Install the dependencies needed to run the samples.
$ pip install -r requirements.txt
To run this sample:
$ python snippets.py
usage: snippets.py [-h] [--database-name DATABASE_NAME]
instance_name
{insert_data,query_data,read_data,update_data,read_write_transaction,query_data_with_index,read_data_with_index,read_data_with_storing_index}
...
This application demonstrates how to do basic operations using Cloud
Spanner.
For more information, see the README.rst under /spanner.
positional arguments:
instance_name Your Cloud Spanner instance name.
{insert_data,query_data,read_data,update_data,read_write_transaction,query_data_with_index,read_data_with_index,read_data_with_storing_index}
insert_data Inserts sample data into the given database. The
database and table must already exist and can be
created using `create_database`.
query_data Queries sample data from the database using SQL.
read_data Reads sample data from the database.
update_data Updates sample data in the database. This updates the
`MarketingBudget` column which must be created before
running this sample. Run the following query on your
database to create the column: ALTER TABLE Albums ADD
COLUMN MarketingBudget INT64
read_write_transaction
Performs a read-write transaction to update two sample
records in the database. This will transfer 200,000
from the `MarketingBudget` field for the first Album
to the second Album. If the `MarketingBudget` is too
low, it will raise an exception. Before running this
sample, you will need to run the `update_data` sample
to populate the fields.
query_data_with_index
Inserts sample data into the given database. The
database and table must already exist and can be
created using `create_database`.
read_data_with_index
Inserts sample data into the given database. The
database and table must already exist and can be
created using `create_database`.
read_data_with_storing_index
Inserts sample data into the given database. The
database and table must already exist and can be
created using `create_database`.
optional arguments:
-h, --help show this help message and exit
--database-name DATABASE_NAME
Your Cloud Spanner database name.
This sample uses the `Google Cloud Client Library for Python`_. You can read the documentation for more details on API usage and use GitHub to `browse the source`_ and `report issues`_.