Visitar URL original
google_cloud_spanner - Rust
Skip to main content

Crate google_cloud_spanner

Crate google_cloud_spanner 

Source
Expand description

Google Cloud Client Libraries for Rust - Spanner

NOTE: While the version is still 0.x, we believe the APIs to be stable. We plan to release a 1.0 version of this crate in an upcoming release.

We welcome feedback about the APIs, documentation, missing features, bugs, etc.

This crate contains traits, types, and functions to interact with Google Cloud Spanner. Cloud Spanner is a fully managed, mission-critical, relational database service that offers transactional consistency at global scale, schemas, SQL (ANSI 2011 with extensions), and automatic, synchronous replication for high availability.

Most applications will use the clients defined in the client module:

  • Spanner: The root service client. Manages gRPC channel pools, credentials, and emulator settings, and constructs DatabaseClient instances and admin client builders.
  • DatabaseClient: The primary client for reading, querying, and modifying data in a specific database.
  • DatabaseAdmin: Admin client for managing databases, backups, and applying DDL schema updates.
  • InstanceAdmin: Admin client for creating and managing Spanner instances and configurations.

§Architecture and Client Lifecycle

  • Spanner (Root Service Client): Represents a shared gRPC transport and credential context. It should be initialized once at application startup and reused across the entire process.
  • DatabaseClient (Per-Database Client): Manages a long-lived multiplexed session for a specific database in the background. Creating a DatabaseClient initializes session and connection state, so an application should create a single DatabaseClient per database and share it across all tasks.
  • Thread Safety & Cheap Cloning: Both Spanner and DatabaseClient wrap internal state in Arc pointers. Cloning them is cheap ($O(1)$) and shares underlying sessions, channel pools, and routing state. Never create a new client per request.

§Transaction Models

Spanner provides specialized transaction types tailored for different access patterns:

  • Single-Use: Optimized for executing a single read or query with minimal latency and no transaction initialization overhead. Supports timestamp bounds to configure read staleness.
  • Read-Write: Atomic reads and writes coordinated via TransactionRunner. Automatically retries the closure if Spanner aborts the transaction.
  • Multi-Use Read-Only: Executes multiple queries and reads against a consistent point-in-time snapshot without taking locks.
  • Write-Only: High-throughput blind writes using mutations when reads prior to writing are not required.
  • Partitioned DML: Executes distributed, parallel bulk updates or deletions across large tables without transaction mutation limits.
  • Batch Read-Only: Splits queries or reads into parallel partitions that can be distributed across worker processes (e.g., Apache Beam).
  • Batch Write: Executes multiple mutation groups in parallel with per-group atomicity.

§Example: Executing a Parameterized Query

Parameterized queries prevent SQL injection and maximize Spanner’s query plan cache hits:

use google_cloud_spanner::client::Spanner;
use google_cloud_spanner::statement::Statement;

// Initialize the root service client.
let spanner = Spanner::builder().build().await?;

// Create a long-lived client for a specific database.
let database_client = spanner
    .database_client("projects/my-project/instances/my-instance/databases/my-database")
    .build()
    .await?;

// Execute a single-use query.
let transaction = database_client.single_use().build();
let statement = Statement::builder(
    "SELECT SingerId, FirstName, LastName FROM Singers WHERE SingerId = @id",
)
.add_param("id", 42i64)
.build();

let mut result_set = transaction.execute_query(statement).await?;
while let Some(row) = result_set.next().await.transpose()? {
    let singer_id: i64 = row.get("SingerId")?;
    let first_name: String = row.get("FirstName")?;
    let last_name: String = row.get("LastName")?;
    println!("Singer {singer_id}: {first_name} {last_name}");
}

§Example: Read-Write Transaction

Read-write transactions execute reads and modifications atomically. Use TransactionRunner to execute operations; it automatically retries the transaction closure if Spanner aborts it:

use google_cloud_spanner::client::DatabaseClient;
use google_cloud_spanner::statement::Statement;

let runner = database_client.read_write_transaction().build();
let singer_ids = vec![1i64, 2i64, 3i64];

let transaction_result = runner
    .run(async |transaction| {
        // 1. Query singers matching the array parameter using `IN UNNEST`:
        let query_statement = Statement::builder(
            "SELECT SingerId, Rating FROM Singers WHERE SingerId IN UNNEST(@ids)",
        )
        .add_param("ids", &singer_ids)
        .build();

        let mut result_set = transaction.execute_query(query_statement).await?;
        let mut statements = Vec::new();

        while let Some(row) = result_set.next().await.transpose()? {
            let singer_id: i64 = row.get("SingerId")?;
            let current_rating: Option<i64> = row.get("Rating")?;
            let update_statement = Statement::builder(
                "UPDATE Singers SET Rating = @new_rating WHERE SingerId = @id",
            )
            .add_param("new_rating", current_rating.unwrap_or(0) + 1)
            .add_param("id", singer_id)
            .build();
            statements.push(update_statement);
        }

        // 2. If no singers matched, return early to avoid unnecessary updates:
        if statements.is_empty() {
            return Ok(Vec::new());
        }

        // 3. Update singer ratings in a single round trip using Batch DML:
        let update_counts = transaction.execute_batch_update(statements).await?;

        // 4. Update summary statistics using a single DML statement:
        let summary_statement = Statement::builder(
            "UPDATE SingerStatistics SET AverageRating = (SELECT AVG(Rating) FROM Singers) WHERE Metric = 'Global'",
        )
        .build();
        transaction.execute_update(summary_statement).await?;

        Ok(update_counts)
    })
    .await?;

