匹配以独立消息形式到达的相关值。 这些示例使用一个MQTT源,配合多个订阅和自定义的状态变换,保留记录,直到能够将它们匹配起来。
Prerequisites
完成 共享的示例前置条件,包括现有 default 的配置文件、factory-mqtt 端点,以及用于 ghcr.io 的 factory-artifacts 注册表端点。 这些例子引用了这些共享资源,而不重新创建它们。
对于 Azure CLI 和 Bicep,请按照创建数据流图中的环境设置操作。 将AIO_INSTANCE_NAME和RESOURCE_GROUP设置为你现有的实例和资源组。 对于 Bicep,还要将 CUSTOM_LOCATION_NAME 设置为该实例现有的自定义位置名称。
选择匹配规则
-
时间戳和资产 ID:将速度读数与该读数采样时有效的温度匹配。
-
部件 ID 和资产 ID:将预测与同一物理部件的延迟测量匹配。
自定义转换管理待处理记录、过期记录和重复项处理。 在一个运行时实例上运行每个样本图:其状态存在内存,重启时丢失。 恢复需要外部持久输入日志和下游去重。
按时间戳匹配上下文
在10:00:02处,12的速度读数与10:00:01处的温度20匹配,而不是与22处的温度10:00:03匹配。 后一个温度值在速度读数的时间戳对应时刻无效。
选择不晚于读取时间戳的最新上下文。 这种选择需要自定义有状态的WASM或外部服务,而不是内置窗口。
该连接配置在用于ghcr.io的graph-align-by-time注册表端点中使用自定义factory-artifacts样本。
示例源代码和协定描述其有界状态和诊断。 本例的契约为:按资产进行缓冲;为乱序上下文预留2秒;选择不晚于速度时间戳的最新温度,且其时效最长为5秒;当该决策准备好时发出读值。 如果缺少符合条件的上下文,则发出诊断信息并跳过此次读取。
操作体验仅支持创建和查看来自 Azure 容器注册表 (ACR) 的图制品,对于内置转换,还支持来自mcr.microsoft.com的图制品。 它无法通过factory-artifacts选择或显示该样本的GHCR工件。 使用Azure CLI或Bicep选项卡来部署该示例。
对于一个单独重新发布且已配置 ACR 注册表终结点的 ACR 变体:
- 使用现有的
align-by-time配置文件创建一个名为default的数据流图。
- 使用
factory-mqtt以及主题factory/speed和factory/temperature配置源。
- 选择添加图转换(可选)。 在 图选择中,选择重新发布的
graph-align-by-time:1.0.0-rc.4 构件,然后选择 应用。
- 使用
factory-mqtt 和主题 factory/aligned配置目标,然后选择 保存。 操作体验会自动生成制品的节点连接。
这个 ACR 变体使用不同的注册表端点;它并不是以下部署标签页中完全相同的 GHCR 配置。 有关支持的操作和限制,请参见 使用 WASM 变换。
将这些完整的图属性保存为graph.json:
{
"mode": "Enabled",
"nodes": [
{
"name": "source",
"nodeType": "Source",
"sourceSettings": {
"endpointRef": "factory-mqtt",
"dataSources": ["factory/speed", "factory/temperature"]
}
},
{
"name": "processor",
"nodeType": "Graph",
"graphSettings": {
"registryEndpointRef": "factory-artifacts",
"artifact": "azure-samples/explore-iot-operations/dataflow-patterns/graph-align-by-time:1.0.0-rc.4"
}
},
{
"name": "destination",
"nodeType": "Destination",
"destinationSettings": {
"endpointRef": "factory-mqtt",
"dataDestination": "factory/aligned"
}
}
],
"nodeConnections": [
{"from": {"name": "source"}, "to": {"name": "processor"}},
{"from": {"name": "processor"}, "to": {"name": "destination"}}
]
}
应用图:
az iot ops dataflowgraph apply --name align-by-time --instance $AIO_INSTANCE_NAME --resource-group $RESOURCE_GROUP --config-file graph.json
将此完整模板保存为 main.bicep。 它引用了现有实例、自定义位置和配置文件:
param aioInstanceName string
param customLocationName string
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: 'align-by-time'
extendedLocation: {
name: customLocation.id
type: 'CustomLocation'
}
properties: {
mode: 'Enabled'
nodes: [
{
name: 'source'
nodeType: 'Source'
sourceSettings: {
endpointRef: 'factory-mqtt'
dataSources: [
'factory/speed'
'factory/temperature'
]
}
}
{
name: 'processor'
nodeType: 'Graph'
graphSettings: {
registryEndpointRef: 'factory-artifacts'
artifact: 'azure-samples/explore-iot-operations/dataflow-patterns/graph-align-by-time:1.0.0-rc.4'
}
}
{
name: 'destination'
nodeType: 'Destination'
destinationSettings: {
endpointRef: 'factory-mqtt'
dataDestination: 'factory/aligned'
}
}
]
nodeConnections: [
{
from: { name: 'source' }
to: { name: 'processor' }
}
{
from: { name: 'processor' }
to: { name: 'destination' }
}
]
}
}
部署模板:
az deployment group create --resource-group $RESOURCE_GROUP --template-file main.bicep --parameters aioInstanceName="$AIO_INSTANCE_NAME" customLocationName="$CUSTOM_LOCATION_NAME"
Important
生产环境中不支持使用 Kubernetes 部署清单,并且只能用于调试和测试。
将此完整资源保存为align-by-time.yaml:
apiVersion: connectivity.iotoperations.azure.com/v1
kind: DataflowGraph
metadata:
name: align-by-time
namespace: azure-iot-operations
spec:
profileRef: default
mode: Enabled
nodes:
- name: source
nodeType: Source
sourceSettings:
endpointRef: factory-mqtt
dataSources: [factory/speed, factory/temperature]
- name: processor
nodeType: Graph
graphSettings:
registryEndpointRef: factory-artifacts
artifact: azure-samples/explore-iot-operations/dataflow-patterns/graph-align-by-time:1.0.0-rc.4
- name: destination
nodeType: Destination
destinationSettings:
endpointRef: factory-mqtt
dataDestination: factory/aligned
nodeConnections:
- from: { name: source }
to: { name: processor }
- from: { name: processor }
to: { name: destination }
应用图以进行调试:
kubectl apply -f align-by-time.yaml
按顺序发布这些合成消息,每行一条 JSON 消息(NDJSON):
{"kind":"temperature","assetId":"line-1","sampledAt":"2026-10-19T10:00:01Z","temperature":20}
{"kind":"temperature","assetId":"line-1","sampledAt":"2026-10-19T10:00:03Z","temperature":22}
{"kind":"speed","assetId":"line-1","sampledAt":"2026-10-19T10:00:02Z","speed":12}
{"kind":"tick","assetId":"line-1","sampledAt":"2026-10-19T10:00:04Z"}
前两条消息转到factory/temperature;速度读数和最后一条 tick 消息转到factory/speed。 该转换保留温度并缓存速度读数。 两秒的宽限期过后,10:00:04时刻的 tick 消息会释放10:00:02时刻的速度读数,在factory/aligned上产生以下结果:
{
"assetId": "line-1",
"sampledAt": "2026-10-19T10:00:02Z",
"speed": 12,
"temperature": 20,
"temperatureSampledAt": "2026-10-19T10:00:01Z"
}
当一次到达释放多个读数时,定制模块会输出{"kind":"batch","records":[...]}。 下游处理必须处理该批次封装。
请牢记以下运行时间限制:
- 为每个活跃资产发送可信的周期性 tick。 没有后台计时器,所以空闲图无法释放待处理读数,也无法在没有新的到达时使状态过期。
- 使用RFC3339时间戳,不早于Unix时代,精度为毫秒级或更粗略。 默认的最大未来时钟偏移是30秒。
- 监控诊断是否有延迟或重复输入和缺失上下文。 要查看样本的限制和重复规则,请查看
contracts.json 文件。
- 将缓冲的时间戳和ID保持在JSON格式中。 输出的MQTT元数据来自发布该结果的到达消息,而不一定来自原始读数。
按 ID 将预测与延迟测量匹配
将预测203与资产line-1、部件part-42的后续测量200相匹配。 残差为测量减去预测值: 200 - 203 = -3。
按资产和部件ID匹配。 如果片段ID重复出现,请包含运行或批处理ID。 缓存待处理记录以支持任意到达顺序,限制其保留时间和数量,并保留预测的时间戳和模型版本。
自定义 graph-correlate-by-id 样本匹配记录并计算 residual 和 targetError。 残差表示预测误差;当目标为198时,独立的目标误差为200 - 198 = 2。
部署ID相关性示例
操作体验仅支持创建和查看来自 ACR 的图制品,对于内置转换,还支持来自mcr.microsoft.com的图制品。 它无法通过factory-artifacts选择或显示该样本的GHCR工件。 使用Azure CLI或Bicep选项卡来部署该示例。
对于一个单独重新发布且已配置 ACR 注册表终结点的 ACR 变体:
- 使用现有的
correlate-predictions配置文件创建一个名为default的数据流图。
- 使用
factory-mqtt以及主题factory/predictions和factory/measurements配置源。
- 选择添加图转换(可选)。 在 图选择中,选择重新发布的
graph-correlate-by-id:1.0.0-rc.4 构件,然后选择 应用。
- 使用
factory-mqtt 和主题 factory/matches配置目标,然后选择 保存。 操作体验会自动生成制品的节点连接。
这个 ACR 变体使用不同的注册表端点;它并不是以下部署标签页中完全相同的 GHCR 配置。 有关支持的操作和限制,请参见 使用 WASM 变换。
将这些完整的图属性保存为graph.json:
{
"mode": "Enabled",
"nodes": [
{
"name": "source",
"nodeType": "Source",
"sourceSettings": {
"endpointRef": "factory-mqtt",
"dataSources": ["factory/predictions", "factory/measurements"]
}
},
{
"name": "processor",
"nodeType": "Graph",
"graphSettings": {
"registryEndpointRef": "factory-artifacts",
"artifact": "azure-samples/explore-iot-operations/dataflow-patterns/graph-correlate-by-id:1.0.0-rc.4"
}
},
{
"name": "destination",
"nodeType": "Destination",
"destinationSettings": {
"endpointRef": "factory-mqtt",
"dataDestination": "factory/matches"
}
}
],
"nodeConnections": [
{"from": {"name": "source"}, "to": {"name": "processor"}},
{"from": {"name": "processor"}, "to": {"name": "destination"}}
]
}
应用图:
az iot ops dataflowgraph apply --name correlate-predictions --instance $AIO_INSTANCE_NAME --resource-group $RESOURCE_GROUP --config-file graph.json
将此完整模板保存为 main.bicep。 它引用了现有实例、自定义位置和配置文件:
param aioInstanceName string
param customLocationName string
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: 'correlate-predictions'
extendedLocation: {
name: customLocation.id
type: 'CustomLocation'
}
properties: {
mode: 'Enabled'
nodes: [
{
name: 'source'
nodeType: 'Source'
sourceSettings: {
endpointRef: 'factory-mqtt'
dataSources: [
'factory/predictions'
'factory/measurements'
]
}
}
{
name: 'processor'
nodeType: 'Graph'
graphSettings: {
registryEndpointRef: 'factory-artifacts'
artifact: 'azure-samples/explore-iot-operations/dataflow-patterns/graph-correlate-by-id:1.0.0-rc.4'
}
}
{
name: 'destination'
nodeType: 'Destination'
destinationSettings: {
endpointRef: 'factory-mqtt'
dataDestination: 'factory/matches'
}
}
]
nodeConnections: [
{
from: { name: 'source' }
to: { name: 'processor' }
}
{
from: { name: 'processor' }
to: { name: 'destination' }
}
]
}
}
部署模板:
az deployment group create --resource-group $RESOURCE_GROUP --template-file main.bicep --parameters aioInstanceName="$AIO_INSTANCE_NAME" customLocationName="$CUSTOM_LOCATION_NAME"
Important
生产环境中不支持使用 Kubernetes 部署清单,并且只能用于调试和测试。
Azure Samples 提供了完整的
correlate-predictions.yaml 拥有自己的主题和已发布的图引用。 单独的
align-by-time.yaml 包含时间戳示例。
克隆示例仓库,完成 共享先决条件后执行以下命令:
git clone https://github.com/Azure-Samples/explore-iot-operations.git
cd explore-iot-operations
kubectl apply -f samples/wasm/dataflow-patterns/manifests/registry.yaml
kubectl apply -f samples/wasm/dataflow-patterns/manifests/correlate-predictions.yaml
这些清单使用 azure-iot-operations 中现有的 default 配置文件和 factory-mqtt 端点。 注册表清单描述了共享 factory-artifacts 端点;如果已经配置好,其应用步骤是幂等的。
ID 关联要求预测包含kind: "prediction"、assetId、pieceId、正整数modelVersion和有限数值predictedWeight。 测量需要 kind: "measurement"相同的资产和部件ID,以及有限的数值 measuredWeight 和 targetWeight。 使用不同的predictedAt和measuredAt字段,用于描述不同时间点的可选时间戳。 共享的非关键字字段必须取值相等,否则匹配失败。
该定制模块管理待处理匹配项、限制、重复处理和已完成匹配项输出。 该图将匹配项发布到单独的主题factory/matches。
合成输入消息(NDJSON): 将第一行发布到 factory/predictions ,第二行随后发布到 factory/measurements。 预测包含了用于重新训练的原始特征值。
{"kind":"prediction","assetId":"line-1","pieceId":"part-42","modelVersion":8,"speed":10,"temperature":20,"predictedWeight":203}
{"kind":"measurement","assetId":"line-1","pieceId":"part-42","measuredWeight":200,"targetWeight":198}
仅凭预测无法产生匹配。 当测量到达时,应在factory/matches上收到以下负载,其中包含原始模型版本和特征值:
{"kind":"correlated","assetId":"line-1","pieceId":"part-42","modelVersion":8,"speed":10,"temperature":20,"predictedWeight":203,"measuredWeight":200,"targetWeight":198,"residual":-3,"targetError":2}
两种顺序都可以。 默认情况下,待处理条目在第一端到达后 60 秒失效;重试不会延长该时间。 有效的到达或可信 {"kind":"tick","assetId":"line-1"} 消息会触发清理;没有后台计时器。
已完成的匹配项默认会在 60 秒内去重,而不是永久去重。 重启会失去待处理和已完成的条目。 为进行恢复,请在外部持久接收器中按资产、部件和可选的运行 ID 去重。 在重新训练时,保留每个预测使用的原始特征值。
相关内容