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.comCreate an Instance
gcloud spanner instances create spanner-instance \
--config=regional-us-central1 \
--nodes=1 --description="A Spanner Instance"Create a Database
A Cloud Spanner instance can host multiple databases, that each contains its own tables.
gcloud spanner databases create orders \
--instance=spanner-instanceConnect 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:
Cloud Spanner differs from traditional RDBMS in a several ways:
No server-side automatic ID generation.
Avoid monodically increasing IDs - I.e., no auto incremented ID, because it may create hot spots in partitions.
No foreign key constraints.
Cloud Spanner, being horizontally scalable and shards data with your keys, prefers the following:
UUIDv4 as IDs - It's random and can be partitioned easily.
Parent-Children relationship encoded using a compose primary key.
Colocate Parent-Children data using Interleave tables - If accessing parent means children data is also likely to be read, interleaving allows children data to be colocated with parent row in the same parition.
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.
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.
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.
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?