Skip to main content

Setting Up for Google Cloud Spanner

Hands-On Lab


Photo of Joseph Lowery

Joseph Lowery

Google Cloud Training Architect II in Content





Today’s global companies require a global solution for their extensive data needs. Cloud Spanner provides a highly available, SQL-compliant database solution capable of scaling horizontally on a planet-wide scale. In this hands-on lab, you’ll walk through the process of establishing a Cloud Spanner instance and then creating a populated database to manage: from querying data to exporting it to Google Cloud Storage.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Setting Up for Google Cloud Spanner

In this hands-on lab, we’ll walk through the process of establishing a Cloud Spanner instance and then creating a populated database to manage: from querying data to exporting it to Google Cloud Storage.

How to Log in to Google Lab Accounts

On the lab page, right-click Open GCP Console and select the option to open it in a new private browser window (this option will read differently depending on your browser — e.g., in Chrome, it says "Open Link in Incognito Window"). Then, sign in to Google Cloud Platform using the credentials provided on the lab page.

On the Welcome to your new account screen, review the text, and click Accept. In the "Welcome L.A.!" pop-up once you're signed in, check to agree to the terms of service, choose your country of residence, and click Agree and Continue.

Setting Up for Cloud Spanner

Now, on to the lab!

Enable APIs.

  1. Navigate to APIs & Services > Library.
  2. Search for "Cloud Spanner".
  3. Select the Cloud Spanner API card.
  4. Click Enable.
  5. Head back to APIs & Services > Library.
  6. Search for "dataflow".
  7. Select the Dataflow API card.
  8. Click Enable.
  9. One last time, head to APIs & Services > Library.
  10. Search for "storage".
  11. Select the Cloud Storage card.
  12. We should see it's already enabled. (If it isn't, click Enable.)

Create a Cloud Spanner instance.

  1. Navigate to Spanner.
  2. Click Create instance.
  3. Provide the following details:
    • Instance name: la-spanner
    • Configuration: Regional, us-central1
    • Nodes: 1
  4. Click Create.

Create database and table.

  1. Click Create database.

  2. Name the database (e.g., "la-db-spanner").

  3. Click Continue.

  4. In the Define your database schema section, click Edit as text.

  5. In the DDL statements box, enter the following code:

    CREATE TABLE Artists (
    ArtistId INT64 NOT NULL,
    FirstName STRING(1024),
    LastName STRING(1024),
    ArtistInfo BYTES(MAX),
    BirthDate DATE )
    PRIMARY KEY(ArtistId);
  6. Click Create.

Populate table.

  1. Select Artists.
  2. Review the schema.
  3. Click Data.
  4. Click Insert.
  5. Enter the following records, clicking Save after each one and then Insert:

    ArtistId = 100
    FirstName = Paul
    LastName = Cezanne
    BirthDate = 1839-01-19
    ArtistId = 101
    FirstName = Ansel
    LastName = Adams
    BirthDate = 1902-02-20
    ArtistId = 102
    FirstName = Frida
    LastName = Kahloe
    BirthDate = 1907-07-06

Editing data.

  1. Select the checkbox next to the Frida Kahloe record.
  2. Choose Edit.
  3. Change the LastName to Kahlo.
  4. Click Save.

Query data.

  1. Click Query.

  2. In the Query field, enter:

    SELECT * from Artists WHERE BirthDate > '1900-01-01'
  3. Click Run query.

  4. Review results.

Export data.

  1. First, we need to create a bucket. Navigate to Storage (via the search bar at the top of the console).
  2. Click Create Bucket.
  3. Provide the following details:
    • Name: la-spanner-export-<INSERT NUMBERS OR LETTERS HERE> (must be unique across all Cloud Storage)
    • Default storage class: Regional
    • Location: us-central1
  4. Click Create.
  5. Navigate back to Spanner.
  6. Choose database instance from left sidebar.
  7. From the context menu, select Export.
  8. Provide the following details:
    • Choose where to store your export: Browse and select the newly created bucket
    • Choose a database to export: Select your database
    • Choose a region for the export job: us-central1
  9. Click Confirm charges checkbox.
  10. Click Export.
  11. Visit Cloud Spanner to view progress of operation.
  12. Visit Cloud Storage bucket to confirm operation.


Congratulations on completing this lab!