教程:将 Azure 流分析作为 IoT Edge 模块进行部署Tutorial: Deploy Azure Stream Analytics as an IoT Edge module

许多 IoT 解决方案使用分析服务来了解从 IoT 设备到达云的数据。Many IoT solutions use analytics services to gain insight about data as it arrives in the cloud from IoT devices. 使用 Azure IoT Edge 时,可以获取 Azure 流分析逻辑,将其转到设备中。With Azure IoT Edge, you can take Azure Stream Analytics logic and move it onto the device itself. 在边缘处理遥测流可以减少上传数据量,缩短对可操作见解进行响应的时间。By processing telemetry streams at the edge, you can reduce the amount of uploaded data and reduce the time it takes to react to actionable insights.

集成 Azure IoT Edge 和 Azure 流分析,以简化工作负载开发。Azure IoT Edge and Azure Stream Analytics are integrated to simplify your workload development. 可以在 Azure 门户中创建 Azure 流分析作业,然后将其部署为 IoT Edge 模块,无附加代码。You can create an Azure Stream Analytics job in the Azure portal and then deploy it as an IoT Edge module with no additional code.

Azure 流分析提供一种丰富结构化的查询语法,可用于在云和 IoT Edge 设备上进行数据分析。Azure Stream Analytics provides a richly structured query syntax for data analysis, both in the cloud and on IoT Edge devices. 有关详细信息,请参阅 Azure 流分析文档For more information, see Azure Stream Analytics documentation.

本教程中的流分析模块在一个滚动的 30 秒时段内计算平均温度。The Stream Analytics module in this tutorial calculates the average temperature over a rolling 30-second window. 当平均温度达到 70 时,模块会发送一个警报,以便设备采取操作。When that average reaches 70, the module sends an alert for the device to take action. 在这种情况下,该操作是重置模拟温度传感器。In this case, that action is to reset the simulated temperature sensor. 在生产环境中,当温度达到危险级别时,可以使用此功能关闭机器或采取预防措施。In a production environment, you might use this functionality to shut off a machine or take preventative measures when the temperature reaches dangerous levels.

在本教程中,你将了解如何执行以下操作:In this tutorial, you learn how to:

  • 在 Edge 上创建 Azure 流分析作业用于处理数据。Create an Azure Stream Analytics job to process data on the edge.
  • 将新的 Azure 流分析作业与其他 IoT Edge 模块相连接。Connect the new Azure Stream Analytics job with other IoT Edge modules.
  • 将 Azure 流分析作业从 Azure 门户部署到 IoT Edge 设备。Deploy the Azure Stream Analytics job to an IoT Edge device from the Azure portal.

关系图 - 教程体系结构:阶段和部署 ASA 作业

![Diagram - Tutorial architecture: stage and deploy ASA job](./media/tutorial-deploy-stream-analytics/asa-architecture.png)

如果没有 Azure 订阅,可在开始前创建一个 试用帐户If you don't have an Azure subscription, create a trial account before you begin.


Azure IoT Edge 设备:An Azure IoT Edge device:

  • 可以按照适用于 LinuxWindows 设备的快速入门中的步骤,将 Azure 虚拟机用作 IoT Edge 设备。You can use an Azure virtual machine as an IoT Edge device by following the steps in the quickstart for Linux or Windows devices.

云资源:Cloud resources:

  • Azure 中的免费或标准层 IoT 中心A free or standard-tier IoT Hub in Azure.

创建 Azure 流分析作业Create an Azure Stream Analytics job

在本部分,你将创建一个 Azure 流分析作业用于执行以下步骤:In this section, you create an Azure Stream Analytics job that will do the following steps:

  • 从 IoT Edge 设备接收数据。Receive data from your IoT Edge device.
  • 查询遥测数据中超出设置的范围的值。Query the telemetry data for values outside a set range.
  • 根据查询结果对 IoT Edge 设备采取措施。Take action on the IoT Edge device based on the query results.

创建存储帐户Create a storage account