println!("Updated singer rows: {:?}", transaction_result.result);

§Example: Consistent Snapshot Multi-Query Read

Multi-use read-only transactions guarantee that multiple reads and queries observe the exact same consistent snapshot in time without taking locks.

While strong reads (the default) may require consulting the Paxos leader region to negotiate timestamps, specifying a staleness of at least 15 seconds allows nearby replicas (including read-only replicas) to serve reads locally without leader communication, reducing latency in multi-region instances:

use std::time::Duration;
use google_cloud_spanner::client::DatabaseClient;
use google_cloud_spanner::statement::Statement;
use google_cloud_spanner::transaction::TimestampBound;

// Configure staleness (>= 15s) so nearby replicas can serve the query without leader consultation.
let timestamp_bound = TimestampBound::exact_staleness(Duration::from_secs(15));
let transaction = database_client
    .read_only_transaction()
    .set_timestamp_bound(timestamp_bound)
    .build()
    .await?;

let first_statement = Statement::builder(
    "SELECT SingerId, FirstName FROM Singers WHERE SingerId = @id",
)
.add_param("id", 1i64)
.build();
let mut first_results = transaction.execute_query(first_statement).await?;
while let Some(row) = first_results.next().await.transpose()? {
    let singer_id: i64 = row.get("SingerId")?;
    let first_name: String = row.get("FirstName")?;
    println!("Singer {singer_id}: {first_name}");
}

let second_statement = Statement::builder(
    "SELECT AlbumId, AlbumTitle FROM Albums WHERE SingerId = @id",
)
.add_param("id", 1i64)
.build();
let mut second_results = transaction.execute_query(second_statement).await?;
while let Some(row) = second_results.next().await.transpose()? {
    let album_id: i64 = row.get("AlbumId")?;
    let album_title: String = row.get("AlbumTitle")?;
    println!("Album {album_id}: {album_title}");
}

§Example: Writing Mutations

For blind writes where reading prior to writing is not required, write mutations directly using write_only_transaction:

use google_cloud_spanner::client::DatabaseClient;
use google_cloud_spanner::mutation::Mutation;

let mutations = vec![
    Mutation::new_insert_or_update_builder("Singers")
        .set("SingerId").to(1i64)
        .set("FirstName").to("Marc")
        .set("LastName").to("Richards")
        .set("Rating").to(4i64)
        .build(),
    Mutation::new_insert_or_update_builder("Singers")
        .set("SingerId").to(2i64)
        .set("FirstName").to("Catalina")
        .set("LastName").to("Smith")
        .set("Rating").to(5i64)
        .build(),
];

database_client
    .write_only_transaction()
    .build()
    .write(mutations)
    .await?;

§Local Development with the Spanner Emulator

To develop locally against the Spanner Emulator, set the SPANNER_EMULATOR_HOST environment variable before creating the client:

export SPANNER_EMULATOR_HOST=localhost:9010

The client builder automatically detects this environment variable, configures anonymous credentials, points gRPC calls to the emulator host, and defaults to a static channel pool with a single channel.

§Features

  • builtin-metrics: enabled by default. Automatically exports Spanner built-in request and attempt latency metrics to Google Cloud Monitoring in the background.
  • default-rustls-provider: enabled by default. Use the default rustls crypto provider (aws-lc-rs) for TLS and authentication.
  • unstable-metrics: enable integration with a custom caller-supplied OpenTelemetry MeterProvider. Because the opentelemetry crate is pre-1.0, this feature is subject to SemVer-incompatible changes across OpenTelemetry minor releases.
  • unstable-stream: enable the (unstable) features to convert several types to a futures::Stream.
  • unstable-time: enable support for types from the time crate (time::OffsetDateTime and time::Date).

Re-exports§

pub use opentelemetry;unstable-metrics

Modules§

batch
Batch execution and query partitioning support. Re-exports for Spanner batch execution and query partitioning.
builder
Consolidates all client and request builders. Re-exports for Spanner client and request builders.
channel_pool
Configuration types for the gRPC channel pool. Channel pooling for Spanner.
client
Spanner client implementations.
error
Crate error types.
key
Key and key range definition types.
model
The messages and enums that are part of this client library.
mutation
Write mutations and transaction commit binders.
omni
Spanner Omni options and configuration. Spanner Omni instance types and configuration utilities.
read
Configurable read requests and builders.
result
Spanner execution result streams and rows. Public types for Spanner execution results.
retry_policy
RPC retry policies used by the Spanner client. RPC retry policies used by the Spanner client.
statement
SQL statement builders and parameter bindings.
stub
Mocking and stub definitions.
transaction
Transaction-scoped interfaces and transaction runners. Re-exports for Spanner transactions.
types
Spanner primitive type constructors for typed parameter binding.
value
Type and value representations and conversion traits.

Macros§

key
Represents a primary key or index key for Cloud Spanner.
mutation
Constructs a Mutation using declarative syntax.

Structs§

Decimal
Decimal represents a 128 bit representation of a fixed-precision decimal number. The finite set of values of type Decimal are of the form m / 10e, where m is an integer such that -296 < m < 296, and e is an integer between 0 and 28 inclusive.
Error
The core error returned by all client libraries.

Type Aliases§

Result
An alias of std::result::Result where the error is always Error.