arrow_back

Dataflow: Qwik Start - Python

Join Sign in

Dataflow: Qwik Start - Python

30 minutes 1 Credit

GSP207

Google Cloud selp-paced labs logo

Overview

In this lab you will set up your Python development environment, get the Cloud Dataflow SDK for Python, and run an example pipeline using the Cloud Console.

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.

How to start your lab and sign in to the Google Cloud Console

  1. Click the Start Lab button. If you need to pay for the lab, a pop-up opens for you to select your payment method. On the left is the Lab Details panel with the following:

    • The Open Google Console button
    • Time remaining
    • The temporary credentials that you must use for this lab
    • Other information, if needed, to step through this lab
  2. Click Open Google Console. The lab spins up resources, and then opens another tab that shows the Sign in page.

    Tip: Arrange the tabs in separate windows, side-by-side.

    Note: If you see the Choose an account dialog, click Use Another Account.
  3. If necessary, copy the Username from the Lab Details panel and paste it into the Sign in dialog. Click Next.

  4. Copy the Password from the Lab Details panel and paste it into the Welcome dialog. Click Next.

    Important: You must use the credentials from the left panel. Do not use your Google Cloud Skills Boost credentials. Note: Using your own Google Cloud account for this lab may incur extra charges.
  5. Click through the subsequent pages:

    • Accept the terms and conditions.
    • Do not add recovery options or two-factor authentication (because this is a temporary account).
    • Do not sign up for free trials.

After a few moments, the Cloud Console opens in this tab.

Note: You can view the menu with a list of Google Cloud Products and Services by clicking the Navigation menu at the top-left. Navigation menu icon

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. In the Cloud Console, in the top right toolbar, click the Activate Cloud Shell button.

Cloud Shell icon

  1. Click Continue.

It takes a few moments to provision and connect to the environment. 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

(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 For full documentation of gcloud, in Google Cloud, Cloud SDK documentation, see the gcloud command-line tool overview.

Create a Cloud Storage bucket

  1. In the Cloud Console, click on Navigation menu and then click on Cloud Storage.

  2. Click Create bucket.

  3. In the Create bucket dialog, specify the following attributes:

  • Name: A unique bucket name. Do not include sensitive information in the bucket name, as the bucket namespace is global and publicly visible.

  • Location type: Region

  • Location: us-central1

  • A location where bucket data will be stored.

  1. Click Create.

Test Completed Task

Click Check my progress to verify your performed task. If you have completed the task successfully you will granted with an assessment score.

Create a Cloud Storage bucket.

Install pip and the Cloud Dataflow SDK

  1. The Cloud Dataflow SDK for Python requires Python version 3.7.

To ensure you are running the process with the correct version, run the Python3.7 Docker Image:

docker run -it -e DEVSHELL_PROJECT_ID=$DEVSHELL_PROJECT_ID python:3.7 /bin/bash

This command pulls a Docker container with the latest stable version of Python 3.7 and then opens up a command shell for you to run the following commands inside your container.

  1. After the container is running, install the latest version of the Apache Beam for Python by running the following command from a virtual environment:

pip install apache-beam[gcp]

You will see some warnings returned that are related to dependencies. It is safe to ignore them for this lab.

  1. Run the wordcount.py example locally by running the following command:

python -m apache_beam.examples.wordcount --output OUTPUT_FILE

You may see a message similar to the following:

INFO:root:Missing pipeline option (runner). Executing pipeline using the default runner: DirectRunner. INFO:oauth2client.client:Attempting refresh to obtain initial access_token

This message can be ignored.

You can now list the files that are on your local cloud environment to get the name of the OUTPUT_FILE:

ls

Copy the name of the OUTPUT_FILE and cat into it:

cat <file name>

Your results show each word in the file and how many times it appears.

Run an Example Pipeline Remotely

Set the BUCKET environment variable to the bucket you created earlier.

BUCKET=gs://<bucket name provided earlier>

Now you'll run the wordcount.py example remotely:

python -m apache_beam.examples.wordcount --project $DEVSHELL_PROJECT_ID \ --runner DataflowRunner \ --staging_location $BUCKET/staging \ --temp_location $BUCKET/temp \ --output $BUCKET/results/output \ --region us-central1

In your output, wait until you see the message:

JOB_MESSAGE_DETAILED: Workers have started successfully.

Then continue with the lab.

Check that your job succeeded

Open the navigation menu and select Dataflow from the list of services:

58c1c356b9c07c37.png

You should see your wordcount job with a status of Running at first.

Click on the name to watch the process. When all the boxes are checked off, you can continue watching the logs in Cloud Shell.

The process is complete when the status is Succeeded:

b762c1ec75fdc2e8.png

Test Completed Task

Click Check my progress to verify your performed task. If you have completed the task successfully you will granted with an assessment score.

Run an Example Pipeline Remotely.

Click on Cloud Storage in the Console.

b91e6a77309137cf.png

Click on the name of your bucket. In your bucket, you should see the results and staging directories:

44c6038c0f40e62e.png

Click on the results folder and you should see the output files that your job created:

61623f437d9ddedc.png

Click on a file to see the word counts it contains.

Test your Understanding

Below are a multiple choice questions to reinforce your understanding of this lab's concepts. Answer them to the best of your abilities.

Congratulations!

4212cb7c1b865097.png

Finish Your Quest

Continue your Quest with Baseline: Data, ML, AI. A Quest is a series of related labs that form a learning path. Completing this Quest earns you the badge above, to recognize your achievement. You can make your badge (or badges) public and link to them in your online resume or social media account. Enroll in this Quest and get immediate completion credit if you've taken this lab. See other available Qwiklabs Quests.

Next Steps / Learn More

This lab is part of a series of labs called Qwik Starts. These labs are designed to give you a little taste of the many features available with Google Cloud. Search for "Qwik Starts" in the lab catalog to find the next lab you'd like to take!

To get your own copy of the book this lab is based on: Data Science on the Google Cloud Platform: O'Reilly Media, Inc.

Google Cloud Training & Certification

...helps you make the most of Google Cloud technologies. Our classes include technical skills and best practices to help you get up to speed quickly and continue your learning journey. We offer fundamental to advanced level training, with on-demand, live, and virtual options to suit your busy schedule. Certifications help you validate and prove your skill and expertise in Google Cloud technologies.

Manual Last Updated March 1, 2022
Lab Last Tested March 1, 2022

Copyright 2022 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.