Loading...
No results found.

Google Cloud Skills Boost

Apply your skills in Google Cloud console


Get access to 700+ labs and courses

ETL Processing on Google Cloud Using Dataflow and BigQuery (Python)

Lab 1 hour 30 minutes universal_currency_alt 5 Credits show_chart Intermediate
info This lab may incorporate AI tools to support your learning.
Get access to 700+ labs and courses

GSP290

Overview

Dataflow is a Google Cloud service that provides unified stream and batch data processing at scale. It is built on the Apache Beam project, which is an open source model for defining both batch and streaming data-parallel processing pipelines. Using one of the open source Apache Beam SDKs, you can build a program that defines the pipeline and then use Dataflow to execute the pipeline.

In this lab, you use the Apache Beam SDK for Python to build and run a pipeline in Dataflow to ingest data from Cloud Storage to BigQuery, and then transform and enrich the data in BigQuery.

Note: Be sure to open the Python files and read the comments when instructed. This provides an understanding of what the code does.

What you'll do

In this lab, you learn how to:

  • Build and run Dataflow pipelines (Python) to ingest data from Cloud Storage to BigQuery.
  • Build and run Dataflow pipelines (Python) to transform and enrich data in BigQuery.
  • Build and run Dataflow pipelines (Python) to join data in BigQuery and write the results to a new table.

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 are made available to you.

This hands-on lab lets you do the lab activities in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials 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 (recommended) or private browser window to run this lab. This prevents 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: Use only the student account for this lab. If you use a different Google Cloud account, you may incur charges to that 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 dialog opens for you to select your payment method. On the left is the Lab Details pane with the following:

    • The Open Google Cloud 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 Cloud console (or right-click and select Open Link in Incognito Window if you are running the Chrome browser).

    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 below and paste it into the Sign in dialog.

    {{{user_0.username | "Username"}}}

    You can also find the Username in the Lab Details pane.

  4. Click Next.

  5. Copy the Password below and paste it into the Welcome dialog.

    {{{user_0.password | "Password"}}}

    You can also find the Password in the Lab Details pane.

  6. Click Next.

    Important: You must use the credentials the lab provides you. Do not use your Google Cloud account credentials. Note: Using your own Google Cloud account for this lab may incur extra charges.
  7. 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 Google Cloud console opens in this tab.

Note: To access Google Cloud products and services, click the Navigation menu or type the service or product name in the Search field.

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 at the top of the Google Cloud console.

  2. Click through the following windows:

    • Continue through the Cloud Shell information window.
    • Authorize Cloud Shell to use your credentials to make Google Cloud API calls.

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 {{{project_0.project_id | "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.

Output:

ACTIVE: * ACCOUNT: {{{user_0.username | "ACCOUNT"}}} 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_0.project_id | "PROJECT_ID"}}} Note: For full documentation of gcloud, in Google Cloud, refer to the gcloud CLI overview guide.

Task 1. Ensure that the Dataflow API is successfully enabled

To ensure access to the necessary API, restart the connection to the Dataflow API.

Important: Even if the API is currently enabled, follow steps 1-4 below to disable, and then re-enable the API, to restart the API successfully.
  1. In the Google Cloud console title bar, type Dataflow API in the Search field and then click Dataflow API from the search results.

  2. Click Manage.

  3. Click Disable API.

If asked to confirm, click Disable.

  1. Click Enable.

When the API has been re-enabled, the page shows the Disable option.

Click Check my progress to verify your performed task.

Disable and re-enable the Dataflow API.

Task 2. Download the starter code

Download the Dataflow Python examples to use in this lab.

  1. Run the following command in the Cloud Shell to get Dataflow Python examples from Google Cloud's professional services GitHub:
gcloud storage cp -r gs://spls/gsp290/dataflow-python-examples .
  1. Set a variable for your Project ID.
export PROJECT={{{ project_0.project_id }}} gcloud config set project $PROJECT

Task 3. Create a Cloud Storage bucket and copy files to the bucket

In Cloud Shell, create a Cloud Storage bucket, and then copy files to the bucket. These files are the Dataflow Python examples.

Create a Cloud Storage bucket

  • Still in Cloud Shell, use the make bucket command to create a new regional bucket in the region within your project:
gcloud storage buckets create gs://$PROJECT --location={{{ project_0.default_region | REGION }}}

Click Check my progress to verify your performed task.

Create a Cloud Storage Bucket.

Copy files to your bucket

  • In Cloud Shell, use the gsutil command to copy files into the Cloud Storage bucket you just created:
gcloud storage cp gs://spls/gsp290/data_files/usa_names.csv gs://$PROJECT/data_files/ gcloud storage cp gs://spls/gsp290/data_files/head_usa_names.csv gs://$PROJECT/data_files/

Click Check my progress to verify your performed task.

Copy Files to Your Bucket.

