透過對每個讀數套用已知公式來計算預測。 例如,當速度為 20、溫度為 203 時,weight = 2 * speed + 0.5 * temperature + 173 預測權重為 10。 內建的地圖轉換會執行計算。
範例使用預先定義的公式與合成值。
相關的獨立模式涵蓋 擬合並批准更新,以及 透過MQTT交付核准係數。
Prerequisites
完成 共享的範例前置條件,包括 default 個人檔案與 factory-mqtt 端點。 除錯時用 YAML,生產管理則用 Azure CLI 或 Bicep。
使用現有的 default registry 端點來進行內建轉換。 對於 CLI 部署,將 RESOURCE_GROUP 和 AIO_INSTANCE_NAME 設為你現有的執行個體和資源群組。 對於 Bicep 部署,也可以設定CUSTOM_LOCATION_NAME為實例現有的自訂位置。 部署分頁會參考這些共享資源;它們不會創建這些資源。
選擇係數來自何處
| 係數來源 |
處理路徑 |
何時使用 |
| 每則訊息中包含 |
使用係數讀取 → 對應 → 預測 |
產生者已經提供該模型及其版本 |
| 依資產獨立儲存 |
讀取 → 擴充 → 對應 → 預測 |
應用程式會獨立於感測器訊息維持核准的係數 |
選擇任一完整圖。 擴充處理會提供儲存的參數;對應處理會計算公式。 這兩種方式都保留了零件 ID、特徵和模型版本。
評估訊息中提供的係數
當單筆記錄包含輸入值和係數時,map 轉換會評估speed * speedCoefficient + temperature * temperatureCoefficient + intercept。
以下範例中的萬用字元規則保留原始欄位。 若輸入分開到達,則使用 內容查閱或關聯來組合記錄。
需要執行階段環境或超出表達式語言演算法的模型,則需要 自訂的 WASM 或外部服務。
請使用以下其中一種方法部署此圖。 訂閱 factory/predicted-values,然後將以下輸入發佈給 factory/features-with-model。
在作業體驗中,開啟您的執行個體,然後選取資料流程圖>建立資料流程圖。
請命名圖表 predict-with-coefficients 並選擇 default 設定檔。
新增來源factory-mqtt,其端點為factory/features-with-model,主題為source。
使用登錄端點 default 中的 azureiotoperations/graph-dataflow-map:1.0.0 新增 map 轉換predict。 請依序配置這些規則:
| 輸入(依序) |
Output |
Expression |
* |
* |
保留空白 |
speed、、 temperature、 speedCoefficient、 temperatureCoefficient、 intercept |
predictedWeight |
$1 * $3 + $2 * $4 + $5 |
新增具有端點 factory-mqtt 和主題 factory/predicted-values 的目的地destination。
連接 source → predict → destination 並選擇 儲存。
將完整的圖屬性儲存為 graph.json。 轉換規則是在 configuration.value 中跳脫的 JSON 字串。
{
"mode": "Enabled",
"nodes": [
{
"name": "source",
"nodeType": "Source",
"sourceSettings": {
"endpointRef": "factory-mqtt",
"dataSources": ["factory/features-with-model"]
}
},
{
"name": "predict",
"nodeType": "Graph",
"graphSettings": {
"registryEndpointRef": "default",
"artifact": "azureiotoperations/graph-dataflow-map:1.0.0",
"configuration": [
{
"key": "rules",
"value": "{\"map\":[{\"inputs\":[\"*\"],\"output\":\"*\"},{\"inputs\":[\"speed\",\"temperature\",\"speedCoefficient\",\"temperatureCoefficient\",\"intercept\"],\"output\":\"predictedWeight\",\"expression\":\"$1 * $3 + $2 * $4 + $5\"}]}"
}
]
}
},
{
"name": "destination",
"nodeType": "Destination",
"destinationSettings": {
"endpointRef": "factory-mqtt",
"dataDestination": "factory/predicted-values"
}
}
],
"nodeConnections": [
{"from": {"name": "source"}, "to": {"name": "predict"}},
{"from": {"name": "predict"}, "to": {"name": "destination"}}
]
}
將圖形套用至現有實例的 default 設定檔:
az iot ops dataflowgraph apply \
--name predict-with-coefficients \
--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: 'predict-with-coefficients'
extendedLocation: {
name: customLocation.id
type: 'CustomLocation'
}
properties: {
mode: 'Enabled'
nodes: [
{
name: 'source'
nodeType: 'Source'
sourceSettings: {
endpointRef: 'factory-mqtt'
dataSources: ['factory/features-with-model']
}
}
{
name: 'predict'
nodeType: 'Graph'
graphSettings: {
registryEndpointRef: 'default'
artifact: 'azureiotoperations/graph-dataflow-map:1.0.0'
configuration: [
{
key: 'rules'
value: '{"map":[{"inputs":["*"],"output":"*"},{"inputs":["speed","temperature","speedCoefficient","temperatureCoefficient","intercept"],"output":"predictedWeight","expression":"$1 * $3 + $2 * $4 + $5"}]}'
}
]
}
}
{
name: 'destination'
nodeType: 'Destination'
destinationSettings: {
endpointRef: 'factory-mqtt'
dataDestination: 'factory/predicted-values'
}
}
]
nodeConnections: [
{ from: { name: 'source' }, to: { name: 'predict' } }
{ from: { name: 'predict' }, 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 部署資訊清單,且僅應用於偵錯與測試。
儲存為 predict-with-coefficients.yaml。
apiVersion: connectivity.iotoperations.azure.com/v1
kind: DataflowGraph
metadata:
name: predict-with-coefficients
namespace: azure-iot-operations
spec:
profileRef: default
mode: Enabled
nodes:
- name: source
nodeType: Source
sourceSettings:
endpointRef: factory-mqtt
dataSources: [factory/features-with-model]
- name: predict
nodeType: Graph
graphSettings:
registryEndpointRef: default
artifact: azureiotoperations/graph-dataflow-map:1.0.0
configuration:
- key: rules
value: |
{
"map": [
{"inputs":["*"],"output":"*"},
{
"inputs":["speed","temperature","speedCoefficient","temperatureCoefficient","intercept"],
"output":"predictedWeight",
"expression":"$1 * $3 + $2 * $4 + $5"
}
]
}
- name: destination
nodeType: Destination
destinationSettings:
endpointRef: factory-mqtt
dataDestination: factory/predicted-values
nodeConnections:
- from: { name: source }
to: { name: predict }
- from: { name: predict }
to: { name: destination }
kubectl apply -f predict-with-coefficients.yaml
在 factory/features-with-model 上的輸入:
{"assetId":"line-1","pieceId":"part-42","modelVersion":8,"speed":10,"temperature":20,"speedCoefficient":2,"temperatureCoefficient":0.5,"intercept":173}
輸出 會 factory/predicted-values 保留輸入欄位,並加上 predictedWeight:
{"assetId":"line-1","pieceId":"part-42","modelVersion":8,"speed":10,"temperature":20,"speedCoefficient":2,"temperatureCoefficient":0.5,"intercept":173,"predictedWeight":203}
從狀態儲存庫讀取係數
利用 enrichment 讀取鍵為 factory-active-models 的完整 active-model 記錄,然後用映射轉換來評估公式。
若要進行獨立示範,請使用 state-store writer 初始化 v8 記錄;不需要執行擬合步驟。 寫入器和訊息代理程式的時鐘必須同步。 請等待匹配 +OK 回覆後再發布讀數。
請使用以下其中一種方法部署此圖。 訂閱 factory/predictions,然後將此輸入發佈至 factory/features:
{"assetId":"line-1","pieceId":"part-43","speed":10,"temperature":20}
使用 seeded v8 模型時,輸出包含 predictedWeight: 203 和 modelVersion: 8。
圖表會讀取每個預測的核准紀錄。
在作業體驗中,開啟您的執行個體,然後選取資料流程圖>建立資料流程圖。
請命名圖表 predict-with-approved-model 並選擇 default 設定檔。
新增來源factory-mqtt,其端點為factory/features,主題為source。
使用登錄端點 default 中的 azureiotoperations/graph-dataflow-map:1.0.0 新增 map 轉換read-model。 在其資料集配置中,設定狀態儲存鍵 factory-active-models as model、匹配輸入 $source.assetId、 $context.assetId及 匹配表達式 $1 == $2。 依序配置這些映射規則,保留表達式為空:
| Input |
Output |
* |
* |
$context(model).modelVersion |
modelVersion |
$context(model).speedCoefficient |
speedCoefficient |
$context(model).temperatureCoefficient |
temperatureCoefficient |
$context(model).intercept |
intercept |
使用相同的成品和登錄端點,新增 map 轉換predict。 請依序配置這些規則:
| 輸入(依序) |
Output |
Expression |
* |
* |
保留空白 |
assetId |
kind |
"prediction" |
speed、、 temperature、 speedCoefficient、 temperatureCoefficient、 intercept |
predictedWeight |
$1 * $3 + $2 * $4 + $5 |
新增具有端點 factory-mqtt 和主題 factory/predictions 的目的地destination。
連接 source → read-model → predict → destination 並選擇 儲存。
將完整的圖屬性儲存為 graph.json。
{
"mode": "Enabled",
"nodes": [
{
"name": "source",
"nodeType": "Source",
"sourceSettings": {
"endpointRef": "factory-mqtt",
"dataSources": ["factory/features"]
}
},
{
"name": "read-model",
"nodeType": "Graph",
"graphSettings": {
"registryEndpointRef": "default",
"artifact": "azureiotoperations/graph-dataflow-map:1.0.0",
"configuration": [
{
"key": "rules",
"value": "{\"datasets\":[{\"key\":\"factory-active-models as model\",\"inputs\":[\"$source.assetId\",\"$context.assetId\"],\"expression\":\"$1 == $2\"}],\"map\":[{\"inputs\":[\"*\"],\"output\":\"*\"},{\"inputs\":[\"$context(model).modelVersion\"],\"output\":\"modelVersion\"},{\"inputs\":[\"$context(model).speedCoefficient\"],\"output\":\"speedCoefficient\"},{\"inputs\":[\"$context(model).temperatureCoefficient\"],\"output\":\"temperatureCoefficient\"},{\"inputs\":[\"$context(model).intercept\"],\"output\":\"intercept\"}]}"
}
]
}
},
{
"name": "predict",
"nodeType": "Graph",
"graphSettings": {
"registryEndpointRef": "default",
"artifact": "azureiotoperations/graph-dataflow-map:1.0.0",
"configuration": [
{
"key": "rules",
"value": "{\"map\":[{\"inputs\":[\"*\"],\"output\":\"*\"},{\"inputs\":[\"assetId\"],\"output\":\"kind\",\"expression\":\"\\\"prediction\\\"\"},{\"inputs\":[\"speed\",\"temperature\",\"speedCoefficient\",\"temperatureCoefficient\",\"intercept\"],\"output\":\"predictedWeight\",\"expression\":\"$1 * $3 + $2 * $4 + $5\"}]}"
}
]
}
},
{
"name": "destination",
"nodeType": "Destination",
"destinationSettings": {
"endpointRef": "factory-mqtt",
"dataDestination": "factory/predictions"
}
}
],
"nodeConnections": [
{"from": {"name": "source"}, "to": {"name": "read-model"}},
{"from": {"name": "read-model"}, "to": {"name": "predict"}},
{"from": {"name": "predict"}, "to": {"name": "destination"}}
]
}
az iot ops dataflowgraph apply \
--name predict-with-approved-model \
--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: 'predict-with-approved-model'
extendedLocation: {
name: customLocation.id
type: 'CustomLocation'
}
properties: {
mode: 'Enabled'
nodes: [
{
name: 'source'
nodeType: 'Source'
sourceSettings: {
endpointRef: 'factory-mqtt'
dataSources: ['factory/features']
}
}
{
name: 'read-model'
nodeType: 'Graph'
graphSettings: {
registryEndpointRef: 'default'
artifact: 'azureiotoperations/graph-dataflow-map:1.0.0'
configuration: [
{
key: 'rules'
value: '{"datasets":[{"key":"factory-active-models as model","inputs":["$source.assetId","$context.assetId"],"expression":"$1 == $2"}],"map":[{"inputs":["*"],"output":"*"},{"inputs":["$context(model).modelVersion"],"output":"modelVersion"},{"inputs":["$context(model).speedCoefficient"],"output":"speedCoefficient"},{"inputs":["$context(model).temperatureCoefficient"],"output":"temperatureCoefficient"},{"inputs":["$context(model).intercept"],"output":"intercept"}]}'
}
]
}
}
{
name: 'predict'
nodeType: 'Graph'
graphSettings: {
registryEndpointRef: 'default'
artifact: 'azureiotoperations/graph-dataflow-map:1.0.0'
configuration: [
{
key: 'rules'
value: '{"map":[{"inputs":["*"],"output":"*"},{"inputs":["assetId"],"output":"kind","expression":"\\"prediction\\""},{"inputs":["speed","temperature","speedCoefficient","temperatureCoefficient","intercept"],"output":"predictedWeight","expression":"$1 * $3 + $2 * $4 + $5"}]}'
}
]
}
}
{
name: 'destination'
nodeType: 'Destination'
destinationSettings: {
endpointRef: 'factory-mqtt'
dataDestination: 'factory/predictions'
}
}
]
nodeConnections: [
{ from: { name: 'source' }, to: { name: 'read-model' } }
{ from: { name: 'read-model' }, to: { name: 'predict' } }
{ from: { name: 'predict' }, 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 部署資訊清單,且僅應用於偵錯與測試。
儲存為 predict-with-approved-model.yaml。
apiVersion: connectivity.iotoperations.azure.com/v1
kind: DataflowGraph
metadata:
name: predict-with-approved-model
namespace: azure-iot-operations
spec:
profileRef: default
mode: Enabled
nodes:
- name: source
nodeType: Source
sourceSettings:
endpointRef: factory-mqtt
dataSources: [factory/features]
- name: read-model
nodeType: Graph
graphSettings:
registryEndpointRef: default
artifact: azureiotoperations/graph-dataflow-map:1.0.0
configuration:
- key: rules
value: |
{
"datasets": [
{
"key":"factory-active-models as model",
"inputs":["$source.assetId","$context.assetId"],
"expression":"$1 == $2"
}
],
"map": [
{"inputs":["*"],"output":"*"},
{"inputs":["$context(model).modelVersion"],"output":"modelVersion"},
{"inputs":["$context(model).speedCoefficient"],"output":"speedCoefficient"},
{"inputs":["$context(model).temperatureCoefficient"],"output":"temperatureCoefficient"},
{"inputs":["$context(model).intercept"],"output":"intercept"}
]
}
- name: predict
nodeType: Graph
graphSettings:
registryEndpointRef: default
artifact: azureiotoperations/graph-dataflow-map:1.0.0
configuration:
- key: rules
value: |
{
"map": [
{"inputs":["*"],"output":"*"},
{"inputs":["assetId"],"output":"kind","expression":"\"prediction\""},
{
"inputs":["speed","temperature","speedCoefficient","temperatureCoefficient","intercept"],
"output":"predictedWeight",
"expression":"$1 * $3 + $2 * $4 + $5"
}
]
}
- name: destination
nodeType: Destination
destinationSettings:
endpointRef: factory-mqtt
dataDestination: factory/predictions
nodeConnections:
- from: { name: source }
to: { name: read-model }
- from: { name: read-model }
to: { name: predict }
- from: { name: predict }
to: { name: destination }
kubectl apply -f predict-with-approved-model.yaml
第一個對應會將所選模型和版本複製到紀錄中。
第二種是從這些欄位計算出來的。 Map 規則會讀取轉換的輸入。
若要比較更新前後的預測,請先將寫入器的modelVersion 改為 7,並將 intercept 改為 175 以預先建立 v7 記錄。 發布讀取資料、寫入 v8,然後發佈新項目的讀取資料。
| 已儲存的模型 |
速度計算10、溫度20 |
預測重量 |
| v7 |
10 * 2 + 20 * 0.5 + 175 |
205 |
| v8 |
10 * 2 + 20 * 0.5 + 173 |
203 |
當實例觀察到 v8 後,對 part-44 的讀取會在 factory/predictions 上產生此輸出:
{"assetId":"line-1","pieceId":"part-44","speed":10,"temperature":20,"modelVersion":8,"speedCoefficient":2,"temperatureCoefficient":0.5,"intercept":173,"kind":"prediction","predictedWeight":203}
狀態查詢快取可以在寫入成功後讓 v7 維持作用中。 檢查發出的 modelVersion,看看執行個體何時偵測到 v8。 範例假設存在匹配模型;針對缺失或陳舊的模型上下文,新增拒絕或恢復路徑。
查詢使用處理過程中可用的模型。 對於依讀數的時間戳記選取的模型,請使用 時間戳記比對。
相關內容