Skip to content

Contact sales

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

Query Real-Time IoT Data in Azure Stream Analytics

Learning how to set up an Azure Stream Analytics job is well-documented — and is arguably the least important part of planning for this IoT data processing option. On the other hand, developing the expertise to write meaningful and useful queries and transformations against real-time streaming data is well worth your time. But gathering enough meaningful data to mock an IoT solution at scale can be challenging. In this lab, we supply you with a robust sample of data and a series of real-world scenarios to help you practice some of the most practical and common IoT data query patterns.

Azure icon
Labs

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 1h 30m
Published
Clock icon Feb 25, 2022

Contact sales

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

Table of Contents

  1. Challenge

    Set Up Sample Data and Write a Query

    1. Log in to the Azure portal using lab credentials in a new InPrivate or incognito window.
    2. Download the mock data file as described in the Additional Resources section of this lab.
    3. Navigate to the Azure Stream Analytics job in the portal.
    4. Navigate to the Query, and look for Upload sample input to upload the JSON file with mock data. Once the data is uploaded, you should see sample data below the query pane, in the Input Preview tab. This may take several seconds.
    5. Select Test query to run the sample query. The query should return 1860 rows with the following 4 column headers: time, dspl, temp, hmdt.

      Note: The point of this lab is to write queries and not to test your ability to get the sample data loaded. If you have any trouble, at this point, go ahead and check the lab guide or the solution video.

    6. Write a query that aliases 3 of the 4 column names for better readability and returns data from sensorA.
      • Reasonable column names would be: time (no alias), SensorName, Temperature, and Humidity.
      • Correctly written, your query should return 389 rows in the Test results tab below the query pane.
  2. Challenge

    Write 3 Queries

    1. Building on your first query in the last objective, add a column called Status that returns the string Alert if the temperature is above 100 and the humidity is below 40. If it's not, it should return OK in the status column.

      Hints:

      • This query uses a CASE statement.
      • This query should return the same 389 rows as the first query challenge, but with an additional column that includes an alert when the described temperature and humidity conditions are present.
    2. Suppose you want to get the average temperature, every 1 minute, over all sensors. You want to display 2 columns: the timestamp representing the minute and the average temperature for that minute. Write a query that fulfills these requirements.

      Hints:

      • Apply the appropriate aggregation to the temp column, and use the tumbling window function set to group and report every 1 minute.
      • Use the TIMESTAMP BY time clause to use the time field from the sample data as the event timestamp.
      • This query should return 32 rows with average temperatures between 101 and 114 (rounded).
      • Because streaming data queries are typically interested in understanding the data output in timeframes, a good majority of the stream analytics queries will include one of a few windowing functions. See the Additional Resources section of this lab for a links to Azure Stream Analytics query documentation.
    3. In casual observation, you note that the sample data is a slice of data from a single day, and the messages are sent at inconsistent intervals. You want to get a better sense of the shape of the data by using overlapping intervals of time to smooth out anomalies, so you write a query to check every 1 minute for the count of messages sent over the last 5 minutes. You decide to limit your query to data from Sensor E.

      Write a query to return 2 columns: the timestamp representing the interval/event timestamp and a column with the count of events over the last 5 minutes.

      Hints:

      • Use a hopping window function to "hop" each minute, with a window size of 5 minutes.
      • Use the TIMESTAMP BY time clause to use the time field from the sample data as the event timestamp.
      • This query should return 36 rows, with timestamps 1 minute apart, and the count of messages sent in the previous 5 minutes.
      • Because streaming data queries are typically interested in understanding the data output in timeframes, a good majority of the stream analytics queries will include one of a few windowing functions. See the Additional Resources section of this lab for a links to Azure Stream Analytics query documentation.

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