Skip to main content

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


Download files

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

Source Distribution

spannerlib_python-0.1.1.tar.gz (78.7 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

spannerlib_python-0.1.1-py3-none-any.whl (79.2 MB view details)

Uploaded Python 3

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

Hashes for spannerlib_python-0.1.1.tar.gz
Algorithm Hash digest
SHA256 4a835600b83895619f6bba2f12db045390da55838d8c83b00e0a137a2257af54
MD5 ffe497085920882b97efd1d1343245ce
BLAKE2b-256 1296b4383799cfabe04e2d34cf44215620d4f0ffb7b31ace2ab6c45a90526583

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file spannerlib_python-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for spannerlib_python-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 75398588c3d3d3937aac20a1a31f3e6c85f89a7aece5b31c370858f74d91298d
MD5 9bf6524b1489c7e8273097ee233fda37
BLAKE2b-256 fc0518b5cc803379b9d40a0105e76114a7ef9d3f58ff06bb517127f1b57c8b00

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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