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.
Learning Objectives
Successfully complete this lab by achieving the following learning objectives:
- 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.
- 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.
- 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.
- 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