Python Client for Cloud Spanner
Project description
Python idiomatic client for Cloud Spanner.
Quick Start
$ pip install --upgrade google-cloud-spanner
Fore more information on setting up your Python development environment, such as installing pip and on your system, please refer to Python Development Environment Setup Guide for Google Cloud Platform.
Authentication
With google-cloud-python we try to make authentication as painless as possible. Check out the Authentication section in our documentation to learn more. You may also find the authentication document shared by all the google-cloud-* libraries to be helpful.
Using the API
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. (About Cloud Spanner)
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],
],
)
Learn More
See the google-cloud-python API Cloud Spanner documentation to learn how to connect to Cloud Spanner using this Client Library.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file google-cloud-spanner-0.29.0.tar.gz
.
File metadata
- Download URL: google-cloud-spanner-0.29.0.tar.gz
- Upload date:
- Size: 151.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bc4c6269b07d0982289d4ecd9b88da943e097ab2104c79ce18e4966766bb877b |
|
MD5 | 22074141f7703d87883ef9f108fd5c84 |
|
BLAKE2b-256 | f4b3cd6c4743dbe0050006ece8985cf7ab35ba3efbc45d5a97cb42e0b27f8ce7 |
File details
Details for the file google_cloud_spanner-0.29.0-py2.py3-none-any.whl
.
File metadata
- Download URL: google_cloud_spanner-0.29.0-py2.py3-none-any.whl
- Upload date:
- Size: 133.1 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 39707e37e2399df0a6ea7f5b636fc06858359a70af92ac485bbba9bf810a43e7 |
|
MD5 | 6b20753c3b73fbe3e757b25750ebbc90 |
|
BLAKE2b-256 | b33fd1771299f1df521dd918830511f03d1df9ee27d5a2e20252811feed39060 |