Skip to content

Repository files navigation

Introduction

This repository has a collection of Cloud Spanner examples in Java using different frameworks.

Options

There are multiple ways to connect to Cloud Spanner. The choices depends on the technology stack you prefer, or what your application is already using.

OptionWhen to use?
Spring Data SpannerIdiomatic Spring Data layer to Cloud Spanner. Consider this if you start with a new Spring Boot application, and need the most idiomatic, lightweight ORM to access Cloud Spanner features.
HibernateHibernate dialect for Cloud Spanner. Consider this if you want to use Hibernate, or already using Hibernate. You can use Cloud Spanner with Hibernate with Spring Data JPA, Microprofile, Quarkus, etc.
JDBCOpen source JDBC driver for Cloud Spanner. Consider this if you want to use Cloud Spanner with pure JDBC without any ORM. You can use this with Spring JDBC Template, MyBatis, etc.
R2DBCReactive database driver for Cloud Spanner. Consider this if you are building a fully reactive application using Reactive Stream, Reactor, RxJava, Akka Streams, etc.
Cloud Spanner Client LibraryRaw API access that may have raw features that any other options does not offer. Consider this as a last resort.

Setup Cloud Spanner for Examples

All of the examples use the same Cloud Spanner database / schema.

  1. Create a Cloud Spanner instance

    $ gcloud services enable spanner.googleapis.com
    $ gcloud spanner instances create demo --config=regional-us-central1 \
    --nodes=1 --description="Spanner demo"
  2. Configure the Schema

    CREATETABLEperson (
    id STRING(36) NOT NULL,
    name STRING(MAX),
    ) PRIMARY KEY (id)

    or, run this through a ddl file:

    cat <<EOF > schema.ddlCREATE TABLE person ( id STRING(36) NOT NULL, name STRING(MAX),) PRIMARY KEY (id)EOF

    and import the file:

    gcloud spanner databases ddl update demo \
    --instance=demo --ddl="$(cat schema.ddl)"
    
  3. Make sure you have the Service Account credentials configured.

    # Create a service account
    gcloud iam service-accounts create spanner-demo
    # Get the current Project IDexport GCP_PROJECT=$(gcloud config list --format 'value(core.project)')# Bind IAM permission
    gcloud projects add-iam-policy-binding $GCP_PROJECT \
    --member serviceAccount:spanner-demo@$GCP_PROJECT.iam.gserviceaccount.com \
    --role roles/spanner.databaseUser
    # Create the service account key file
    gcloud iam service-accounts keys create \
    --iam-account spanner-demo@${GCP_PROJECT}.iam.gserviceaccount.com \
    $HOME/spanner-demo.json
    # Configure Google Application Credentials environmental variableexport GOOGLE_APPLICATION_CREDENTIALS=$HOME/spanner-demo.json

Examples

Please note that every example configures the Cloud Spanner instance name differently. See each README for detail.

About

A collection of Cloud Spanner examples with Java

Topics

Resources

Stars

4 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages