#google-cloud #spanner #google-api #gcp

google-cloud-spanner

Google Cloud Platform spanner client library

45 releases (breaking)

0.33.0 Feb 5, 2025
0.32.0 Nov 26, 2024
0.31.1 Sep 22, 2024
0.30.0 Jul 12, 2024
0.2.0 Nov 28, 2021

#2030 in Web programming

Download history 716/week @ 2024-11-20 448/week @ 2024-11-27 635/week @ 2024-12-04 1089/week @ 2024-12-11 698/week @ 2024-12-18 287/week @ 2024-12-25 381/week @ 2025-01-01 460/week @ 2025-01-08 453/week @ 2025-01-15 611/week @ 2025-01-22 722/week @ 2025-01-29 824/week @ 2025-02-05 608/week @ 2025-02-12 544/week @ 2025-02-19 898/week @ 2025-02-26 714/week @ 2025-03-05

2,919 downloads per month
Used in 2 crates

MIT license

9.5MB
167K SLoC

Bazel 132K SLoC // 0.1% comments Rust 35K SLoC // 0.0% comments Shell 181 SLoC // 0.3% comments Forge Config 1 SLoC // 0.8% comments

google-cloud-spanner

Google Cloud Platform spanner library.

crates.io

Installation

[dependencies]
google-cloud-spanner = "version"

Quickstart

Create Client and call transaction API same as Google Cloud Go.

 use google_cloud_spanner::client::Client;
 use google_cloud_spanner::mutation::insert;
 use google_cloud_spanner::statement::Statement;
 use google_cloud_spanner::value::CommitTimestamp;
 use google_cloud_spanner::client::Error;

 #[tokio::main]
 async fn main() -> Result<(), Error> {

     const DATABASE: &str = "projects/local-project/instances/test-instance/databases/local-database";

     // Create spanner client
     let config = ClientConfig::default().with_auth().await.unwrap();
     let mut client = Client::new(DATABASE, config).await.unwrap();

     // Insert
     let mutation = insert("Guild", &["GuildId", "OwnerUserID", "UpdatedAt"], &[&"guildId", &"ownerId", &CommitTimestamp::new()]);
     let commit_timestamp = client.apply(vec![mutation]).await?;

     // Read with query
     let mut stmt = Statement::new("SELECT GuildId FROM Guild WHERE OwnerUserID = @OwnerUserID");
     stmt.add_param("OwnerUserID",&"ownerId");
     let mut tx = client.single().await?;
     let mut iter = tx.query(stmt).await?;
     while let Some(row) = iter.next().await? {
         let guild_id = row.column_by_name::<String>("GuildId");
     }

     // Remove all the sessions.
     client.close().await;
     Ok(())
 }

Performance

Result of the 24 hours Load Test.

Metrics This library Google Cloud Go
RPS 438.4 443.4
Used vCPU 0.37 ~ 0.42 0.65 ~ 0.70

Test condition

  • 2.0 vCPU GKE Autopilot Pod
  • 1 Node spanner database server
  • 100 Users
  • Here is the application for Load Test.

Dependencies

~9–24MB
~379K SLoC