Spanner Emulator — your local Spanner DB for development & testing
Prerequisites
Microsoft Windows
Install Docker Desktop
Gcloud CLI
Objective
start emulator locally
Create an instance — instance is an allocation of resources that is used by Spanner databases created in that instance
Create a database — A Spanner database is a container for your tables, views, and indexes. Each Spanner instance can have multiple databases.
Create tables — Spanner databases contain one or more tables. Tables are structured as rows and columns
Describe the DDL — describe a Cloud Spanner database
Executes a SQL query against a Cloud Spanner database
Step 1:
start Docker desktop
gcloud emulators spanner start
Step 2:
create a gcloud configuration
gcloud config configurations create emulator
gcloud config set auth/disable_credentials true
gcloud config set project your-project-id
gcloud config set api_endpoint_overrides/spanner http://localhost:9020/
Step 3:
create an instance with the emulator’s instance config:
gcloud spanner instances create test-instance — config=emulator-config description=”Test Instance” — nodes=1 (enter double hyphen for flags.)
Step 4:
Create a Google Dialect database
gcloud spanner databases create test-database — instance=test-instance — ddl=”CREATE TABLE orders (order_id STRING(36) NOT NULL,description STRING(255), creation_timestamp TIMESTAMP,) PRIMARY KEY (order_id);CREATE TABLE order_items (order_id STRING(36) NOT NULL,order_item_id STRING(36) NOT NULL,description STRING(255),quantity INT64,) PRIMARY KEY (order_id, order_item_id),INTERLEAVE IN PARENT orders ON DELETE CASCADE;”
Step 5:
describe DDL:
gcloud spanner databases ddl describe test-database — instance=test-instance
Step 6:
gcloud spanner databases execute-sql test-database — instance=test-instance — sql=”SELECT * FROM orders;”
we tested Spanner emulator locally, we were able to create tables using gcloud CLI.