创建可以在 IoT Edge 设备上运行的 Azure 流分析作业时,需采用特定的存储方式,以便能够从设备进行调用。When you create an Azure Stream Analytics job to run on an IoT Edge device, it needs to be stored in a way that can be called from the device. 可以使用现有的 Azure 存储帐户,也可以现在就创建一个新的。You can use an existing Azure Storage account, or create a new one now.

  1. 在 Azure 门户中,转到“创建资源” > “存储” > “存储帐户”。 In the Azure portal, go to Create a resource > Storage > Storage account.

  2. 提供以下值来创建存储帐户:Provide the following values to create your storage account:

    字段Field Value
    订阅Subscription 选择与 IoT 中心相同的订阅。Choose the same subscription as your IoT hub.
    资源组Resource group 建议对 IoT Edge 快速入门和教程中的所有测试资源使用同一资源组。We recommend that you use the same resource group for all of your test resources for the IoT Edge quickstarts and tutorials. 例如,IoTEdgeResourcesFor example, IoTEdgeResources.
    名称Name 为存储帐户提供唯一的名称。Provide a unique name for your storage account.
    位置Location 选择靠近你的位置。Choose a location close to you.
  3. 将其他字段保留默认值,然后选择“查看 + 创建”。 Keep the default values for the other fields and select Review + Create.

  4. 查看设置,然后选择“创建” 。Review your settings then select Create.

创建新作业Create a new job

  1. 在 Azure 门户中,转到“创建资源” > “物联网” > “流分析作业”。 In the Azure portal, go to Create a resource > Internet of Things > Stream Analytics job.

  2. 提供以下值来创建作业:Provide the following values to create your job:

    字段Field Value
    作业名称Job name 为作业提供一个名称。Provide a name for your job. 例如,IoTEdgeJobFor example, IoTEdgeJob
    订阅Subscription 选择与 IoT 中心相同的订阅。Choose the same subscription as your IoT hub.
    资源组Resource group 建议对在 IoT Edge 快速入门和教程中创建的所有测试资源使用同一资源组。We recommend that you use the same resource group for all of the test resources that you create during the IoT Edge quickstarts and tutorials. 例如,IoTEdgeResourcesFor example, IoTEdgeResources.
    位置Location 选择靠近你的位置。Choose a location close to you.
    宿主环境Hosting environment 选择“边缘”。 Select Edge.
  3. 选择“创建” 。Select Create.

配置作业Configure your job

在 Azure 门户中创建流分析作业以后,即可使用输入、输出以及要在流过的数据上运行的查询对其进行配置。Once your Stream Analytics job is created in the Azure portal, you can configure it with an input, an output, and a query to run on the data that passes through.

本部分使用三个元素(输入、输出和查询)创建一个可以从 IoT Edge 设备接收温度数据的作业。Using the three elements of input, output, and query, this section creates a job that receives temperature data from the IoT Edge device. 本部分在一个滚动的 30 秒窗口中分析该数据。It analyzes that data in a rolling 30-second window. 如果该窗口中的平均温度超出 70 度,则会向 IoT Edge 设备发送警报。If the average temperature in that window goes over 70 degrees, then an alert is sent to the IoT Edge device. 在下一部分部署作业时,需指定数据的具体来源和目标。You'll specify exactly where the data comes from and goes in the next section when you deploy the job.

  1. 导航到 Azure 门户中的流分析作业。Navigate to your Stream Analytics job in the Azure portal.

  2. 在“作业拓扑”下选择“输入”,然后选择“添加流输入”。 Under Job Topology, select Inputs then Add stream input.

    Azure 流分析 - 添加输入

  3. 从下拉列表中选择“边缘中心”。 Choose Edge Hub from the drop-down list.

  4. 在“新建输入” 窗格中,输入 temperature 作为输入别名。In the New input pane, enter temperature as the input alias.

  5. 将其他字段保留默认值,然后选择“保存”。 Keep the default values for the other fields, and select Save.

  6. 在“作业拓扑”下 打开“输出” ,然后选择“添加”。 Under Job Topology, open Outputs then select Add.

    Azure 流分析 - 添加输出

  7. 从下拉列表中选择“边缘中心”。 Choose Edge Hub from the drop-down list.

  8. 在“新建输出” 窗格中,输入 alert 作为输出别名。In the New output pane, enter alert as the output alias.

  9. 将其他字段保留默认值,然后选择“保存”。 Keep the default values for the other fields, and select Save.

  10. 在“作业拓扑”下选择“查询”。 Under Job Topology, select Query.

  11. 将默认文本替换为以下查询。Replace the default text with the following query. 如果计算机的平均温度在 30 秒的时限内达到 70 度,SQL 代码会向警报输出发送重置命令。The SQL code sends a reset command to the alert output if the average machine temperature in a 30-second window reaches 70 degrees. 重置命令已作为可执行的操作预先编程到传感器中。The reset command has been pre-programmed into the sensor as an action that can be taken.

        'reset' AS command
       temperature TIMESTAMP BY timeCreated
    GROUP BY TumblingWindow(second,30)
    HAVING Avg(machine.temperature) > 70
  12. 选择“保存”。 Select Save.

