Storing Stream Data from IoT Hub into Azure Cosmos DB

In my earlier post, I had discussed on how to integrate Event Hubs/IoT Hubs with Power BI via Stream Analytics . There were few questions on how to store the stream data in storage account. Storing stream data without proper data management and retention policies can incur huge costs to the organisation.We will not explore data management best practices in this post, but we will discuss one of the options to store stream data.

Event stream data can be structured or unstructured and since Azure Cosmos DB is a fully managed globally distributed multi-model data service with capabilities of low latency, 99.99% high availability, it is one of the better candidates to store streaming data.

In this post, we will store IoT Hub events hub data to Azure Cosmos DB via stream analytics. This data can be used by admin teams to monitor IoT Devices using tools like Power BI.

Pre-requisites

  • An Azure Subscription.
  • IoT Hub Should be up and running.
  • Events Data received by IoT Hub should have below Schema:
{
   "MachineName":"AB01",
   "ServerName":"XXXYYZZZZ01",
   "Process":"CNC",
   "LastStatus":3,
   "Status":"Connect",
   "Time":"2020-05-30T16:18:31.7058659Z"
}

Create Stream Analytics Account

  • Search for Stream Analytics
  • Click on Add
  • Provide details and click on Create.

Create Input Dataset

  • Go to Resource
  • Click on Inputs > Add stream input > IoT Hub
  • Provide details and click on Save

Create Azure Cosmos DB Storage Account

  • Search for Azure Cosmos DB
  • Click on Add
  • Provide Details and Click on Review + Create

Create Query to Save Data into Output

  • Go to Resource
  • Click on Outputs > Add stream Output > Azure Cosmos DB
  • Provide Details and Click on Save
  • Click on Query
  • Type Below sample query and Click on Save Query
SELECT MachineName,ServerName,Process,LastStatus,Status,Time
INTO [CosmosOutput]
FROM  [IoTIn]

In this post, we learnt on how to save stream data from IoT Hub to Azure Cosmos DB. You may want to explore other data storage options as per your project needs. Do let me know your questions in the comments section.

Keep Learning!!!



Categories: Azure, Azure IoT

Tags: , , , ,

Leave a Reply