1つの結果に平均して10回の読み取りをまとめて、ばらつきとメッセージ量を減らす。 値 9, 11, 10, 10, 9, 11, 10, 10, 9, 11の場合、平均は 10です。
Prerequisites
例の共有前提条件を、defaultプロファイルと factory-mqtt エンドポイントも含めて満たします。 デバッグにはYAMLを使い、本番管理にはAzure CLIまたはBicepを使いましょう。
既存の内蔵レジストリエンドポイント「 default」を活用してください。
環境変数については、AIO_INSTANCE_NAMEとRESOURCE_GROUPを既存のインスタンス名とリソースグループ名に設定してください。 Bicep の場合は、CUSTOM_LOCATION_NAME に既存のカスタム ロケーション名も設定してください。
内蔵ウィンドウは重複しないバッチごとに平均値を1つ生成し、各読み取りのたびに生成されるわけではありません。 バッチが閉じるのを待つと、タイムアウトで部分的なバッチが閉じられることがあります。
構成構文については、 カウントベースのウィンドウ および 境界振る舞いを参照してください。
messageInCurrent 設定には、バッチを閉じる 10 番目のメッセージも、そのバッチに含まれます。
このグラフは、1つのアセットの読み取りデータだけをウィンドウ枠に送信します。
フィルターは、他の資産が同じバッチに平均されるのを防ぎます。
10個の値は別々のメッセージであり、1つのメッセージ内の配列ではありません。 アセットIDフィールドは自動的にウィンドウを分割するわけではありません。関連のない信号やユニットも分けておきましょう。
操作エクスペリエンスでインスタンスを開き、[データ フロー グラフ]>[データ フロー グラフの作成] を選択します。
グラフにaverage-line-1という名前を付け、defaultプロファイルを選択します。
factory-mqttという名前のソースを、エンドポイントfactory/valuesとトピックsourceを使って追加してください。
defaultというフィルター変換を、レジストリエンドポイントonly-line-1のazureiotoperations/graph-dataflow-filter:1.1.0を使って追加します。 入力 assetId と式 $1 != "line-1" のルールを追加します。 結果が true の場合、そのメッセージは破棄されます。
レジストリエンドポイントbatchのazureiotoperations/graph-dataflow-window:1.1.0を使って、defaultというウィンドウ変換を追加します。
Message countを10に、境界メッセージの動作をmessageInCurrentに設定します。 タイムアウトはデフォルトの60秒のままにしておきましょう。
以下の蓄積ルールをウィンドウに追加してください:
| Input |
Output |
Expression |
assetId |
assetId |
first($1) |
value |
meanValue |
average($1) |
value |
readingCount |
count($1) |
エンドポイントfactory/meansとトピックdestinationを使って、factory-mqttという宛先を追加します。
source → only-line-1 → batch → destinationに接続し、有効化されたグラフをデプロイするために保存を選択します。
これらの完全なグラフ プロパティを graph.json として保存します。 コマンドは既存の default プロファイルを使用します。
{
"mode": "Enabled",
"nodes": [
{
"name": "source",
"nodeType": "Source",
"sourceSettings": {
"endpointRef": "factory-mqtt",
"dataSources": ["factory/values"]
}
},
{
"name": "only-line-1",
"nodeType": "Graph",
"graphSettings": {
"registryEndpointRef": "default",
"artifact": "azureiotoperations/graph-dataflow-filter:1.1.0",
"configuration": [
{
"key": "rules",
"value": "{\"filter\":[{\"inputs\":[\"assetId\"],\"expression\":\"$1 != \\\"line-1\\\"\"}]}\n"
}
]
}
},
{
"name": "batch",
"nodeType": "Graph",
"graphSettings": {
"registryEndpointRef": "default",
"artifact": "azureiotoperations/graph-dataflow-window:1.1.0",
"configuration": [
{
"key": "count",
"value": "{\"type\":\"messageCount\",\"maxMessageCount\":10,\"boundaryMessage\":\"messageInCurrent\"}"
},
{
"key": "rules",
"value": "{\n \"accumulate\": [\n {\"inputs\":[\"assetId\"],\"output\":\"assetId\",\"expression\":\"first($1)\"},\n {\"inputs\":[\"value\"],\"output\":\"meanValue\",\"expression\":\"average($1)\"},\n {\"inputs\":[\"value\"],\"output\":\"readingCount\",\"expression\":\"count($1)\"}\n ]\n}\n"
}
]
}
},
{
"name": "destination",
"nodeType": "Destination",
"destinationSettings": {
"endpointRef": "factory-mqtt",
"dataDestination": "factory/means"
}
}
],
"nodeConnections": [
{ "from": { "name": "source" }, "to": { "name": "only-line-1" } },
{ "from": { "name": "only-line-1" }, "to": { "name": "batch" } },
{ "from": { "name": "batch" }, "to": { "name": "destination" } }
]
}
az iot ops dataflowgraph apply \
--name average-line-1 \
--instance $AIO_INSTANCE_NAME \
--resource-group $RESOURCE_GROUP \
--config-file graph.json
このテンプレートを main.bicepとして保存してください。 既存のインスタンス、カスタムロケーション、 default プロファイルを参照します。
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: 'average-line-1'
extendedLocation: {
name: customLocation.id
type: 'CustomLocation'
}
properties: {
mode: 'Enabled'
nodes: [
{
name: 'source'
nodeType: 'Source'
sourceSettings: {
endpointRef: 'factory-mqtt'
dataSources: [ 'factory/values' ]
}
}
{
name: 'only-line-1'
nodeType: 'Graph'
graphSettings: {
registryEndpointRef: 'default'
artifact: 'azureiotoperations/graph-dataflow-filter:1.1.0'
configuration: [
{
key: 'rules'
value: '{"filter":[{"inputs":["assetId"],"expression":"$1 != \\"line-1\\""}]}\n'
}
]
}
}
{
name: 'batch'
nodeType: 'Graph'
graphSettings: {
registryEndpointRef: 'default'
artifact: 'azureiotoperations/graph-dataflow-window:1.1.0'
configuration: [
{
key: 'count'
value: '{"type":"messageCount","maxMessageCount":10,"boundaryMessage":"messageInCurrent"}'
}
{
key: 'rules'
value: '{\n "accumulate": [\n {"inputs":["assetId"],"output":"assetId","expression":"first($1)"},\n {"inputs":["value"],"output":"meanValue","expression":"average($1)"},\n {"inputs":["value"],"output":"readingCount","expression":"count($1)"}\n ]\n}\n'
}
]
}
}
{
name: 'destination'
nodeType: 'Destination'
destinationSettings: {
endpointRef: 'factory-mqtt'
dataDestination: 'factory/means'
}
}
]
nodeConnections: [
{
from: { name: 'source' }
to: { name: 'only-line-1' }
}
{
from: { name: 'only-line-1' }
to: { name: 'batch' }
}
{
from: { name: 'batch' }
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 配置マニフェストの使用は運用環境ではサポートされていないため、デバッグとテストにのみ使用する必要があります。
以下のマニフェストを average-line-1.yamlとして保存してください。
apiVersion: connectivity.iotoperations.azure.com/v1
kind: DataflowGraph
metadata:
name: average-line-1
namespace: azure-iot-operations
spec:
profileRef: default
mode: Enabled
nodes:
- name: source
nodeType: Source
sourceSettings:
endpointRef: factory-mqtt
dataSources: [factory/values]
- name: only-line-1
nodeType: Graph
graphSettings:
registryEndpointRef: default
artifact: azureiotoperations/graph-dataflow-filter:1.1.0
configuration:
- key: rules
value: |
{"filter":[{"inputs":["assetId"],"expression":"$1 != \"line-1\""}]}
- name: batch
nodeType: Graph
graphSettings:
registryEndpointRef: default
artifact: azureiotoperations/graph-dataflow-window:1.1.0
configuration:
- key: count
value: '{"type":"messageCount","maxMessageCount":10,"boundaryMessage":"messageInCurrent"}'
- key: rules
value: |
{
"accumulate": [
{"inputs":["assetId"],"output":"assetId","expression":"first($1)"},
{"inputs":["value"],"output":"meanValue","expression":"average($1)"},
{"inputs":["value"],"output":"readingCount","expression":"count($1)"}
]
}
- name: destination
nodeType: Destination
destinationSettings:
endpointRef: factory-mqtt
dataDestination: factory/means
nodeConnections:
- from: { name: source }
to: { name: only-line-1 }
- from: { name: only-line-1 }
to: { name: batch }
- from: { name: batch }
to: { name: destination }
kubectl apply -f average-line-1.yaml
以下の例の各行を別々のメッセージとしてfactory/valuesに公開してください。
{"assetId":"line-1","value":9}
{"assetId":"line-1","value":11}
{"assetId":"line-1","value":10}
{"assetId":"line-1","value":10}
{"assetId":"line-1","value":9}
{"assetId":"line-1","value":11}
{"assetId":"line-1","value":10}
{"assetId":"line-1","value":10}
{"assetId":"line-1","value":9}
{"assetId":"line-1","value":11}
フィルターは他のアセットをドロップします。 もしそれ以前の部分バッチがなければ、10回目の読み取りがウィンドウを閉じ、 factory/means (100 / 10 = 10)にペイロードを生成します。
{"assetId":"line-1","meanValue":10,"readingCount":10}
前述の 10 件のメッセージによる結果を得るには、既定のタイムアウトである 60 秒以内に、10 件の読み取り値を送信してください。 タイムアウトはより小さなバッチを発行でき、それが readingCount によって可視化されます。 平均を使う前に、センサーペイロードに数値が含まれていることを確認する。
1秒間に10回の読み取りでは、10回の読み取りを収集するのに約1秒かかります。入力が遅くなると収集遅延が増加します。 デフォルトのタイムアウトを変更するには、明示 的な期間ベースのクロージング条件 を設定し、下流の部分的なバッチを考慮してください。
各測定後に平滑化された値は、Azure Samplesのデプロイ指示にある指数移動平均サンプルを参照してください。
関連するコンテンツ