- Lab
- A Cloud Guru
Handling Streaming Messages with Cloud Pub/Sub
One of the primary benefits of Cloud Pub/Sub is its ability to handle streaming data, as well as occasional and batch data. Streaming data could come from many sources, including multiple Internet of Things devices. In this hands-on lab, we’ll learn how to set up a Cloud Pub/Sub topic and subscription, simulate streaming data from traffic sensors, and pull multiple records of data from the subscription.
Path Info
Table of Contents
-
Challenge
Create a Topic
- From the main navigation menu, select Pub/Sub > Topics.
- Click Create a topic.
- Enter a name for the topic, such as "la-streaming-topic".
- Keep the Encryption option at its default setting.
- Click Create Topic.
-
Challenge
Create a Subscription
- Drill down into the topic we just created and choose Create Subscription from the bottom of the page in the Subscriptions section.
- Enter a name for the subscription, such as "la-streaming-subscription".
- The Cloud Pub/Sub topic must be selected
- Set Delivery Type to Pull.
- Under Retain acknowledged messages, click the Enable option.
- Leave all the other options as their defaults.
- Click Create.
-
Challenge
Retrieve the Files
- From the top navigation, click Activate Cloud Shell.
- In the Cloud Shell, enter the following command to clone the GitHub repository: git clone https://github.com/ACloudGuru-Resources/training-data-analyst.git
- Change to the
training-data-analyst/courses/streaming/publish
directory: cd training-data-analyst/courses/streaming/publish - Copy the data file from a Cloud Storage bucket: gsutil cp gs://cloud-training-demos/sandiego/sensor_obs2008.csv.gz .
- Open the Cloud Shell code editor.
- Review the file
send_sensor_data.py
in thetraining-data-analyst/courses/streaming/publish
folder. - On line 26, change the
TOPIC
variable fromsandiego
to the last part of your topic name (following the final '/'). - Save the file.
-
Challenge
Stream Data and Confirm Operation
- Enable the Resource Manager API
gcloud services enable cloudresourcemanager.googleapis.com
- Authenticate the shell with the following code:
gcloud auth application-default login --no-launch-browser
- Click the generated link to confirm the authentication.
- Execute the following command to install the Google Cloud Pub/Sub library:
sudo pip3 install google-cloud-pubsub
- Execute the following code to simulate streaming data:
./send_sensor_data.py --speedFactor=60 --project=[PROJECT_ID]
- Create a new Cloud Shell instance by clicking the plus (
+
) icon. - Change directory to the working folder:
cd training-data-analyst/courses/streaming/publish
- Pull the messages from the subscription with the following command:
gcloud pubsub subscriptions pull --auto-ack [SUBSCRIPTION_NAME] --limit=25
- Enable the Resource Manager API
What's a lab?
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Provided environment for hands-on practice
We will provide the credentials and environment necessary for you to practice right within your browser.
Guided walkthrough
Follow along with the author’s guided walkthrough and build something new in your provided environment!
Did you know?
On average, you retain 75% more of your learning if you get time for practice.