匹配以獨立訊息形式抵達的相關值。 這些範例使用一個具有多個訂閱的 MQTT 來源,以及一個會暫存記錄直到完成比對的自訂具狀態轉換。
Prerequisites
完成 共享的範例前置條件,包括現有 default 的設定檔、factory-mqtt 端點及供 ghcr.io 使用的 factory-artifacts 登錄端點。 這些範例參照這些共用資源,而不會重新建立它們。
對於 Azure CLI 和 Bicep,請依照「建立資料流程圖」中的環境設定。 將 AIO_INSTANCE_NAME 和 RESOURCE_GROUP 設定為你現有的執行個體和資源群組。 對於 Bicep,也可以設定CUSTOM_LOCATION_NAME為實例現有的自訂位置名稱。
選擇匹配規則
-
時間戳記和資產識別碼:將速度讀數與取樣當時有效的溫度進行比對。
-
物品與資產 ID: 將預測結果與同一實體物件的延遲測量相匹配。
自訂轉換管理待處理紀錄、過期及重複資料處理。 在一個執行時實例上執行每個取樣圖:其狀態在記憶體中,重新啟動時會遺失。 復原需要外部持久輸入日誌及下游去重機制。
依時間戳記匹配上下文
在 10:00:02 的速度讀數 12 與在 20 的溫度 10:00:01 相符,而非在 10:00:03 的溫度 22。 較晚的溫度在速度讀數的時間戳記時無效。
選擇讀取時間戳記當下或之前的最新情境。 此選擇需要自訂有狀態的 WASM 或外部服務,而非內建視窗。
此接線設定使用 ghcr.io 的 factory-artifacts 登錄端點中的自訂 graph-align-by-time 範例。
範例原始碼與合約說明了其有界狀態與診斷功能。 此範例的合約為:依資產進行緩衝處理;允許 2 秒的亂序內容寬限時間;選取速度時間戳記當下或之前最新 (且最多不超過 5 秒前) 的溫度;在該判定準備就緒時發出讀數。 若缺少符合條件的上下文,則發出診斷訊號並跳過讀取。
作業體驗僅支援從 Azure Container Registry (ACR) 建立及檢視圖形成品,而針對內建轉換則僅支援 mcr.microsoft.com。 系統無法透過 factory-artifacts 選取或顯示此範例的 GHCR 成品。 使用 Azure CLI 或 Bicep 標籤來部署完全相同的範例。
針對具已設定 ACR 登錄服務端點的個別重新發佈 ACR 變體:
- 建立名為
default且使用現有align-by-time設定檔的資料流圖。
- 使用
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;速度讀數與最後一個時間點訊號會傳送至 factory/speed。 轉換會保留溫度並暫存速度讀數。
10:00:04 的時間點訊號會在 2 秒寬限時間過後,釋出 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 中繼資料來自觸發釋出結果的送達訊息,而不一定是原始讀數。
依識別碼比對預測與延遲的測量結果
將資產 line-1、件數 part-42 的預測203與後續測量200相匹配。 殘差為測量減去預測值: 200 - 203 = -3。
依資產與工件識別碼進行比對。 如果零件 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 變體:
- 建立名為
default且使用現有correlate-predictions設定檔的資料流圖。
- 使用
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範例提供了完整的
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"、相同的資產與工件識別碼,以及屬於有限數值的 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 秒,而非永久。 重新啟動後,待處理和已完成的項目都會遺失。 若要進行復原,請在外部持久性接收端中依資產、工件及選用的執行識別碼去除重複項目。 在重新訓練時,保留每個預測所使用的原始特徵值。
相關內容