title | description | services | keywords | author | manager | ms.author | ms.date | ms.topic | ms.service |
---|---|---|---|---|---|---|---|---|---|
Deploy Azure Stream Analytics with Azure IoT Edge | Microsoft Docs |
Deploy Azure Stream Analytics as a module to an edge device |
iot-edge |
msebolt |
timlt |
v-masebo |
11/15/2017 |
article |
iot-edge |
IoT devices can produce large quantities of data. Sometimes this data has to be analyzed or processed before reaching the cloud to reduce the size of uploaded data or to eliminate the round-trip latency of an actionable insight.
Azure Stream Analytics (ASA) provides a richly structured query syntax for data analysis both in the cloud and on IoT Edge devices. For more information about ASA on IoT Edge, see ASA documentation.
This tutorial walks you through the creation of an Azure Stream Analytics job, and its deployment on an IoT Edge device in order to process a local telemetry stream directly on the device, and generate alerts to drive immediate action on the device. There are two modules involved in this tutorial. A simulated temperature sensor module (tempSensor) that generates temperature data from 20 to 120 degrees, incremented by 1 every 5 seconds, and an ASA module that filters out temperatures greater than 100 degrees. The ASA module also resets the tempSensor when the 30 seconds average reaches 100.
You learn how to:
[!div class="checklist"]
- Create an ASA job to process data on the Edge
- Connect the new ASA job with other IoT Edge modules
- Deploy the ASA job to an IoT Edge device
- An IoT Hub
- The device that you created and configured in the quickstart or in Deploy Azure IoT Edge om a simulated device in Windows and Linux.
- Docker on your IoT Edge device
- Install Docker on Windows and make sure it's running.
- Install Docker on Linux and make sure it's running.
- Python 2.7.x on your IoT Edge device
- Install Python 2.7 on Windows.
- Most Linux distributions, including Ubuntu, already have Python 2.7 installed. Use the following command to make sure pip is installed:
sudo apt-get install python-pip
.
Note
Note your device connection string and IoT Edge device ID will be necessary for this tutorial.
IoT Edge takes advantage of pre-built Azure Service IoT Edge modules for quick deployment and Azure Stream Analytics (ASA) is one such module. You can create an ASA job from its portal, then come to IoT Hub portal to deploy it as an IoT Edge Module.
For more information on Azure Stream Analytics, see the Overview section of the Stream Analytics Documentation.
In this section, you create an Azure Stream Analytics job to take data from your IoT hub, query the sent telemetry data from your device, and forward the results to an Azure Storage Container (BLOB). For more information, see the Overview section of the Stream Analytics Documentation.
Note
An Azure Storage account is required to provide an endpoint to be used as an output in your ASA job. The example below uses the BLOB storage type. For more information, see the Blobs section of the Azure Storage Documentation.
-
In the Azure portal, navigate to Create a resource -> Storage, click See all, and click Storage account - blob, file, table, queue.
-
Enter a name for your storage account, and select the same location where your IoT Hub is stored. Click Create. Note the name for later.
-
In the Azure portal, navigate to the storage account that you just created. Click Browse blobs under Blob Service.
-
Create a new container for the ASA module to store data. Set the access level to Container. Click OK.
-
In the Azure portal, navigate to Create a resource > Internet of Things and select Stream Analytics Job.
-
Enter a name, choose Edge as the Hosting environment, and use the remaining default values. Click Create.
[!NOTE] Currently, there are a limited number of regions that support ASA jobs on IoT Edge. Select one of the following as the location: Central US, West Central US, North Central US, East US, Canada East, Japan East, East Asia, North Europe, UK South, Australia East, Brazil South.
-
Go into the created job, under Job Topology, select Inputs, click Add.
-
Enter name
temperature
, choose Data stream as the source type, and use defaults for the other parameters. Click Create.[!NOTE] Additional inputs can include IoT Edge specific endpoints.
-
Under Job Topology, select Outputs, click Add.
-
Enter name
alert
and use defaults. Click Create. -
Under Job Topology, select Query, and enter the following:
SELECT 'reset' AS command INTO alert FROM temperature TIMESTAMP BY timeCreated GROUP BY TumblingWindow(second,30) HAVING Avg(machine.temperature) > 100
You are now ready to deploy the ASA job on your IoT Edge device.
-
In the Azure portal, in your IoT Hub, navigate to IoT Edge (preview) and open your {deviceId}'s blade.
-
Select Set modules, then select Import Azure Service IoT Edge Module.
-
Select the subscription and the ASA Edge job that you created. Then select your storage account. Click Save.
-
Click Add IoT Edge Module to add the temperature sensor module. Enter tempSensor for name,
microsoft/azureiotedge-simulated-temperature-sensor:1.0-preview
for Image URL. Leave the other settings unchanged, and click Save. -
Copy the name of the ASA module. Click Next to configure routes.
-
Copy the following to Routes. Replace {moduleName} with the module name you copied:
{ "routes": { "telemetryToCloud": "FROM /messages/modules/tempSensor/* INTO $upstream", "alertsToCloud": "FROM /messages/modules/{moduleName}/* INTO $upstream", "alertsToReset": "FROM /messages/modules/{moduleName}/* INTO BrokeredEndpoint(\"/modules/tempSensor/inputs/control\")", "telemetryToAsa": "FROM /messages/modules/tempSensor/* INTO BrokeredEndpoint(\"/modules/{moduleName}/inputs/temperature\")" } }
-
Click Next.
-
In the Review Template step, click Submit.
-
Return to the device details page and click Refresh. You should see the new {moduleName} module running along with the IoT Edge agent module and the IoT Edge hub.
Now you can go to your IoT Edge device to check out the interaction between the ASA module and the tempSensor module.
-
At a command prompt, configure the runtime with your IoT Edge device connection string:
iotedgectl setup --connection-string "{device connection string}" --auto-cert-gen-force-no-passwords
-
Run the command to start the runtime:
iotedgectl start
-
Run the command to see the modules running:
docker ps
-
Run the command to see all system logs and metrics data. Use the module name from above:
docker logs -f {moduleName}
-
In the Azure portal, in your Storage account, under Blob Service, click Browse blobs, select your container, and select newly created JSON file.
-
Click Download and view the results.
In this tutorial, you configured an Azure Storage container and a Streaming Analytics job to analyze data from your IoT Edge device. You then loaded a custom ASA module to move data from your device, through the stream, into a BLOB for download. You can continue on to other tutorials to further see how Azure IoT Edge can create solutions for your business.
[!div class="nextstepaction"] Deploy an Azure Machine Learning model as a module