Skip to content

Instantly share code, notes, and snippets.

@apstndb
Last active April 1, 2020 16:56
Show Gist options
  • Save apstndb/676d3a9e6022574880a0f45c9718cf2c to your computer and use it in GitHub Desktop.
Save apstndb/676d3a9e6022574880a0f45c9718cf2c to your computer and use it in GitHub Desktop.

Start Cloud Spanner Emulator

gcloud beta emulators spanner start

Setup database(not idempotent and error prone)

SPANNER_EMULATOR_HOST_REST=localhost:9020
PROJECT_NAME="example-project"
INSTANCE_NAME="example-instance"
DATABASE_NAME="example-database"
INSTANCE_FULLNAME="$(jq --arg instance $INSTANCE_NAME -n '{instanceId:$instance}' | curl -s "${SPANNER_EMULATOR_HOST_REST}/v1/projects/${PROJECT_NAME}/instances" --data @- | jq -r '.response.name')"
DATABASE_FULLNAME="$(jq --arg database $DATABASE_NAME -n '{createStatement:"CREATE DATABASE `\($database)`"}' | curl -s "${SPANNER_EMULATOR_HOST_REST}/v1/${INSTANCE_FULLNAME}/databases" --data @- | jq -r '.response.name')"

Connect Cloud Spanner Emulator using spanner-cli

eval "$(gcloud beta emulators spanner env-init)"
spanner-cli -p ${PROJECT_NAME} -i ${INSTANCE_NAME} -d ${DATABASE_NAME}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment