Skip to content

Spanner Setup Guide

Shirley Cohen edited this page Mar 10, 2021 · 6 revisions

Follow this guide to set up your Spanner environment for this class.

Note that we will be using the Spanner emulator so this is not a one-time set up. You'll need to redo some of the same steps each time you stop and restart the environment. The steps that are marked as skipped only need to be run the first time. The steps that are not marked, need to be run each time you restart your jupyter instance.

Install and Configure the Spanner Emulator:

  1. Start up your Jupyter notebook instance and open a terminal window. Run the following commands in the terminal:
  2. sudo apt-get install google-cloud-sdk-spanner-emulator (skip this step after initial setup)
  3. gcloud beta emulators spanner start &

Once you see Cloud Spanner emulator running, REST server listening at localhost:9020, gRPC server listening at localhost:9010, press Enter to return to the shell prompt.

  1. gcloud config configurations create emulator (skip this step after initial setup)

Expected output:
Created [emulator].
Activated [emulator].

  1. gcloud config configurations activate emulator
  2. gcloud config set auth/disable_credentials true
  3. gcloud config set project {your_project_id}

Expected output:
Updated property [core/project].
WARNING: You do not appear to have access to project [{your_project_id}] or it does not exist.

  1. gcloud config set api_endpoint_overrides/spanner http://localhost:9020/
  2. gcloud spanner instance-configs list

Expected output:

NAME DISPLAY_NAME
emulator-config Emulator Instance Config

Create Spanner instance and database:

  1. gcloud spanner instances create span-instance --config=emulator-config --description="Spanner Test Instance" --nodes=1

Expected output:
Creating instance...done.

  1. gcloud spanner databases create test-database --instance span-instance --ddl "CREATE TABLE T1 (Key INT64, Value STRING(MAX)) PRIMARY KEY (Key)"

Expected output:
Creating database...done.

  1. gcloud spanner rows insert --table=T1 --database=test-database --instance=span-instance --data=Key=1,Value=TestValue1

  2. gcloud spanner databases execute-sql test-database --instance span-instance --sql "select * from T1"

Expected output:

Key Value
1 TestValue1

Install Go (needed to run Spanner-cli): (skip this section after initial setup)

  1. curl -O https://storage.googleapis.com/golang/go1.15.2.linux-amd64.tar.gz
  2. tar -xvf go1.15.2.linux-amd64.tar.gz
  3. sudo chown -R root:root ./go
  4. sudo mv go /usr/local

Add variables to your .bash_profile: (skip this section after initial setup)
export GOPATH=$HOME/go
export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin
export SPANNER_EMULATOR_HOST="localhost:9010"

  1. source .bash_profile
  2. go help

Install and configure Spanner cli:

  1. go get -u github.com/cloudspannerecosystem/spanner-cli (skip this step after initial setup)

Be patient. This command may take a couple minutes to run.

  1. spanner-cli -p {your_project_id} -i span-instance -d test-database

Expected output:
Connected.

  1. In the Spanner cli, run show tables;

Expected output:
+-------------------------+
| Tables_in_test-database |
+-------------------------+
| T1 |
+-------------------------+
1 rows in set (0.00 sec)

  1. In the Spanner cli, run select * from T1;

Expected output:
+-----+------------+
| Key | Value |
+-----+------------+
| 1 | TestValue1 |
+-----+------------+
1 rows in set (336.143us)

  1. In the Spanner cli, run exit; to quit

Clone this wiki locally