Cloud Spanner

Cloud Spanner is a scalable, enterprise-grade, globally-distributed, and strongly consistent database service built for the cloud specifically to combine the benefits of relational database structure with non-relational horizontal scale. It delivers high-performance transactions and strong consistency across rows, regions, and continents with an industry-leading 99.999% availability SLA, no planned downtime, and enterprise-grade security. Cloud Spanner revolutionizes database administration and management and makes application development more efficient.

Cloud Spanner Instance

Enable API

gcloud services enable spanner.googleapis.com

Create an Instance

gcloud spanner instances create spanner-instance \
  --config=regional-us-central1 \
  --nodes=1 --description="A Spanner Instance"

This example creates a new regional instance (i.e., spanning across zones within the same region). Cloud Spanner supports multi-regional configuration to span across multiple regions for highest availability.

Create a Database

A Cloud Spanner instance can host multiple databases, that each contains its own tables.

gcloud spanner databases create orders \
  --instance=spanner-instance

Connect to Instance

There is no interactive CLI to Cloud Spanner. You can create table and execute SQL statements from the Cloud Console, or from gcloud command line. Following are some common operations:

List Databases

Execute SQL Statements

Show Query Plan

Create a Table

Create a DDL file, schema.ddl:

Use gcloud to execute the DDL:

Spring Data Spanner Starter

The easiest way to access Cloud Spanner is using Spring Cloud GCP's Spring Data Spanner starter. This starter provides full Spring Data support for Cloud Spanner while implementing idiomatic access patterns.

Spring Data Feature

Supported

ORM

Declarative Transaction

Repository

REST Repository

Query methods

Query annotation

Pagination

Events

Auditing

Dependency

Add the Spring Data Spanner starter:

Configuration

Configure Cloud Spanner instance and database to connect to.

Notice that there is no explicit configuration for username/password. Cloud Spanner authentication uses the GCP credential (either your user credential, or Service Account credential), and authorization is configured via Identity Access Management (IAM).

ORM

Spring Data Cloud Spanner allows you to map domain POJOs to Cloud Spanner tables via annotations. Read the Spring Data Spanner reference documentation for details

Repository

Use Spring Data repository to quickly get CRUD access to the Cloud Spanner tables.

Order is the parent and has only a primary key. Spring Data repository's ID parameter type can be the type for the single key. OrderItem, however, has a composite key. Spring Data repository's ID parameter type must be Cloud Spanner's Key type for a composite key.

In a business logic service, you can utilize the repositories:

Rest Repository

Spring Data Rest can expose a Spring Data repository directly on a RESTful endpoint, and rendering the payload as JSON with HATEOS format. It supports common access patterns like pagination.

Add Spring Data Rest starter:

To access the endpoint for Order:

Samples

JDBC

Cloud Spanner JDBC Driver can be used if you need raw JDBC access.

Dependency

Add Cloud Spanner JDBC Driver:

Use Spring Boot JDBC Starter to use JDBC Template:

Configuration

Cloud Spanner JDBC Driver Class:

Cloud Spanner JDBC URL format:

With Spring Data JDBC, you can configure the datasource:

Hibernate

Cloud Spanner Hibernate Dialect lets you use Cloud Spanner with Hibernate ORM. You can use Cloud Spanner with Hibernate from any Java application. When using Spring Boot, you can use the Hibernate Dialect with Spring Data JPA.

Dependency

Add both the Cloud Spanner JDBC driver and Cloud Spanner Hibernate Dialect:

Add Spring Data JPA starter:

Configuration

Cloud Spanner Hibernate Dialect class:

Configure Spring Data JPA:

ORM

Use JPA annotations to map POJO to Cloud Spanner database tables.

In Cloud Spanner, parent-children relationship is modeled as a composite key. With JPA, use IdClass or EmbeddableId to map a composite key.

Repository

Use Spring Data repository for CRUD access to the tables.

Rest Repository

Use Spring Data Rest to expose the repositories as RESTful services with HATEOS format.

Sample

R2DBC

Cloud Spanner R2DBC driver let's you access Cloud Spanner data with reactive API.

Cloud Spanner R2DBC driver is under active development.

It can be used with Spring Data R2DBC using the Cloud Spanner R2DBC Dialect.

Samples

Last updated

Was this helpful?