教程:将 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 门户中创建一项 Azure 流分析作业,然后将其作为 IoT Edge 模块进行部署,不需额外的代码。Azure IoT Edge and Azure Stream Analytics are integrated so that 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.

先决条件Prerequisites

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 存储帐户,也可以创建新的 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 ValueValue
    订阅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.
    NameName 为存储帐户提供唯一的名称。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. 从下拉列表中选择“Edge 中心” 。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. 从下拉列表中选择“Edge 中心” 。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 代码会将重置命令发送到 alert 输出。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.

    SELECT  
        'reset' AS command 
    INTO 
       alert 
    FROM 
       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. 将流分析模块添加到模块列表后,选择“配置”以查看其构建方式。 When the Stream Analytics module is added to the list of modules, select Configure to see how it's structured.

    映像 URI 指向一个标准 Azure 流分析映像。The image URI 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.

    模块孪生是通过一个名为 ASAJobInfo 的所需属性配置的。The module twin is configured with a desired 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. 关闭模块配置页。Close the module configuration page.

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

  9. 将“Routes”中的默认值替换为以下代码。 Replace the default value in Routes with the following code. {moduleName} 的所有三个实例更新为 Azure 流分析模块的名称。Update all three instances of {moduleName} with the name of your Azure Stream Analytics module.

    {
        "routes": {
            "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.

  11. 在“查看部署”步骤中,可以查看在向导中提供的信息如何转换为 JSON 部署清单。 In the Review Deployment step, 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 Submit.

  12. 返回到“设备详细信息”页,并选择“刷新” 。Return to the device details page, and then 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.