数据流图是一个可组合的处理管道,用于在源和目标之间移动时转换数据。 标准 数据流 遵循固定扩充、筛选、映射序列。 数据流图允许你按任意顺序链接转换、分支到并行路径并按时间窗口聚合数据。
本文逐步介绍如何创建数据流图。 有关数据流图和可用转换的概述,请参阅 数据流图概述。
数据流图目前仅支持 MQTT、Kafka 和 OpenTelemetry 终结点。 它们不支持其他端点类型,比如Data Lake、Microsoft Fabric OneLake、Azure 数据资源管理器和本地存储。
变换使用表达式语言来计算数值、测试条件和参考字段。 表达式按位置指代输入,而非按名称:列表中的第一个输入 inputs 是 $1,第二个是 $2,依此类推。 内置函数(例如 cToF)可用于转换和操作这些值。
有关完整的运算符、函数、数据类型和元数据字段列表,请参见表达式参考。
先决条件
已在开发计算机上安装 Azure CLI。 请查看可用的 Azure CLI 扩展,了解使用azure-iot-ops扩展所需的最低版本。 如有必要,请使用az --version检查版本,使用az upgrade进行更新。 有关详细信息,请参阅安装 Azure CLI。
适用于 Azure CLI 的 Azure IoT 操作 扩展。 使用以下命令添加扩展或将其更新到最新版本:
az extension add --upgrade --name azure-iot-ops
本文Azure CLI示例使用环境变量,这样你可以设置每个值一次,然后复制粘贴命令 as-is。 如果你使用的是快速入门中的 Azure IoT 操作 Codespaces 环境,这些变量已经为你设置好了,可以跳过这一步。 否则,在运行命令前,先在你的shell里设置以下环境变量。
以下脚本设置了最常用的环境变量:
| 环境变量 |
说明 |
SUBSCRIPTION_ID |
包含您的Azure IoT 操作实例的订阅ID。 |
RESOURCE_GROUP |
包含您的 Azure IoT 操作 实例的资源组名称。 |
AIO_INSTANCE_NAME |
你的 Azure IoT 操作 实例名称。 要列出你的实例,运行 az iot ops list -o table。 |
CLUSTER_NAME |
托管你的实例的 Azure Arc 支持的 Kubernetes 集群名称。 |
LOCATION |
例如,用于新资源eastus的 Azure 区域。 |
SUBSCRIPTION_ID=<subscription-id>
RESOURCE_GROUP=<resource-group-name>
AIO_INSTANCE_NAME=<instance-name>
CLUSTER_NAME=<cluster-name>
LOCATION=<region>
$SUBSCRIPTION_ID = "<subscription-id>"
$RESOURCE_GROUP = "<resource-group-name>"
$AIO_INSTANCE_NAME = "<instance-name>"
$CLUSTER_NAME = "<cluster-name>"
$LOCATION = "<region>"
你只需要设置本文使用的变量。 本文可能会使用你选择的资源名称的额外环境变量。 文章解释了如何将他们放置在引入的位置。
本文还使用 GRAPH_NAME 了 和 PROFILE 环境变量作为数据流图和配置文件名称。 在执行相关命令之前,先设置好每个。
创建数据流图
数据流图包含三种类型的元素:引入数据的 源 、处理数据的 转换 以及发送数据 的目标 。按希望数据流的顺序连接它们。
在运维体验中,切换到你的 Azure IoT 操作 实例。
选择 “数据流图>创建数据流图”。
输入数据流图的名称并选择数据流概要配置。 默认情况下选择默认配置文件。
通过将元素添加到画布来生成管道:
添加源:选择源终结点并配置主题以订阅传入消息。
添加转换:选择一个或多个转换来处理数据。 可用的变换包括映射、滤波、分支、串接、窗口和油门。 有关每个转换类型的详细信息,请参阅 数据流图概述。
添加目标:选择目标终结点并配置要向其发送已处理数据的主题或路径。
按要数据流的顺序连接元素。
选择 “保存 ”以部署数据流图。
Azure CLI 使用一个包含所有节点和连接的单一 JSON 配置文件的数据流图。 使用 az iot ops dataflowgraph apply 创建或替换该图。 以下示例读取温度数据,将其转换为华氏度,并发送到目标主题。
使用 graph.json 数据流图形属性创建文件。 在文件中 graph.json ,将每个变换的规则 value 以 eximent JSON 字符串的形式存储在字段中。 有关每个转换规则的可读形式,请参阅该转换类型的操作方法。
{
"mode": "Enabled",
"nodes": [
{
"nodeType": "Source",
"name": "sensors",
"sourceSettings": {
"endpointRef": "default",
"dataSources": [
"telemetry/temperature"
]
}
},
{
"nodeType": "Graph",
"name": "convert",
"graphSettings": {
"registryEndpointRef": "default",
"artifact": "azureiotoperations/graph-dataflow-map:1.0.0",
"configuration": [
{
"key": "rules",
"value": "{\"map\":[{\"inputs\":[\"*\"],\"output\":\"*\"},{\"inputs\":[\"temperature\"],\"output\":\"temperature_f\",\"expression\":\"cToF($1)\"}]}"
}
]
}
},
{
"nodeType": "Destination",
"name": "output",
"destinationSettings": {
"endpointRef": "default",
"dataDestination": "telemetry/converted"
}
}
],
"nodeConnections": [
{
"from": {
"name": "sensors"
},
"to": {
"name": "convert"
}
},
{
"from": {
"name": "convert"
},
"to": {
"name": "output"
}
}
]
}
应用该配置文件。
az iot ops dataflowgraph apply \
--name temperature-processing \
--instance $AIO_INSTANCE_NAME \
--resource-group $RESOURCE_GROUP \
--config-file graph.json
该图使用 default 数据流剖面。 若要使用不同的配置文件,请添加 --profile <PROFILE_NAME>。
使用以下结构创建 Bicep .bicep 文件。 此示例创建一个数据流图,该图读取温度数据,将其转换为华氏度,并将其发送到目标主题。
param aioInstanceName string = '<AIO_INSTANCE_NAME>'
param customLocationName string = '<CUSTOM_LOCATION_NAME>'
resource aioInstance 'Microsoft.IoTOperations/instances@2026-07-01' existing = {
name: aioInstanceName
}
resource customLocation 'Microsoft.ExtendedLocation/customLocations@2021-08-31-preview' existing = {
name: customLocationName
}
resource defaultDataflowProfile 'Microsoft.IoTOperations/instances/dataflowProfiles@2026-07-01' existing = {
parent: aioInstance
name: 'default'
}
resource dataflowGraph 'Microsoft.IoTOperations/instances/dataflowProfiles/dataflowGraphs@2026-07-01' = {
parent: defaultDataflowProfile
name: 'temperature-processing'
extendedLocation: {
name: customLocation.id
type: 'CustomLocation'
}
properties: {
profileRef: 'default'
mode: 'Enabled'
nodes: [
{
nodeType: 'Source'
name: 'sensors'
sourceSettings: {
endpointRef: 'default'
dataSources: [
'telemetry/temperature'
]
}
}
{
nodeType: 'Graph'
name: 'convert'
graphSettings: {
registryEndpointRef: 'default'
artifact: 'azureiotoperations/graph-dataflow-map:1.0.0'
configuration: [
{
key: 'rules'
value: '{"map":[{"inputs":["*"],"output":"*"},{"inputs":["temperature"],"output":"temperature_f","expression":"cToF($1)"}]}'
}
]
}
}
{
nodeType: 'Destination'
name: 'output'
destinationSettings: {
endpointRef: 'default'
dataDestination: 'telemetry/converted'
}
}
]
nodeConnections: [
{
from: { name: 'sensors' }
to: { name: 'convert' }
}
{
from: { name: 'convert' }
to: { name: 'output' }
}
]
}
}
部署 Bicep 文件:
az deployment group create --resource-group $RESOURCE_GROUP --template-file main.bicep
重要
生产环境中不支持使用 Kubernetes 部署清单,并且只能用于调试和测试。
使用以下结构创建 Kubernetes 清单 .yaml 文件。 此示例创建一个数据流图,该图读取温度数据,将其转换为华氏度,并将其发送到目标主题。
apiVersion: connectivity.iotoperations.azure.com/v1
kind: DataflowGraph
metadata:
name: temperature-processing
namespace: azure-iot-operations
spec:
profileRef: default
nodes:
- nodeType: Source
name: sensors
sourceSettings:
endpointRef: default
dataSources:
- telemetry/temperature
- nodeType: Graph
name: convert
graphSettings:
registryEndpointRef: default
artifact: azureiotoperations/graph-dataflow-map:1.0.0
configuration:
- key: rules
value: |
{
"map": [
{
"inputs": ["*"],
"output": "*"
},
{
"inputs": ["temperature"],
"output": "temperature_f",
"expression": "cToF($1)"
}
]
}
- nodeType: Destination
name: output
destinationSettings:
endpointRef: default
dataDestination: telemetry/converted
nodeConnections:
- from:
name: sensors
to:
name: convert
- from:
name: convert
to:
name: output
应用清单:
kubectl apply -f main.yaml
源定义数据进入管道的位置。 指定终结点引用和一个或多个主题。
在数据流图编辑器中,选择源元素并配置:
| 设置 |
说明 |
|
终结点 |
要使用的数据流终结点。 选择本地 MQTT 中转站的 默认值 。 |
|
Topics |
一个或多个传入消息的订阅主题。 |
CLI 一次性应用整个图形,因此将源配置为 Source 配置文件中的 graph.json 节点,然后运行 az iot ops dataflowgraph apply:
{
"nodeType": "Source",
"name": "sensors",
"sourceSettings": {
"endpointRef": "default",
"dataSources": [
"telemetry/temperature",
"telemetry/humidity"
]
}
}
{
nodeType: 'Source'
name: 'sensors'
sourceSettings: {
endpointRef: 'default'
dataSources: [
'telemetry/temperature'
'telemetry/humidity'
]
}
}
重要
生产环境中不支持使用 Kubernetes 部署清单,并且只能用于调试和测试。
- nodeType: Source
name: sensors
sourceSettings:
endpointRef: default
dataSources:
- telemetry/temperature
- telemetry/humidity
转换源和目标之间的过程数据。 每个变换都引用一个内置的工件,并有配置规则。
可用的内置转换包括:
| 转换 |
项目 |
说明 |
|
地图 |
azureiotoperations/graph-dataflow-map:1.0.0 |
重命名、重组、计算和复制字段。 |
|
滤波器 |
azureiotoperations/graph-dataflow-filter:1.0.0 |
删除与条件匹配的消息。 |
|
分支 |
azureiotoperations/graph-dataflow-branch:1.0.0 |
根据条件将每条消息路由到 true 路径或 false 路径。 |
|
串联 |
azureiotoperations/graph-dataflow-concatenate:1.0.0 |
将两个或多个路径合并回一条路径。 |
|
窗口 |
azureiotoperations/graph-dataflow-window:1.0.0 |
按时间间隔收集消息,然后聚合。 |
|
限流 |
azureiotoperations/graph-dataflow-throttle:1.0.0 |
限制每个MQTT主题模式的消息速率。 |
有关使用外部数据扩充消息的详细信息,请参阅 使用外部数据进行扩充。
在数据流图编辑器中,选择 “添加转换 ”并选择转换类型。 在视觉编辑器中配置规则。
在您的 nodeType 配置文件中,每个转换都是一个节点,其中 Graph 设置为 graph.json。 转换的规则是一个 JSON 对象,类似于将温度转换为华氏度的映射:
{
"map": [
{
"inputs": ["temperature"],
"output": "temperature_f",
"expression": "cToF($1)"
}
]
}
属性将 configuration 这些规则作为字符串处理,因此将规则 JSON 转义出来,并将其放在 value 字段中。 使用 az iot ops dataflowgraph apply 应用完整的图:
{
"nodeType": "Graph",
"name": "convert",
"graphSettings": {
"registryEndpointRef": "default",
"artifact": "azureiotoperations/graph-dataflow-map:1.0.0",
"configuration": [
{
"key": "rules",
"value": "{\"map\":[{\"inputs\":[\"temperature\"],\"output\":\"temperature_f\",\"expression\":\"cToF($1)\"}]}"
}
]
}
}
Tip
要生成转义字符串,请将规则保存到类似 rules.json 的文件中,运行 jq -c . rules.json,并将单行输出粘贴到 value 字段中。
每个转换都是一个具有nodeType: 'Graph'的节点。 该 configuration 属性将规则作为 JSON 字符串传递:
{
nodeType: 'Graph'
name: 'convert'
graphSettings: {
registryEndpointRef: 'default'
artifact: 'azureiotoperations/graph-dataflow-map:1.0.0'
configuration: [
{
key: 'rules'
value: '{"map":[{"inputs":["temperature"],"output":"temperature_f","expression":"cToF($1)"}]}'
}
]
}
}
重要
生产环境中不支持使用 Kubernetes 部署清单,并且只能用于调试和测试。
每个转换都是一个具有nodeType: Graph的节点。 该 configuration 属性将规则作为 JSON 字符串传递:
- nodeType: Graph
name: convert
graphSettings:
registryEndpointRef: default
artifact: azureiotoperations/graph-dataflow-map:1.0.0
configuration:
- key: rules
value: |
{
"map": [
{
"inputs": ["temperature"],
"output": "temperature_f",
"expression": "cToF($1)"
}
]
}
链式变换任意数量。 按照希望数据流的顺序在 nodeConnections 节中连接它们:
在您的 nodeConnections 配置文件的 graph.json 部分中定义处理顺序:
"nodeConnections": [
{
"from": {
"name": "sensors"
},
"to": {
"name": "remove-bad-data"
}
},
{
"from": {
"name": "remove-bad-data"
},
"to": {
"name": "convert"
}
},
{
"from": {
"name": "convert"
},
"to": {
"name": "output"
}
}
]
nodeConnections: [
{ from: { name: 'sensors' }, to: { name: 'remove-bad-data' } }
{ from: { name: 'remove-bad-data' }, to: { name: 'convert' } }
{ from: { name: 'convert' }, to: { name: 'output' } }
]
重要
生产环境中不支持使用 Kubernetes 部署清单,并且只能用于调试和测试。
nodeConnections:
- from: { name: sensors }
to: { name: remove-bad-data }
- from: { name: remove-bad-data }
to: { name: convert }
- from: { name: convert }
to: { name: output }
目的地定义了处理数据的去向。 指定终结点引用和主题或路径。
选择目标元素并配置:
| 设置 |
说明 |
|
终结点 |
要向其发送数据的数据流终结点。 |
|
Topic |
要发布已处理数据的主题或路径。 |
在您的 Destination 配置文件中将目标配置为 graph.json 节点,然后使用 az iot ops dataflowgraph apply 应用完整图:
{
"nodeType": "Destination",
"name": "output",
"destinationSettings": {
"endpointRef": "default",
"dataDestination": "telemetry/processed"
}
}
{
nodeType: 'Destination'
name: 'output'
destinationSettings: {
endpointRef: 'default'
dataDestination: 'telemetry/processed'
}
}
重要
生产环境中不支持使用 Kubernetes 部署清单,并且只能用于调试和测试。
- nodeType: Destination
name: output
destinationSettings:
endpointRef: default
dataDestination: telemetry/processed
有关基于消息内容的动态主题路由,请参阅将 消息路由到不同的主题。
验证数据流图是否正常工作
部署数据流图后,请验证它是否正在运行:
在操作体验中,选择你的数据流图查看其状态。 良好的图表显示运行状态。
用于 az iot ops dataflowgraph show 查看图形的详细信息:
az iot ops dataflowgraph show \
--name temperature-processing \
--instance $AIO_INSTANCE_NAME \
--resource-group $RESOURCE_GROUP
若要列出与配置文件关联的所有数据流图,请使用 az iot ops dataflowgraph list:
az iot ops dataflowgraph list \
--instance $AIO_INSTANCE_NAME \
--resource-group $RESOURCE_GROUP
检查资源 DataflowGraph 的状态:
az resource show --resource-group $RESOURCE_GROUP --resource-type Microsoft.IoTOperations/instances/dataflowProfiles/dataflowGraphs --name $GRAPH_NAME --parent instances/$AIO_INSTANCE_NAME/dataflowProfiles/$PROFILE
重要
生产环境中不支持使用 Kubernetes 部署清单,并且只能用于调试和测试。
kubectl get dataflowgraph temperature-processing -n azure-iot-operations
检查 Pod 日志中是否有任何错误:
kubectl logs -l app=dataflow -n azure-iot-operations --tail=50
相关内容