Skip to main content

Cloud Spanner API client library

Project description

pypi versions

Cloud Spanner is the world’s first fully managed relational database service to offer both strong consistency and horizontal scalability for mission-critical online transaction processing (OLTP) applications. With Cloud Spanner you enjoy all the traditional benefits of a relational database; but unlike any other relational database service, Cloud Spanner scales horizontally to hundreds or thousands of servers to handle the biggest transactional workloads.

Quick Start

In order to use this library, you first need to go through the following steps:

  1. Select or create a Cloud Platform project.

  2. Enable billing for your project.

  3. Enable the Google Cloud Datastore API.

  4. Setup Authentication.

Installation

Install this library in a virtualenv using pip. virtualenv is a tool to create isolated Python environments. The basic problem it addresses is one of dependencies and versions, and indirectly permissions.

With virtualenv, it’s possible to install this library without needing system install permissions, and without clashing with the installed system dependencies.

Mac/Linux

pip install virtualenv
virtualenv <your-env>
source <your-env>/bin/activate
<your-env>/bin/pip install google-cloud-datastore

Windows

pip install virtualenv
virtualenv <your-env>
<your-env>\Scripts\activate
<your-env>\Scripts\pip.exe install google-cloud-datastore

Example Usage

Executing Arbitrary SQL in a Transaction

Generally, to work with Cloud Spanner, you will want a transaction. The preferred mechanism for this is to create a single function, which executes as a callback to database.run_in_transaction:

# First, define the function that represents a single "unit of work"
# that should be run within the transaction.
def update_anniversary(transaction, person_id, unix_timestamp):
    # The query itself is just a string.
    #
    # The use of @parameters is recommended rather than doing your
    # own string interpolation; this provides protections against
    # SQL injection attacks.
    query = """SELECT anniversary FROM people
        WHERE id = @person_id"""

    # When executing the SQL statement, the query and parameters are sent
    # as separate arguments. When using parameters, you must specify
    # both the parameters themselves and their types.
    row = transaction.execute_sql(
        query=query,
        params={'person_id': person_id},
        param_types={
            'person_id': types.INT64_PARAM_TYPE,
        },
    ).one()

    # Now perform an update on the data.
    old_anniversary = row[0]
    new_anniversary = _compute_anniversary(old_anniversary, years)
    transaction.update(
        'people',
        ['person_id', 'anniversary'],
        [person_id, new_anniversary],
    )

# Actually run the `update_anniversary` function in a transaction.
database.run_in_transaction(update_anniversary,
    person_id=42,
    unix_timestamp=1335020400,
)

Select records using a Transaction

Once you have a transaction object (such as the first argument sent to run_in_transaction), reading data is easy:

# Define a SELECT query.
query = """SELECT e.first_name, e.last_name, p.telephone
    FROM employees as e, phones as p
    WHERE p.employee_id == e.employee_id"""

# Execute the query and return results.
result = transaction.execute_sql(query)
for row in result.rows:
    print(row)

Insert records using a Transaction

To add one or more records to a table, use insert:

transaction.insert(
    'citizens',
    columns=['email', 'first_name', 'last_name', 'age'],
    values=[
        ['phred@exammple.com', 'Phred', 'Phlyntstone', 32],
        ['bharney@example.com', 'Bharney', 'Rhubble', 31],
    ],
)

Update records using a Transaction

Transaction.update updates one or more existing records in a table. Fails if any of the records does not already exist.

transaction.update(
    'citizens',
    columns=['email', 'age'],
    values=[
        ['phred@exammple.com', 33],
        ['bharney@example.com', 32],
    ],
)

Next Steps

Project details


Release history Release notifications | RSS feed

This version

1.6.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

google-cloud-spanner-1.6.0.tar.gz (172.3 kB view details)

Uploaded Source

Built Distribution

google_cloud_spanner-1.6.0-py2.py3-none-any.whl (153.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file google-cloud-spanner-1.6.0.tar.gz.

File metadata

  • Download URL: google-cloud-spanner-1.6.0.tar.gz
  • Upload date:
  • Size: 172.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.0

File hashes

Hashes for google-cloud-spanner-1.6.0.tar.gz
Algorithm Hash digest
SHA256 f7140e1cb43fbf670521112f03822b63d15fbcbd2830c7cfa1b868836e04b6b4
MD5 d7ddae5c44cc14b1de804e8e985927af
BLAKE2b-256 623048757edeeb6c4f8699e5897c1768be23acbebf76539690e31f4f476ed3d9

See more details on using hashes here.

Provenance

File details

Details for the file google_cloud_spanner-1.6.0-py2.py3-none-any.whl.

File metadata

  • Download URL: google_cloud_spanner-1.6.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 153.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.0

File hashes

Hashes for google_cloud_spanner-1.6.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 f37ba0d8351d62ad816e04f6d0e4df19e4565acbbad9b517d1fbfad1d1b10d98
MD5 5a7968c11139ad932164e3bf37a36fc9
BLAKE2b-256 6864c3ad734184ab593d826a9837b77c3ec255c82e37acf0cfdc7461ea9336c7

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page