Real Time Machine Learning with Cloud Dataflow and Vertex AI

Join Sign in

Real Time Machine Learning with Cloud Dataflow and Vertex AI

1 hour 30 minutes 5 Credits


Google Cloud self-paced labs logo


In this lab you implement a real-time, streaming, machine learning (ML) pipeline that uses Cloud Dataflow and Vertex AI.

Dataflow is a managed service for executing a wide variety of data processing patterns.

Vertex AI is a unified ML platform used to build, deploy, and scale ML models with pre-trained and custom tooling within a unified artificial intelligence platform.

The historical dataset this lab uses is from the US Bureau of Transport Statistics website and provides historic information about internal flights in the United States.

This lab uses a set of code samples and scripts developed for the Data Science on Google Cloud Platform, 2nd Edition from O'Reilly Media, Inc.


  • Configure and execute a real time flight event simulation in Python

  • Configure and deploy a streaming Google Cloud Dataflow job to provide real-time flight delay predictions

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

Task 1. Clone the Data Science on Google Cloud repository

  1. In the Cloud Console, on the Navigation menu (Navigation menu icon), click Compute Engine > VM instances.

  2. Click the SSH button inline with lab-vm-ql VM to launch and connect to a terminal. This terminal is called terminal-1.

  3. Click Connect to confirm the connection.

  4. In terminal-1, enter the following command to clone the repository:

git clone
  1. Navigate to the repository source directory for this lab:

cd ~/data-science-on-gcp/11_realtime
  1. Install the required packages:

pip3 install google-cloud-aiplatform cloudml-hypertune pyfarmhash tensorflow==2.8.0 pip3 install kfp 'apache-beam[gcp]'

Task 2. Machine learning training dataset

  1. Run a Dataflow pipeline to create the training dataset:

export PROJECT_ID=$(gcloud info --format='value(config.project)') export BUCKET=$PROJECT_ID-ml python3 --input bigquery --project $PROJECT_ID --bucket $BUCKET --region us-central1 Note: If you see an error related to IAM permission(s) after running this step, wait a couple of minutes and retry the command.

While the pipeline is running, use the Google Cloud Console to monitor the progress of the job.

  1. In the Cloud Console, on the Navigation menu (Navigation menu icon), click More Products > Dataflow.

  2. Click the name of the Dataflow job to open the job details page. This lets you monitor the progress of your job.

Note: It will take around 15-20 minutes to complete the job.

Task 3. Train the model

Run a script to train the model. For more information about the script, see Chapter 10, Data Science on Google Cloud Platform, 2nd Edition.

  1. Run script that copies over the Ch10 and files and makes the necessary changes:

  1. Train custom ML model on the enriched dataset:

python3 --project $PROJECT_ID --bucket $BUCKET --region us-central1 --develop --cpuonly Note: If you see an error related to IAM permission(s) after running this step, wait a couple of minutes and retry it.
  1. In the Cloud Console, on the Navigation menu, click Vertex AI > Training to monitor the training pipeline.

  2. When the status is Finished, click on the training pipeline name to monitor the deployment status.

Note: It will take around 20 minutes to complete the model training and deployment.

Click Check my progress to verify the objective. Check for ai-platform model called flights

  1. In terminal-1, create a small, local sample of BigQuery datasets for local experimentation:

bq query --nouse_legacy_sql --format=sparse \ "SELECT EVENT_DATA FROM dsongcp.flights_simevents WHERE EVENT_TYPE = 'wheelsoff' AND EVENT_TIME BETWEEN '2015-03-01' AND '2015-03-02'" \ | grep FL_DATE \ > simevents_sample.json
  1. Run a local pipeline to invoke predictions:

python3 --input local -p $PROJECT_ID
  1. Verify the results:

cat /tmp/predictions*

Task 4. Stream data through your pipeline

  1. In Cloud Console, click the SSH button next to lab-vm-ql VM to launch another terminal. This terminal is terminal-2.

  2. Click Connect to confirm the connection.

  3. In terminal-2, run the following to start the flight simulation script:

cd ~/data-science-on-gcp/04_streaming/simulate export PROJECT_ID=$(gcloud info --format='value(config.project)') python3 ./ --startTime '2015-02-01 00:00:00 UTC' --endTime '2015-03-03 00:00:00 UTC' --speedFactor=30 --project $PROJECT_ID

This script generates flight data events using the real flight data from 2015.

  1. In terminal-1, run the real-time prediction Dataflow job:

python3 --input pubsub --output bigquery --project $PROJECT_ID --bucket $BUCKET --region us-central1
  1. In the Cloud Console, from the Navigation menu, click Dataflow, then the job name to monitor the running pipeline.

Click Check my progress to verify the objective. Check that the real-time prediction Dataflow job is running

  1. With the pipeline running, navigate to the BigQuery console and verify that flight information is indeed getting streamed in.

  • In the Cloud Console, click Navigation menu > BigQuery.

  • In the Query Editor, add the following query:

SELECT * FROM dsongcp.streaming_preds ORDER BY event_time DESC LIMIT 10
  1. If the query has an error (red exclamation point) wait a couple more minutes until the query check passes (green check mark) then click RUN.

When the script runs, you see data being written into this BigQuery table, the real-time prediction service running on Cloud Dataflow is generating predictions in real-time using the flight data generated by the script.

Click Check my progress to verify the objective. Check for real time predictions in BigQuery


You implemented a real-time, streaming machine learning pipeline that uses Cloud Dataflow and Vertex AI.

Finish your quest

This self-paced lab is part of the Data Science on Google Cloud: Machine Learning quest. A quest is a series of related labs that form a learning path. Completing this quest earns you a badge 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 any quest that contains this lab and get immediate completion credit. See the Google Cloud Skills Boost catalog to see all available quests.

Take your next lab

This is the last lab in this Quest! If you did not take the labs in order, continue with another lab in this quest, for example:

Or, continue your learning with another quest, for example Advanced ML: ML Infrastructure.

Next steps / learn more

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 September 28, 2022

Lab Last Tested June 16, 2022

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.