Setting Up for Google Cloud Spanner

Hands-On Lab

 

Photo of Joseph Lowery

Joseph Lowery

Google Cloud Training Architect II in Content

Length

00:30:00

Difficulty

Beginner

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 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 API.

  1. Navigate to APIs & Libraries > Libraries.
  2. Search for "Cloud Spanner".
  3. Select the Cloud Spanner API card.
  4. Click Enable.
  5. Head back to APIs & Libraries > Libraries.
  6. Search for "dataflow".
  7. Select the Dataflow API card.
  8. Click Enable.
  9. One last time, head to APIs & Libraries > Libraries.
  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), <-Binary data
    BirthDate DATE, )
    PRIMARY KEY(ArtistId);
  6. Click Create.

Populate table.

  1. Select Artists in the left sidebar.
  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 = Kahlo
    Birthdate = 1907-07-06

Query data.

  1. Choose the database from the left column.

  2. Select Query.

  3. In the Query field, enter:

    Select * from Artists Where Birthdate > '1900-01-01’
  4. Click Run query.

  5. 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: la-db-spanner.
    • 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.

Conclusion

Congratulations on completing this lab!