A Python wrapper for the Go spannerlib. This is an internal library that can make breaking changes without prior notice.
Project description
SPANNERLIB-PYTHON: A High-Performance Python Wrapper for the Go Spanner Client Shared lib
NOTICE: This is an internal library that can make breaking changes without prior notice.
Introduction
The spannerlib-python wrapper provides a high-performance, idiomatic Python interface for Google Cloud Spanner by wrapping the official Go Client Shared library.
The Go library is compiled into a C-shared library, and this project calls it directly from Python, aiming to combine Go's performance with Python's ease of use.
Installation
To install the library, use pip:
pip install spannerlib-python
Usage
Here is a simple example of how to use the library to execute a SQL query.
1. Import the library
from google.cloud.spannerlib import Pool
from google.cloud.spanner_v1 import ExecuteSqlRequest
2. Create a connection pool
Initialize the pool with your database connection string.
connection_string = "projects/<your-project>/instances/<your-instance>/databases/<your-database>"
pool = Pool.create_pool(connection_string)
3. Create a connection
conn = pool.create_connection()
4. Execute a query
Use ExecuteSqlRequest to specify your SQL query.
sql = "SELECT 'Hello, World!' as greeting"
request = ExecuteSqlRequest(sql=sql)
# Execute the query
rows = conn.execute(request)
# Iterate over the results
while True:
row = rows.next()
if row is None:
break
# row is a google.protobuf.struct_pb2.ListValue
print(row.values[0].string_value)
# Close the rows object when done
rows.close()
5. Cleanup
Always close the connection and pool to release resources.
conn.close()
pool.close()
Using Context Managers
You can also use the with statement to automatically manage resources (close connections and pools).
from google.cloud.spannerlib import Pool
from google.cloud.spanner_v1 import ExecuteSqlRequest
connection_string = "projects/<your-project>/instances/<your-instance>/databases/<your-database>"
# Pool and Connection are context managers
with Pool.create_pool(connection_string) as pool:
with pool.create_connection() as conn:
sql = "SELECT 'Hello, World!' as greeting"
request = ExecuteSqlRequest(sql=sql)
# Rows/Results are also context managers
with conn.execute(request) as rows:
for row in iter(rows.next, None):
print(row.values[0].string_value)
# Resources are automatically closed here
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file spannerlib_python-0.1.1.tar.gz.
File metadata
- Download URL: spannerlib_python-0.1.1.tar.gz
- Upload date:
- Size: 78.7 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4a835600b83895619f6bba2f12db045390da55838d8c83b00e0a137a2257af54
|
|
| MD5 |
ffe497085920882b97efd1d1343245ce
|
|
| BLAKE2b-256 |
1296b4383799cfabe04e2d34cf44215620d4f0ffb7b31ace2ab6c45a90526583
|
Provenance
The following attestation bundles were made for spannerlib_python-0.1.1.tar.gz:
Publisher:
release-python-spanner-lib-wrapper.yml on googleapis/go-sql-spanner
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
spannerlib_python-0.1.1.tar.gz -
Subject digest:
4a835600b83895619f6bba2f12db045390da55838d8c83b00e0a137a2257af54 - Sigstore transparency entry: 752774120
- Sigstore integration time:
-
Permalink:
googleapis/go-sql-spanner@30e6339ecd3236a5642ce1070348232c2c3f1ff8 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/googleapis
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release-python-spanner-lib-wrapper.yml@30e6339ecd3236a5642ce1070348232c2c3f1ff8 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file spannerlib_python-0.1.1-py3-none-any.whl.
File metadata
- Download URL: spannerlib_python-0.1.1-py3-none-any.whl
- Upload date:
- Size: 79.2 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
75398588c3d3d3937aac20a1a31f3e6c85f89a7aece5b31c370858f74d91298d
|
|
| MD5 |
9bf6524b1489c7e8273097ee233fda37
|
|
| BLAKE2b-256 |
fc0518b5cc803379b9d40a0105e76114a7ef9d3f58ff06bb517127f1b57c8b00
|
Provenance
The following attestation bundles were made for spannerlib_python-0.1.1-py3-none-any.whl:
Publisher:
release-python-spanner-lib-wrapper.yml on googleapis/go-sql-spanner
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
spannerlib_python-0.1.1-py3-none-any.whl -
Subject digest:
75398588c3d3d3937aac20a1a31f3e6c85f89a7aece5b31c370858f74d91298d - Sigstore transparency entry: 752774123
- Sigstore integration time:
-
Permalink:
googleapis/go-sql-spanner@30e6339ecd3236a5642ce1070348232c2c3f1ff8 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/googleapis
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release-python-spanner-lib-wrapper.yml@30e6339ecd3236a5642ce1070348232c2c3f1ff8 -
Trigger Event:
workflow_dispatch
-
Statement type: