Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Labs

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.

Labs

Path Info

Level
Clock icon Beginner
Duration
Clock icon 30m
Published
Clock icon Oct 04, 2019

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    Create a Topic

    1. From the main navigation menu, select Pub/Sub > Topics.
    2. Click Create a topic.
    3. Enter a name for the topic, such as "la-streaming-topic".
    4. Keep the Encryption option at its default setting.
    5. Click Create Topic.
  2. Challenge

    Create a Subscription

    1. Drill down into the topic we just created and choose Create Subscription from the bottom of the page in the Subscriptions section.
    2. Enter a name for the subscription, such as "la-streaming-subscription".
    3. The Cloud Pub/Sub topic must be selected
    4. Set Delivery Type to Pull.
    5. Under Retain acknowledged messages, click the Enable option.
    6. Leave all the other options as their defaults.
    7. Click Create.
  3. Challenge

    Retrieve the Files

    1. From the top navigation, click Activate Cloud Shell.
    2. In the Cloud Shell, enter the following command to clone the GitHub repository: git clone https://github.com/ACloudGuru-Resources/training-data-analyst.git
    3. Change to the training-data-analyst/courses/streaming/publish directory: cd training-data-analyst/courses/streaming/publish
    4. Copy the data file from a Cloud Storage bucket: gsutil cp gs://cloud-training-demos/sandiego/sensor_obs2008.csv.gz .
    5. Open the Cloud Shell code editor.
    6. Review the file send_sensor_data.py in the training-data-analyst/courses/streaming/publish folder.
    7. On line 26, change the TOPIC variable from sandiego to the last part of your topic name (following the final '/').
    8. Save the file.
  4. Challenge

    Stream Data and Confirm Operation

    1. Enable the Resource Manager API gcloud services enable cloudresourcemanager.googleapis.com
    2. Authenticate the shell with the following code: gcloud auth application-default login --no-launch-browser
    3. Click the generated link to confirm the authentication.
    4. Execute the following command to install the Google Cloud Pub/Sub library: sudo pip3 install google-cloud-pubsub
    5. Execute the following code to simulate streaming data: ./send_sensor_data.py --speedFactor=60 --project=[PROJECT_ID]
    6. Create a new Cloud Shell instance by clicking the plus (+) icon.
    7. Change directory to the working folder: cd training-data-analyst/courses/streaming/publish
    8. Pull the messages from the subscription with the following command: gcloud pubsub subscriptions pull --auto-ack [SUBSCRIPTION_NAME] --limit=25

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

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.

Start learning by doing today

View Plans