arrow_back

Programming Spanner Applications with Python

Sign in Join
Quick tip: Review the prerequisites before you run the lab
Use an Incognito or private browser window to run this lab. This prevents any conflicts between your personal account and the student account, which may cause extra charges incurred to your personal account.
Test and share your knowledge with our community!
done
Get access to over 700 hands-on labs, skill badges, and courses

Programming Spanner Applications with Python

Lab 2 hours universal_currency_alt 1 Credit show_chart Introductory
info This lab may incorporate AI tools to support your learning.
Test and share your knowledge with our community!
done
Get access to over 700 hands-on labs, skill badges, and courses

SCBL005

Overview

In this lab, you run the Python code to create Spanner instances and databases. You also see how to create, retrieve, and delete records from databases using both the Google Standard SQL and PostgreSQL dialects.

Objectives

In this lab, you learn how to:

  • Use Python to create and delete Spanner instances and databases.
  • Program Spanner databases that use the PostgreSQL dialect.

Setup and Requirements

Before you click the Start Lab button

Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources will be made available to you.

This hands-on lab lets you do the lab activities yourself in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials that you use to sign in and access Google Cloud for the duration of the lab.

To complete this lab, you need:

  • Access to a standard internet browser (Chrome browser recommended).
Note: Use an Incognito or private browser window to run this lab. This prevents any conflicts between your personal account and the Student account, which may cause extra charges incurred to your personal account.
  • Time to complete the lab---remember, once you start, you cannot pause a lab.
Note: If you already have your own personal Google Cloud account or project, do not use it for this lab to avoid extra charges to your account.

Activate Cloud Shell

Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources.

  1. Click Activate Cloud Shell Activate Cloud Shell icon at the top of the Google Cloud console.

When you are connected, you are already authenticated, and the project is set to your PROJECT_ID. The output contains a line that declares the PROJECT_ID for this session:

Your Cloud Platform project in this session is set to YOUR_PROJECT_ID

gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.

  1. (Optional) You can list the active account name with this command:
gcloud auth list
  1. Click Authorize.

  2. Your output should now look like this:

Output:

ACTIVE: * ACCOUNT: student-01-xxxxxxxxxxxx@qwiklabs.net To set the active account, run: $ gcloud config set account `ACCOUNT`
  1. (Optional) You can list the project ID with this command:
gcloud config list project

Output:

[core] project = <project_ID>

Example output:

[core] project = qwiklabs-gcp-44776a13dea667a6 Note: For full documentation of gcloud, in Google Cloud, refer to the gcloud CLI overview guide.

Task 1. Programming Spanner databases with Python

Create Dataflow Workbench Instance

  1. Using the Navigation menu in the Google Cloud console, select Dataflow > Workbench from the Analytics section.

Tip: You can also search for Dataflow Workbench using the Search box in the Console toolbar.

  1. If the Enable Notebooks API link appears, click the link to activate the API.

  2. From the Workbench page, click the Create New button.

    Name the Notebook my-notebook, choose the region. You may choose any zone for this region.
  3. Click Machine type from the list on the left, select E2 standard and e2-standard-2 for the Machine type.

  4. Leave the remaining fields at their default and click Create.

  5. When the instance is ready, click the Open Jupyter link. This opens Jupyter in another browser tab.

    On the Launcher tab that is open, scroll down (if necessary) and click Terminal.

    Run the following command to clone the Git repository that contains the files needed for this lab:
git clone https://github.com/GoogleCloudPlatform/training-data-analyst

Open Jupyter Notebook

  1. In the file explorer on the left, navigate to training-data-analyst/courses/understanding-spanner/colab-notebooks/Spanner_Samples_Python.ipynb and open that file.

  2. Run the first cell to install the Python Spanner client library.

  3. In the second cell, update the following variables as listed:

Variable New Value (In the notebook, please leave the outside single quotes)
project_id
region_id
  1. Now run the second cell. In this cell, some variables are created and the Spanner API is enabled.

  2. Read the text prior to each code cell and run each one. Take the time to understand what the code is doing.

Task 2. Programming Spanner databases using the PostgreSQL dialect

  1. Open the file: training-data-analyst/courses/understanding-spanner/colab-notebooks/Spanner_PostgeSQL.ipynb.

  2. Run the first cell to confirm the Python Spanner client library is active.

  3. In the second cell, update the following variables as listed:

Variable New Value (In the notebook, please leave the outside single quotes)
project_id
region_id
  1. Now run the second cell. In this cell, some variables are created and the Spanner API is enabled.

  2. Examine and run each of the steps in the notebook.

Congratulations! You have run the Python code to create Spanner instances and databases. You also saw how to create, retrieve, and delete records from databases using both the Google Standard SQL and PostgreSQL dialects.

End your lab

When you have completed your lab, click End Lab. Your account and the resources you've used are removed from the lab platform.

You will be given an opportunity to rate the lab experience. Select the applicable number of stars, type a comment, and then click Submit.

The number of stars indicates the following:

  • 1 star = Very dissatisfied
  • 2 stars = Dissatisfied
  • 3 stars = Neutral
  • 4 stars = Satisfied
  • 5 stars = Very satisfied

You can close the dialog box if you don't want to provide feedback.

For feedback, suggestions, or corrections, please use the Support tab.

Copyright 2023 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.

Before you begin

  1. Labs create a Google Cloud project and resources for a fixed time
  2. Labs have a time limit and no pause feature. If you end the lab, you'll have to restart from the beginning.
  3. On the top left of your screen, click Start lab to begin

Use private browsing

  1. Copy the provided Username and Password for the lab
  2. Click Open console in private mode

Sign in to the Console

  1. Sign in using your lab credentials. Using other credentials might cause errors or incur charges.
  2. Accept the terms, and skip the recovery resource page
  3. Don't click End lab unless you've finished the lab or want to restart it, as it will clear your work and remove the project

This content is not currently available

We will notify you via email when it becomes available

Great!

We will contact you via email if it becomes available

One lab at a time

Confirm to end all existing labs and start this one

Use private browsing to run the lab

Use an Incognito or private browser window to run this lab. This prevents any conflicts between your personal account and the Student account, which may cause extra charges incurred to your personal account.