配置 IoT Edge 设置Configure IoT Edge settings

若要准备需部署到 IoT Edge 设备的流分析作业,需将作业与存储帐户中的容器关联起来。To prepare your Stream Analytics job to be deployed on an IoT Edge device, you need to associate the job with a container in a storage account. 准备部署作业时,作业定义会导出到存储容器。When you go to deploy your job, the job definition is exported to the storage container.

  1. 到“配置”下,依次选择“存储帐户设置”、“添加存储帐户”。 Under Configure, select Storage account settings then select Add storage account.

    Azure 流分析 - 添加存储帐户

  2. 从下拉菜单中选择在本教程开头创建的存储帐户Select the Storage account that you created at the beginning of this tutorial from the drop-down menu.

  3. 对于“容器”字段,请选择“新建”,然后为存储容器提供一个名称。 For the Container field, select Create new and provide a name for the storage container.

  4. 选择“保存”。 Select Save.

部署作业Deploy the job

现已准备好在 IoT Edge 设备上部署 Azure 流分析作业。You are now ready to deploy the Azure Stream Analytics job on your IoT Edge device.

在本部分,请使用 Azure 门户中的设置模块向导来创建部署清单。 In this section, you use the Set Modules wizard in the Azure portal to create a deployment manifest. 部署清单是一个 JSON 文件,该文件描述将要部署到设备的所有模块、用于存储模块映像的容器注册表、模块的管理方式,以及模块的相互通信方式。A deployment manifest is a JSON file that describes all the modules that will be deployed to a device, the container registries that store the module images, how the modules should be managed, and how the modules can communicate with each other. IoT Edge 设备从 IoT 中心检索其部署清单,任何使用其中的信息来部署和配置所有已分配的模块。Your IoT Edge device retrieves its deployment manifest from IoT Hub, then uses the information in it to deploy and configure all of its assigned modules.