Task 4. Create a BigQuery dataset

Create a dataset in BigQuery dataset. This is where your tables are loaded in BigQuery.

In Cloud Shell, create the dataset named lake:

bq mk lake

Click Check my progress to verify your performed task.

Create the BigQuery Dataset named lake.

Task 5. Review and run the data ingestion pipeline

In this task, you review the pipeline code to see how it works. You then set up and run the pipeline.

The data ingestion pipeline ingests data from Cloud Storage into the BigQuery table using a TextIO source and a BigQueryIO destination. Specifically, the pipeline:

  • Ingests the files from Cloud Storage.
  • Filters out the header row in the files.
  • Converts the lines read to dictionary objects.
  • Outputs the rows to BigQuery.

Review the Python code for the data ingestion pipeline

Use the Cloud Shell Code Editor to review the pipeline code.

  1. In the Cloud Shell menu bar, click Open Editor.

  2. Navigate to dataflow_python_examples > dataflow_python_examples, and open the data_ingestion.py file.

  3. Read the comments in the file, which explain what the code is doing.

This code populates a BigQuery table with the data files from Cloud Storage.

  1. To return to Cloud Shell, click Open Terminal.

Set up the Docker container for the Dataflow jobs

  1. Return to your Cloud Shell session to set up the required Python libraries.

The Dataflow jobs in this lab require Python3.8. To ensure you're on the proper version, run the Dataflow processes in a Python 3.8 Docker container.

  1. Run the following in Cloud Shell to start up a Python container:
cd ~ docker run -it -e PROJECT=$PROJECT -v $(pwd)/dataflow-python-examples:/dataflow python:3.8 /bin/bash

This command pulls a Docker container with the latest stable version of Python 3.8 and executes a command shell to run the next commands within the container. The -v flag provides the source code as a volume for the container so that we can edit in Cloud Shell editor and still access it within the running container.

  1. Once the container finishes pulling and starts executing in Cloud Shell, run the following to install apache-beam in that running container:
pip install apache-beam[gcp]==2.59.0
  1. Next, in the running container in the Cloud Shell, change directories into where you linked the source code:
cd dataflow/
  1. Set the project ID in the container:
export PROJECT={{{ project_0.project_id }}}

Run the data ingestion pipeline in the cloud

  1. Run the following code to execute the data ingestion pipeline:
python dataflow_python_examples/data_ingestion.py \ --project=$PROJECT \ --region={{{ project_0.default_region | REGION }}} \ --runner=DataflowRunner \ --machine_type=e2-standard-2 \ --staging_location=gs://$PROJECT/test \ --temp_location gs://$PROJECT/test \ --input gs://$PROJECT/data_files/head_usa_names.csv \ --save_main_session

This code spins up the workers required and then shut them down when the pipeline is complete.

  1. In the console title bar, type Dataflow in the Search field, and then click Dataflow in the search results.

When the Dataflow page opens, view the status of your job.

  1. Click the job's name to watch its progress.

Once your Job Status is Succeeded, you can move to the next step. This ingestion pipeline takes approximately five minutes to start, complete the work, and then shutdown.

  1. Navigate to BigQuery (Navigation menu > BigQuery) to see that your data has been populated.

  2. Click your project name to see the usa_names table under the lake dataset.

  1. Click the table then navigate to the Preview tab to see examples of the usa_names data.
Note: If you don't see the usa_names table, try refreshing the page or view the tables using the classic BigQuery UI.

Click Check my progress to verify your performed task.

Build a data ingestion pipeline.

Task 6. Review and run the data transformation pipeline

In this task, you review the data transformation pipeline to learn how it works. You then run the pipeline to process the Cloud Storage files and output the result to BigQuery.

The data transformation pipeline also ingests data from Cloud Storage into the BigQuery table using a TextIO source and a BigQueryIO destination, but with additional data transformations. Specifically, the pipeline:

  • Ingests the files from Cloud Storage.
  • Converts the lines read to dictionary objects.
  • Transforms the data which contains the year to a format BigQuery understands as a date.
  • Outputs the rows to BigQuery.

Review the Python code for the data transformation pipeline

  • In the Code Editor, open data_transformation.py.

Read the comments in the file, which explain what the code is doing.

Run the data transformation pipeline in the cloud

  1. Run the following code to run the data transformation pipeline:
python dataflow_python_examples/data_transformation.py \ --project=$PROJECT \ --region={{{ project_0.default_region | REGION }}} \ --runner=DataflowRunner \ --machine_type=e2-standard-2 \ --staging_location=gs://$PROJECT/test \ --temp_location gs://$PROJECT/test \ --input gs://$PROJECT/data_files/head_usa_names.csv \ --save_main_session
  1. In the Google Cloud console title bar, type Dataflow in the Search field and then click Dataflow from the search results.

  2. Click the name of this job to view the status of your job.