就本教程来说,请部署两个模块。For this tutorial, you deploy two modules. 第一个模块是 SimulatedTemperatureSensor:一个模拟温度和湿度传感器的模块。The first is SimulatedTemperatureSensor, which is a module that simulates a temperature and humidity sensor. 第二个是流分析作业。The second is your Stream Analytics job. 传感器模块提供的数据流是作业查询将要分析的。The sensor module provides the stream of data that your job query will analyze.

  1. 在 Azure 门户中导航到 IoT 中心。In the Azure portal, navigate to your IoT hub.

  2. 转到“IoT Edge”,然后打开 IoT Edge 设备的详细信息页。 Go to IoT Edge, and then open the details page for your IoT Edge device.

  3. 选择“设置模块” 。Select Set modules.

  4. 如果以前在此设备上部署了 SimulatedTemperatureSensor 模块,则它可以自动填充数据。If you previously deployed the SimulatedTemperatureSensor module on this device, it might autopopulate. 否则,请通过以下步骤来添加模块:If it does not, add the module with the following steps:

    1. 单击“添加”,然后选择“IoT Edge 模块”。 Click Add and select IoT Edge Module.
    2. 对于名称,请键入 SimulatedTemperatureSensorFor the name, type SimulatedTemperatureSensor.
    3. 输入 mcr.microsoft.com/azureiotedge-simulated-temperature-sensor:1.0 作为映像 URI。For the image URI, enter mcr.microsoft.com/azureiotedge-simulated-temperature-sensor:1.0.
    4. 将其他设置保留不变,然后选择“保存” 。Leave the other settings unchanged and select Save.
  5. 通过以下步骤添加 Azure 流分析 Edge 作业:Add your Azure Stream Analytics Edge job with the following steps:

    1. 单击“添加”,然后选择“Azure 流分析模块”。 Click Add and select Azure Stream Analytics Module.
    2. 选择创建的订阅和 Azure 流分析 Edge 作业。Select your subscription and the Azure Stream Analytics Edge job that you created.
    3. 选择“保存”。 Select Save.

    保存更改后,流分析作业的详细信息将发布到创建的存储容器。Once you save your changes, the details of your Stream Analytics job are published to the storage container that you created.

  6. 将流分析模块添加到模块列表后,选择其名称以查看其构建方式,并在“更新 IoT Edge 模块”页上更新其设置。 When the Stream Analytics module is added to the list of modules, select its name to see how it's structured and update its settings on the Update IoT Edge Module page.

    “模块设置”选项卡有映像 URI 指向标准的 Azure 流分析映像。 The Module Settings tab has the image URI that points to a standard Azure Stream Analytics image. 此映像用于每个部署到 IoT Edge 设备的流分析模块。This one image is used for every Stream Analytics module that gets deployed to an IoT Edge device.

    “模块孪生设置”选项卡显示用于定义 Azure 流分析 (ASA) 属性(称为 ASAJobInfo)的 JSON。 The Module Twin Settings tab shows the JSON that defines the Azure Stream Analytics (ASA) property called ASAJobInfo. 该属性的值指向存储容器中的作业定义。The value of that property points to the job definition in your storage container. 此属性说明如何根据特定的作业详细信息配置流分析映像。This property is how the Stream Analytics image is configured with your specific job details.

    默认情况下,流分析模块名称与它基于的作业的名称相同。By default, the Stream Analytics module takes the same name as the job it's based on. 如果需要,可在此页上更改模块名称,但不必要这样做。You can change the module name on this page if you like, but it's not necessary.

  7. 选择“取消”或“保存”。 Select Cancel or Save.

  8. 请记下流分析模块的名称(因为需要在下一步使用它),然后选择“下一步: 路由”以继续操作。Make a note of the name of your Stream Analytics module because you'll need it in the next step, then select Next: Routes to continue.

  9. 在“路由”选项卡中,定义消息在模块和 IoT 中心之间传递的方式 。On the Routes tab, you define how messages are passed between modules and the IoT Hub. 使用名称/值对构造消息。Messages are constructed using name/value pairs. 将默认的 routeupstream 名称和值替换为下表中显示的对(以下名称/值对),将 {moduleName} 的实例替换为 Azure 流分析模块的名称。Replace the default route and upstream name and values with the pairs shown in following table, the following name/value pairs, replacing instances of {moduleName} with the name of your Azure Stream Analytics module.

    名称Name Value
    telemetryToCloud FROM /messages/modules/SimulatedTemperatureSensor/* INTO $upstream
    alertsToCloud FROM /messages/modules/{moduleName}/* INTO $upstream
    alertsToReset FROM /messages/modules/{moduleName}/* INTO BrokeredEndpoint("/modules/SimulatedTemperatureSensor/inputs/control")
    telemetryToAsa FROM /messages/modules/SimulatedTemperatureSensor/* INTO BrokeredEndpoint("/modules/{moduleName}/inputs/temperature")

    在此处声明的路由定义流经 IoT Edge 设备的数据流。The routes that you declare here define the flow of data through the IoT Edge device. 来自 SimulatedTemperatureSensor 的遥测数据先发送到 IoT 中心,然后发送到在流分析作业中配置的“温度”输入。 The telemetry data from SimulatedTemperatureSensor are sent to IoT Hub and to the temperature input that was configured in the Stream Analytics job. 警报输出消息先发送到 IoT 中心,然后发送到 SimulatedTemperatureSensor 模块以触发重置命令。The alert output messages are sent to IoT Hub and to the SimulatedTemperatureSensor module to trigger the reset command.

  10. 在完成时选择“下一步: 查看 + 创建”。Select Next: Review + Create.

  11. 在“查看 + 创建”选项卡中,可以查看在向导中提供的信息如何转换为 JSON 部署清单。 In the Review + Create tab, you can see how the information you provided in the wizard is converted into a JSON deployment manifest. 查看完清单后,选择“创建”。 When you're done reviewing the manifest, select Create.

  12. 你将返回到设备详细信息页。You're returned to the device details page. 选择“刷新” 。Select Refresh.

    应会看到,新的流分析模块与 IoT Edge 代理模块和 IoT Edge 中心模块都在运行。You should see the new Stream Analytics module running, along with the IoT Edge agent and IoT Edge hub modules. 此信息可能需要在几分钟后才能抵达 IoT Edge 设备,然后新模块将会启动。It may take a few minutes for the information to reach your IoT Edge device, and then for the new modules to start. 如果暂时未看到模块运行,请继续刷新页面。If you don't see the modules running right away, continue refreshing the page.

    设备报告的 SimulatedTemperatureSensor 和 ASA 模块

查看数据View data

现在可以转到 IoT Edge 设备,检查 Azure 流分析模块与 SimulatedTemperatureSensor 模块之间的交互。Now you can go to your IoT Edge device to check out the interaction between the Azure Stream Analytics module and the SimulatedTemperatureSensor module.

  1. 检查所有模块是否在 Docker 中运行:Check that all the modules are running in Docker:

    iotedge list  
  2. 查看所有系统日志和指标数据。View all system logs and metrics data. 使用流分析模块名称:Use the Stream Analytics module name:

    iotedge logs -f {moduleName}  
  3. 查看传感器日志来了解重置命令对 SimulatedTemperatureSensor 的影响:View the reset command affect the SimulatedTemperatureSensor by viewing the sensor logs:

    iotedge logs SimulatedTemperatureSensor

    可以看到,机器温度逐渐升高,在 30 秒内达到了 70 度。You can watch the machine's temperature gradually rise until it reaches 70 degrees for 30 seconds. 然后,流分析模块触发重置,机器温度下降到 21 度。Then the Stream Analytics module triggers a reset, and the machine temperature drops back to 21.


清理资源Clean up resources

如果打算继续学习下一篇建议的文章,可以保留已创建的资源和配置,以便重复使用。If you plan to continue to the next recommended article, you can keep the resources and configurations that you created and reuse them. 还可以继续使用相同的 IoT Edge 设备作为测试设备。You can also keep using the same IoT Edge device as a test device.

否则,可以删除本文中使用的本地配置和 Azure 资源,以免产生费用。Otherwise, you can delete the local configurations and the Azure resources that you used in this article to avoid charges.

删除 Azure 资源Delete Azure resources

删除 Azure 资源和资源组的操作不可逆。Deleting Azure resources and resource groups is irreversible. 请确保不要意外删除错误的资源组或资源。Make sure that you don't accidentally delete the wrong resource group or resources. 如果在现有的包含要保留资源的资源组中创建了 IoT 中心,请只删除 IoT 中心资源本身,而不要删除资源组。If you created the IoT hub inside an existing resource group that has resources that you want to keep, delete only the IoT hub resource itself, instead of deleting the resource group.

若要删除资源,请执行以下操作:To delete the resources:

  1. 登录到 Azure 门户,然后选择“资源组”。 Sign in to the Azure portal and select Resource groups.

  2. 选择包含 IoT Edge 测试资源的资源组的名称。Select the name of the resource group that contains your IoT Edge test resources.

  3. 查看包含在资源组中的资源的列表。Review the list of resources contained in your resource group. 若要删除这一切,可以选择“删除资源组”。 If you want to delete all of them, you can select Delete resource group. 如果只需删除部分内容,可以单击要单独删除的每个资源。If you want to delete only some of them, you can click into each resource to delete them individually.

后续步骤Next steps

本教程介绍了如何配置 Azure 流分析作业来分析 IoT Edge 设备中的数据,In this tutorial, you configured an Azure Streaming Analytics job to analyze data from your IoT Edge device. 然后介绍了如何将此 Azure 流分析模块加载到 IoT Edge 设备,以便处理并响应本地出现的温度增高情况,并将聚合的数据流发送到云。You then loaded this Azure Stream Analytics module on your IoT Edge device to process and react to temperature increase locally, as well as sending the aggregated data stream to the cloud. 若要了解 Azure IoT Edge 如何为企业创建更多解决方案,请继续学习其他教程。To see how Azure IoT Edge can create more solutions for your business, continue on to the other tutorials.