This Dataflow pipeline takes approximately five minutes to start, complete the work, and then shutdown.

  1. When your Job Status is Succeeded in the Dataflow Job Status screen, navigate to BigQuery to check to see that your data has been populated.

You should see the usa_names_transformed table under the lake dataset.

  1. Click the table and navigate to the Preview tab to see examples of the usa_names_transformed data.
Note: If you don't see the usa_names_transformed table, try refreshing the page or view the tables using the classic BigQuery UI.

Click Check my progress to verify your performed task.

Build a data transformation pipeline.

Task 7. Review and run the data enrichment pipeline

You now build a data enrichment pipeline that accomplishes the following:

  • Ingest the files from Cloud Storage.
  • Filter out the header row in the files.
  • Convert the lines read to dictionary objects.
  • Output the rows to BigQuery.

Review and edit the Python code for the data enrichment pipeline

  1. In the Code Editor, open data_enrichment.py.

  2. Check out the comments, which explain what the code is doing. This code populates the data in BigQuery.

Line 83 currently looks like:

values = [x.decode('utf8') for x in csv_row]
  1. Edit it to look like the following:
values = [x for x in csv_row]
  1. When you have finished editing this line, remember to Save this updated file by selecting the File option in the Code Editor and clicking Save.

Run the data enrichment pipeline

  1. Run the following code to execute the data enrichment pipeline:
python dataflow_python_examples/data_enrichment.py \ --project=$PROJECT \ --region={{{ project_0.default_region | REGION }}} \ --runner=DataflowRunner \ --machine_type=e2-standard-2 \ --staging_location=gs://$PROJECT/test \ --temp_location gs://$PROJECT/test \ --input gs://$PROJECT/data_files/head_usa_names.csv \ --save_main_session
  1. In the Dataflow page, click your job to view its Job status.

This Dataflow pipeline takes approximately five minutes to start, complete the work, and then shutdown.

  1. Once your Job Status is Succeed in the Dataflow Job Status screen, in the console click Navigation menu > BigQuery to check to see that your data has been populated.

You should see the usa_names_enriched table under the lake dataset.

  1. Click the table and navigate to the Preview tab to see examples of the usa_names_enriched data.
Note: If you don't see the usa_names_enriched table, try refreshing the page or view the tables using the classic BigQuery UI.

Click Check my progress to verify your performed task.

Build a data enrichment pipeline.

Task 8. Review and run the data lake to data mart pipeline

Now you build a Dataflow pipeline that reads data from two BigQuery data sources and then joins the data sources. Specifically, you:

  • Ingest files from two BigQuery sources.
  • Join the two data sources.
  • Filter out the header row in the files.
  • Convert the lines read to dictionary objects.
  • Output the rows to BigQuery.

Run the data ingestion pipeline to perform a data join and write the resulting table to BigQuery

You first review the data_lake_to_mart.py code to gain understanding of what it does. You then run the pipeline in the cloud.

  1. In the Code Editor, open data_lake_to_mart.py file.

Read the comments in the file, which explain what the code is doing. This code joins two tables and write the results to new table in BigQuery.

  1. Run the following code block to execute the pipeline:
python dataflow_python_examples/data_lake_to_mart.py \ --worker_disk_type="compute.googleapis.com/projects//zones//diskTypes/pd-ssd" \ --max_num_workers=4 \ --project=$PROJECT \ --runner=DataflowRunner \ --machine_type=e2-standard-2 \ --staging_location=gs://$PROJECT/test \ --temp_location gs://$PROJECT/test \ --save_main_session \ --region={{{ project_0.default_region | REGION }}}
  1. In the Google Cloud console title bar, type Dataflow in the Search field, and then click Dataflow from the search results.

  2. Click this new job to view the status.

This Dataflow pipeline takes approximately five minutes to start, complete the work, and then shutdown.

  1. Once your Job Status is Succeeded in the Dataflow Job Status screen, click Navigation menu > BigQuery to check that your data has been populated.

You should see the orders_denormalized_sideinput table under the lake dataset.

  1. Click the table and navigate to the Preview section to see examples of orders_denormalized_sideinput data.
Note: If you don't see the orders_denormalized_sideinput table, try refreshing the page or view the tables using the classic BigQuery UI.

Click Check my progress to verify your performed task.

Build a Data lake to Mart Dataflow Pipeline

Test your understanding

Below is a multiple choice question to reinforce your understanding of this lab's concepts. Answer it to the best of your ability.

Congratulations!

You executed Python code using Dataflow to ingest data from Cloud Storage into BigQuery and then transform and enrich the data in BigQuery.

Next steps / Learn more

Looking for more? Check out official documentation on:

Google Cloud training and 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 April 1, 2025

Lab Last Tested April 1, 2025

Copyright 2025 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

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.