Node-RED Batchノード ガイド

☁️ クラウドOK 環境とは?

このガイドでは、Node-REDのBatchノードについて、初心者の方でも理解できるように詳しく説明します。

1. Batchノードとは?

「Batch」って何?

Batchノードは、連続するメッセージをグループ化して新しいメッセージシーケンスを作成するノードです。 日常生活で例えると、工場のベルトコンベアで製品を箱詰めする作業のようなものです。

工場の箱詰めラインに例えると:

基本的な動作

Function
(送信)
→→→ Batch →→→ Join → Debug

Batchノードは、連続するメッセージを受け取り、指定した条件でグループ化したメッセージシーケンスを出力します。 出力されるのはmsg.partsを持つメッセージシーケンスなので、Joinノードと組み合わせて配列にまとめることができます。

入力(30個のメッセージ)
0 → 1 → 2 → 3 → 4 → 5 → 6 → ... → 29
↓ Batch(5個ずつ) ↓
出力(6つのグループ)
[0,1,2,3,4] → [5,6,7,8,9] → ... → [25,26,27,28,29]

動作を図で見る

batch ノードは、次々に届く msg を決まった件数や時間で区切り、「列」として扱えるように印を付けます。中身を 1 つにまとめるには、後ろに join ノードを置きます。

3 件たまったら、その 3 件を送り出す(中身はまとめない)batch(3件ずつ)入ってくる出ていくAABBCCDDEEFF列 1(parts.index 0〜2)列 2(parts.index 0〜2)
3 件ずつの列に区切られ、各 msg に列の情報が付く

2. Batchノードの3つのモード

数量 数量ベースモード

指定した数のメッセージをグループ化

N個ずつまとめて シーケンス作成 オーバラップ可能

時間 時間ベースモード

指定時間内のメッセージをグループ化

N秒間に届いた メッセージを1グループに

連結 連結モード

複数のシーケンスを結合

トピック別の シーケンスを順番に連結

設定項目一覧

Batch ノードは上部で選んだ「モード」によって設定欄が切り替わる。まずモードを選び、続いてそのモード専用の項目を設定する。

共通(モードの選択)

設定項目 説明 既定値
モード グループ化のやり方を選ぶ。メッセージ数でグループ化は一定件数ごと、時間間隔でグループ化は一定秒数ごと、列の結合は複数トピックのシーケンスを指定順に連結する。 メッセージ数でグループ化
名前 ノードに付ける表示名。指定するとフロー上のラベルになる。 (空)

「モード」=メッセージ数でグループ化のときの項目

設定項目 説明 既定値
メッセージ数 1 グループにまとめるメッセージの件数。 10
オーバラップ 隣り合うグループ間で重複させるメッセージの件数。移動平均のように直前の値を引き継ぎたいときに使う。 0
msg.partsを用いたbatch操作を許可 入力に付いている msg.parts のシーケンス情報を考慮してグループ化する。 オフ

「モード」=時間間隔でグループ化のときの項目

設定項目 説明 既定値
時間間隔(秒) 1 グループにまとめる時間の長さ(秒)。 10
メッセージを受信しない場合、空のメッセージを送信 その間隔に 1 件も届かなくても、空のシーケンスを送り出す。 オフ

「モード」=列の結合のときの項目

設定項目 説明 既定値
トピック 連結するシーケンスのトピックを順番に並べる。ここに書いた順序で各トピックのメッセージ列がつながる。 (空 1 件)

オーバラップの動作

数量ベースモードでは、グループ間でメッセージを共有(オーバラップ)できます。

オーバラップなし(overlap=0):

0 1 2 3 4 | 5 6 7 8 9 | 10...

オーバラップあり(overlap=1):

0 1 2 3 4 5 6 7 8 9...
グループ1: [0,1,2,3,4] → グループ2: [4,5,6,7,8] → グループ3: [8,9,10,11,12]...

オーバラップの活用例:

3. 実用的な使用パターン

サンプルフローのインポート方法:

  1. 下のサンプルフローJSONをコピー
  2. Node-REDエディタで メニュー → 読み込み を選択
  3. JSONをペーストして「読み込み」をクリック

このサンプルフローには、以下で説明する全パターンの実例が含まれています。

サンプルフローは、Node-RED本体に同梱されている公式サンプル(「Number-based group mode」「Time-based group mode」「Concatenate mode」の3つ)を1つのタブにまとめ、名前の無かった inject ノードと debug ノードに日本語の名前を付けたものです。エディタの メニュー → 読み込み → サンプル → node-red → sequence → batch からも、元の3つを開けます。

📋 サンプルフロー(クリックで展開)

参照元:NodeREDエディター内サンプルフロー

[ { "id": "9bc9b0abf92b9e57", "type": "tab", "label": "batch", "disabled": false, "info": "", "env": [] }, { "id": "74853568.22b87c", "type": "batch", "z": "9bc9b0abf92b9e57", "name": "", "mode": "count", "count": "5", "overlap": 0, "interval": "5", "allowEmptySequence": false, "topics": [], "x": 410, "y": 260, "wires": [ [ "8f4f683.99d1998" ] ] }, { "id": "3d208473.f31e1c", "type": "comment", "z": "9bc9b0abf92b9e57", "name": "Group 5 consecutive messages", "info": "", "x": 210, "y": 88, "wires": [] }, { "id": "8f4f683.99d1998", "type": "join", "z": "9bc9b0abf92b9e57", "name": "", "mode": "auto", "build": "string", "property": "payload", "propertyType": "msg", "key": "topic", "joiner": "\\n", "joinerType": "str", "accumulate": "false", "timeout": "", "count": "", "reduceRight": false, "x": 550, "y": 260, "wires": [ [ "6c47ccb3.bb0184" ] ] }, { "id": "6c47ccb3.bb0184", "type": "debug", "z": "9bc9b0abf92b9e57", "name": "結果: 5個ずつ", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "false", "x": 710, "y": 260, "wires": [] }, { "id": "49c2ac1.59a9354", "type": "comment", "z": "9bc9b0abf92b9e57", "name": "↑ create message sequence with 5 messages", "info": "", "x": 530, "y": 300, "wires": [] }, { "id": "311dd6b4.5aeb7a", "type": "comment", "z": "9bc9b0abf92b9e57", "name": "↓ join sequence to array", "info": "", "x": 600, "y": 220, "wires": [] }, { "id": "e27c55b0.18e9c8", "type": "inject", "z": "9bc9b0abf92b9e57", "name": "実行: 5個ずつ", "topic": "", "payload": "", "payloadType": "date", "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "x": 180, "y": 180, "wires": [ [ "9e65f29a.69ca2" ] ] }, { "id": "9e65f29a.69ca2", "type": "function", "z": "9bc9b0abf92b9e57", "name": "send: 0-29", "func": "for(var x = 0; x < 30; x++) {\n node.send({payload: x});\n}", "outputs": 1, "noerr": 0, "x": 350, "y": 180, "wires": [ [ "74853568.22b87c" ] ] }, { "id": "817acbfb.452af8", "type": "comment", "z": "9bc9b0abf92b9e57", "name": "↓ send sequence: 0-29", "info": "", "x": 380, "y": 140, "wires": [] }, { "id": "53645699.a35c48", "type": "batch", "z": "9bc9b0abf92b9e57", "name": "", "mode": "count", "count": "5", "overlap": "1", "interval": "5", "allowEmptySequence": false, "topics": [], "x": 410, "y": 528, "wires": [ [ "4cb873e6.f9996c" ] ] }, { "id": "ecff527d.d64cb", "type": "comment", "z": "9bc9b0abf92b9e57", "name": "Group 5 consecutive messages with overlap of 1 msg", "info": "", "x": 280, "y": 356, "wires": [] }, { "id": "4cb873e6.f9996c", "type": "join", "z": "9bc9b0abf92b9e57", "name": "", "mode": "auto", "build": "string", "property": "payload", "propertyType": "msg", "key": "topic", "joiner": "\\n", "joinerType": "str", "accumulate": "false", "timeout": "", "count": "", "reduceRight": false, "x": 550, "y": 528, "wires": [ [ "51089be3.4ecbf4" ] ] }, { "id": "51089be3.4ecbf4", "type": "debug", "z": "9bc9b0abf92b9e57", "name": "結果: 5個ずつ・1個重ねる", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "false", "x": 710, "y": 528, "wires": [] }, { "id": "84c34533.6284a8", "type": "comment", "z": "9bc9b0abf92b9e57", "name": "↑ create message sequence with 5 messages with overlap of 1 msg", "info": "", "x": 600, "y": 568, "wires": [] }, { "id": "c7241026.18245", "type": "comment", "z": "9bc9b0abf92b9e57", "name": "↓ join sequence to array", "info": "", "x": 600, "y": 488, "wires": [] }, { "id": "67d24449.028eec", "type": "inject", "z": "9bc9b0abf92b9e57", "name": "実行: 5個ずつ・1個重ねる", "topic": "", "payload": "", "payloadType": "date", "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "x": 180, "y": 448, "wires": [ [ "5d909bfb.6faf44" ] ] }, { "id": "5d909bfb.6faf44", "type": "function", "z": "9bc9b0abf92b9e57", "name": "send: 0-29", "func": "for(var x = 0; x < 30; x++) {\n node.send({payload: x});\n}", "outputs": 1, "noerr": 0, "x": 350, "y": 448, "wires": [ [ "53645699.a35c48" ] ] }, { "id": "e7af9fbb.f30ad", "type": "comment", "z": "9bc9b0abf92b9e57", "name": "↓ send sequence: 0-29", "info": "", "x": 380, "y": 408, "wires": [] }, { "id": "f4a38f7b.8d62f", "type": "comment", "z": "9bc9b0abf92b9e57", "name": "Example: Number-based Group Mode", "info": "*Number-based Group mode* of batch node can be used to create new message sequences from incoming messages. Recently received *N*-messages are grouped to a sequence. Creating message sequences that has overwrap with adjacent message group is possible.\n", "x": 190, "y": 40, "wires": [] }, { "id": "e3a5c066.16333", "type": "batch", "z": "9bc9b0abf92b9e57", "name": "", "mode": "interval", "count": 10, "overlap": 0, "interval": "5", "allowEmptySequence": false, "topics": [], "x": 390, "y": 860, "wires": [ [ "9223c119.c5268" ] ] }, { "id": "606bb329.983c9c", "type": "comment", "z": "9bc9b0abf92b9e57", "name": "Group messages received within 5s", "info": "", "x": 220, "y": 680, "wires": [] }, { "id": "9223c119.c5268", "type": "join", "z": "9bc9b0abf92b9e57", "name": "", "mode": "auto", "build": "string", "property": "payload", "propertyType": "msg", "key": "topic", "joiner": "\\n", "joinerType": "str", "accumulate": "false", "timeout": "", "count": "", "reduceRight": false, "x": 530, "y": 860, "wires": [ [ "e4d07fa1.78c16" ] ] }, { "id": "e4d07fa1.78c16", "type": "debug", "z": "9bc9b0abf92b9e57", "name": "結果: 5秒ごと", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "false", "x": 690, "y": 860, "wires": [] }, { "id": "848f59e5.7528d8", "type": "comment", "z": "9bc9b0abf92b9e57", "name": "↑ create message sequence received within 5s", "info": "", "x": 520, "y": 900, "wires": [] }, { "id": "cf1bbb5e.ba3e68", "type": "comment", "z": "9bc9b0abf92b9e57", "name": "↓ join sequence to array", "info": "", "x": 580, "y": 820, "wires": [] }, { "id": "a1e311d5.4dca1", "type": "inject", "z": "9bc9b0abf92b9e57", "name": "実行: 5秒ごと", "topic": "", "payload": "", "payloadType": "date", "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "x": 180, "y": 760, "wires": [ [ "97da73f2.ae01e" ] ] }, { "id": "97da73f2.ae01e", "type": "function", "z": "9bc9b0abf92b9e57", "name": "send: 0-29", "func": "for(var x = 0; x < 30; x++) {\n node.send({payload: x});\n}", "outputs": 1, "noerr": 0, "x": 350, "y": 760, "wires": [ [ "2776c823.77eba8" ] ] }, { "id": "2776c823.77eba8", "type": "delay", "z": "9bc9b0abf92b9e57", "name": "", "pauseType": "rate", "timeout": "1", "timeoutUnits": "seconds", "rate": "1", "nbRateUnits": "1", "rateUnits": "second", "randomFirst": "1", "randomLast": "5", "randomUnits": "seconds", "drop": false, "x": 530, "y": 760, "wires": [ [ "e3a5c066.16333" ] ] }, { "id": "93520648.6a7988", "type": "comment", "z": "9bc9b0abf92b9e57", "name": "↓ send sequence: 0-29", "info": "", "x": 380, "y": 720, "wires": [] }, { "id": "5842d64f.9fc608", "type": "comment", "z": "9bc9b0abf92b9e57", "name": "Example: Time-based Group Mode", "info": "*Time-based Group mode* of batch node can be used to create new message sequences from incoming messages received within specified time range. \n", "x": 180, "y": 620, "wires": [] }, { "id": "cf1fd796.197678", "type": "inject", "z": "9bc9b0abf92b9e57", "name": "実行: 2回つなげる", "topic": "SEQ", "payload": "[1,2,3,4,5]", "payloadType": "json", "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "x": 200, "y": 1040, "wires": [ [ "d1c8ddf0.99b4e" ] ] }, { "id": "d1c8ddf0.99b4e", "type": "split", "z": "9bc9b0abf92b9e57", "name": "", "splt": "\\n", "spltType": "str", "arraySplt": 1, "arraySpltType": "len", "stream": false, "addname": "", "x": 370, "y": 1040, "wires": [ [ "f727a5d3.ea1a28" ] ] }, { "id": "f727a5d3.ea1a28", "type": "batch", "z": "9bc9b0abf92b9e57", "name": "", "mode": "concat", "count": 10, "overlap": 0, "interval": 10, "allowEmptySequence": false, "topics": [ { "topic": "SEQ" }, { "topic": "SEQ" } ], "x": 510, "y": 1040, "wires": [ [ "f4d6dba4.7e8ab8" ] ] }, { "id": "f4d6dba4.7e8ab8", "type": "join", "z": "9bc9b0abf92b9e57", "name": "", "mode": "auto", "build": "string", "property": "payload", "propertyType": "msg", "key": "topic", "joiner": "\\n", "joinerType": "str", "accumulate": "false", "timeout": "", "count": "", "reduceRight": false, "x": 650, "y": 1040, "wires": [ [ "31b81865.611788" ] ] }, { "id": "31b81865.611788", "type": "debug", "z": "9bc9b0abf92b9e57", "name": "結果: 2回つなげる", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "false", "x": 810, "y": 1040, "wires": [] }, { "id": "8ad6b9c.7ee8548", "type": "comment", "z": "9bc9b0abf92b9e57", "name": "Duplicate", "info": "", "x": 140, "y": 1000, "wires": [] }, { "id": "5137b2d0.f4838c", "type": "inject", "z": "9bc9b0abf92b9e57", "name": "実行: 負→正の順につなげる", "topic": "SEQ", "payload": "[1,-6,-8,7,2,-3]", "payloadType": "json", "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "x": 200, "y": 1160, "wires": [ [ "408f3032.eafc1" ] ] }, { "id": "408f3032.eafc1", "type": "split", "z": "9bc9b0abf92b9e57", "name": "", "splt": "\\n", "spltType": "str", "arraySplt": 1, "arraySpltType": "len", "stream": false, "addname": "", "x": 370, "y": 1160, "wires": [ [ "dbd6e8b8.cbf2b8" ] ] }, { "id": "c70a7fad.9ee08", "type": "comment", "z": "9bc9b0abf92b9e57", "name": "Filter & Concat", "info": "", "x": 160, "y": 1120, "wires": [] }, { "id": "dbd6e8b8.cbf2b8", "type": "switch", "z": "9bc9b0abf92b9e57", "name": "", "property": "payload", "propertyType": "msg", "rules": [ { "t": "gt", "v": "0", "vt": "num" }, { "t": "else" } ], "checkall": "true", "repair": true, "outputs": 2, "x": 430, "y": 1220, "wires": [ [ "c11e5c5f.876d6" ], [ "e99c703b.f404" ] ] }, { "id": "356c8654.2ad1aa", "type": "comment", "z": "9bc9b0abf92b9e57", "name": "↑ Duplicate SEQ", "info": "", "x": 540, "y": 1080, "wires": [] }, { "id": "c11e5c5f.876d6", "type": "change", "z": "9bc9b0abf92b9e57", "name": "Topic←POS", "rules": [ { "t": "set", "p": "topic", "pt": "msg", "to": "POS", "tot": "str" } ], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 590, "y": 1180, "wires": [ [ "3412e439.eda55c" ] ] }, { "id": "e99c703b.f404", "type": "change", "z": "9bc9b0abf92b9e57", "name": "Topic←NEG", "rules": [ { "t": "set", "p": "topic", "pt": "msg", "to": "NEG", "tot": "str" } ], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 590, "y": 1240, "wires": [ [ "3412e439.eda55c" ] ] }, { "id": "3412e439.eda55c", "type": "batch", "z": "9bc9b0abf92b9e57", "name": "", "mode": "concat", "count": 10, "overlap": 0, "interval": 10, "allowEmptySequence": false, "topics": [ { "topic": "NEG" }, { "topic": "POS" } ], "x": 750, "y": 1220, "wires": [ [ "7f1ce95c.7ddbc8" ] ] }, { "id": "e6f01877.16d558", "type": "debug", "z": "9bc9b0abf92b9e57", "name": "結果: 負→正の順につなげる", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "false", "x": 970, "y": 1160, "wires": [] }, { "id": "7f1ce95c.7ddbc8", "type": "join", "z": "9bc9b0abf92b9e57", "name": "", "mode": "auto", "build": "string", "property": "payload", "propertyType": "msg", "key": "topic", "joiner": "\\n", "joinerType": "str", "accumulate": "false", "timeout": "", "count": "", "reduceRight": false, "x": 810, "y": 1160, "wires": [ [ "e6f01877.16d558" ] ] }, { "id": "c548f2c.641141", "type": "comment", "z": "9bc9b0abf92b9e57", "name": "↑ Order sequence: negative→positive", "info": "", "x": 850, "y": 1260, "wires": [] }, { "id": "c571b56c.ae63b8", "type": "comment", "z": "9bc9b0abf92b9e57", "name": "Filter pos/neg and make separate sequence↑ (but not a simple sort) ", "info": "", "x": 360, "y": 1280, "wires": [] }, { "id": "c851c021.a9688", "type": "comment", "z": "9bc9b0abf92b9e57", "name": "Example: Concatenate Mode", "info": "*Concatenate mode* of batch node can be used to combine input message sequences to create a new message sequence. Order of the sequences can be specified using message topic assigned to each message in a sequence. Message sequence can be specified multiple times.\n", "x": 160, "y": 940, "wires": [] } ]

フローの読み解き

フローには、「Example:」で始まる comment ノードが3つあり、その下に、batch ノードのモードごとの例が並んでいます。例は全部で5つです。どの例も、「実行: …」という名前の inject のボタンで動かし、同じ名前の「結果: …」の debug に結果が出ます。

例(inject の名前と、上にある comment の見出し) ノードの並び batch の設定 パターン
実行: 5個ずつ
Group 5 consecutive messages(続けて届いた5個をまとめる)
inject →「send: 0-29」(function)→ batch → join → debug 「モード」は「メッセージ数でグループ化」。「メッセージ数」は 5、「オーバラップ」は 0 パターン1
実行: 5個ずつ・1個重ねる
Group 5 consecutive messages with overlap of 1 msg(1個重ねて、5個ずつまとめる)
上と同じ 「メッセージ数」は 5、「オーバラップ」は 1 パターン2
実行: 5秒ごと
Group messages received within 5s(5秒の間に届いたものをまとめる)
inject →「send: 0-29」(function)→ delay → batch → join → debug 「モード」は「時間間隔でグループ化」。「時間間隔」は 5 秒 パターン3
実行: 2回つなげる
Duplicate(同じ列を2回つなげる)
inject → split → batch → join → debug 「モード」は「列の結合」。「トピック」は SEQ、SEQ(同じものを2回) パターン4
実行: 負→正の順につなげる
Filter & Concat(振り分けてから、つなげる)
inject → split → switch →「Topic←POS」または「Topic←NEG」(change)→ batch → join → debug 「モード」は「列の結合」。「トピック」は NEG、POS の順 パターン4

batch の前後にあるノードは、batch の動きを見えるようにするための部品です。

ノード 役割と、そこに置いてある理由
「send: 0-29」(function) 0〜29 の数を msg.payload に入れた msg を、30個続けて送る。batch に、続けて届く msg を渡すために置いてある
batch の後ろの join batch は、まとめた msg を1つの配列にはしない。同じグループであることを示す印(msg.parts)を付けて、1個ずつ送り出す。join(「動作」は「自動」)が、その印を見て1つの配列にまとめる。どの msg が同じグループになったかを、debug で見やすくするために置いてある
delay(「実行: 5秒ごと」の例) 「メッセージの流量制限」で、1秒に1個ずつに絞る。function が一度に送った30個を、1秒おきに batch へ届ける。時間でまとめる様子が見えるようにするために置いてある
split(「列の結合」の2つの例) inject が送る配列を、要素1個ずつの msg に分け、msg.parts(どの列の、全部で何個のうちの何番目か)を付ける。「列の結合」モードは、「トピック」に書いたトピックの msg に msg.parts が無いとエラーにするので、前に split が要る
switch と change(「実行: 負→正の順につなげる」の例) switch が、0 より大きい数とそれ以外に分ける。change が、msg.topic に POS(正)または NEG(負)を入れる。switch は「メッセージ列の補正」をオンにしてあり、分けた後のそれぞれを、新しい列として数え直す。batch は、このトピックで列を見分ける

ボタンを押すと出るもの:

押すボタン 同じ名前の「結果: …」に出るもの
実行: 5個ずつ [0,1,2,3,4]、[5,6,7,8,9]、…、[25,26,27,28,29] の6つ
実行: 5個ずつ・1個重ねる [0,1,2,3,4]、[4,5,6,7,8]、[8,9,10,11,12]、…、[24,25,26,27,28] の7つ。各配列の最後の数が、次の配列の先頭にも入る。最後の 29 は、次の5個がそろわないので出てこない(重ねる分の 28 と一緒に batch の中に残り、次にボタンを押したときの先頭に入る。2回目は [28,29,0,1,2] から始まる)
実行: 5秒ごと 約30秒かけて、5秒ごとに1つずつ出る。例: [0,1,2]、[3,4,5,6,7]、…、[23,24,25,26,27]、[28,29]。最初と最後の個数は、ボタンを押すタイミングで変わる。5秒の区切りは、ボタンを押したときからではなく、デプロイしたときから数えているため
実行: 2回つなげる [1,2,3,4,5,1,2,3,4,5]。「トピック」に SEQ を2回書いてあるので、同じ列が2回つながる
実行: 負→正の順につなげる [-6,-8,-3,1,7,2]。入力の [1,-6,-8,7,2,-3] のうち、負の数の列が先、正の数の列が後になる。それぞれの列の中の順番は、入力のまま。大きさの順に並べ替えているのではない

パターン1: 数量ベースのグループ化

用途: 連続メッセージを指定数ずつグループ化

Inject → Function
(0-29送信)
→→→ Batch
(count=5)
→→→ Join → Debug

動作の流れ:

  1. Functionノードで0〜29の30メッセージを連続送信
  2. Batchノードが5メッセージずつグループ化
  3. 6つのメッセージシーケンスを出力(各5メッセージ)
  4. JoinノードでそれぞれをJSON配列に変換

設定例:

モード: メッセージ数でグループ化 メッセージ数: 5 オーバラップ: 0(重複なし) 入力: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ... , 29 (30メッセージ) 出力: [0,1,2,3,4], [5,6,7,8,9], ... , [25,26,27,28,29] (6グループ)

パターン2: オーバラップ付きグループ化

用途: 隣接グループでメッセージを共有(移動平均など)

Inject → Function
(0-29送信)
→→→ Batch
(count=5, overlap=1)
→→→ Join → Debug

オーバラップの効果:

設定例:

モード: メッセージ数でグループ化 メッセージ数: 5 オーバラップ: 1 入力: 0, 1, 2, ..., 29 (30メッセージ) 出力: グループ1: [0,1,2,3,4] グループ2: [4,5,6,7,8] ← 4が共有 グループ3: [8,9,10,11,12] ← 8が共有 ... グループ7: [24,25,26,27,28] (29 は、次の5個がそろわないので出力されない。重ねる分の 28 と一緒に batch の中に残る)

パターン3: 時間ベースのグループ化

用途: 一定時間内に届いたメッセージのまとめ

Inject → Function → Delay
(1msg/sec)
→→→ Batch
(5秒間隔)
→→→ Join → Debug

時間ベースのポイント:

設定例:

モード: 時間間隔でグループ化 時間間隔: 5(秒) メッセージを受信しない場合、空のメッセージを送信: オフ 入力: 1秒間隔で 0, 1, 2, 3, ... , 29 出力: 5秒ごとに、その間に届いた分 例: [0,1,2] → [3,4,5,6,7] → ... → [28,29] (最初と最後の個数は、ボタンを押すタイミングで変わる)

パターン4: 列の結合(連結モード)

用途: 複数のメッセージ列を、決めた順番でつなげて1つの列にする

Inject
(配列)
→ Split →→→ Batch
(列の結合)
→→→ Join → Debug

動作の流れ:

  1. Splitノードが配列を要素ごとの msg に分け、列の印(msg.parts)を付ける
  2. Batchノードは、「トピック」に書いたトピックの列が、すべて最後まで届くのを待つ
  3. そろったら、「トピック」に書いた順に列をつなげて、1つの列として送り出す
  4. 「トピック」に書いていないトピックの msg は、使われない

設定例:

モード: 列の結合 トピック: SEQ, SEQ(同じトピックを2回) 入力: トピック SEQ の列 1, 2, 3, 4, 5 出力: [1,2,3,4,5,1,2,3,4,5] モード: 列の結合 トピック: NEG, POS 入力: 1, -6, -8, 7, 2, -3(正の数に POS、それ以外に NEG のトピックを付ける) 出力: [-6,-8,-3,1,7,2]

4. 実践演習

演習1: 基本的なグループ化初級

課題:

1〜12の数値を3つずつグループ化してください。

💼 使いどころ: 大量のデータを一定件数ずつのまとまりに区切ってから後続の処理に渡す場面で使います。

要求仕様:

期待される動作:

成功の条件:

💡 ヒント

Injectノードの設定:

  • msg.payload: JSON型で [1,2,3,4,5,6,7,8,9,10,11,12]

Splitノードの設定:

  • 既定のまま(配列は「固定長」1 で、要素を1つずつのメッセージに分割する)
  • 12個の配列が、1〜12の12メッセージになる

Batchノードの設定:

  • モード: メッセージ数でグループ化
  • メッセージ数: 3
  • オーバラップ: 0

Joinノードの設定:

  • 動作: 自動
✅ 解答例フロー
[ { "id": "batch_node_guide_tab_1", "type": "tab", "label": "batch_node_guide 例2", "disabled": false, "info": "", "env": [] }, { "id": "ex1_inject", "type": "inject", "z": "batch_node_guide_tab_1", "name": "1〜12を送る", "props": [ { "p": "payload" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "[1,2,3,4,5,6,7,8,9,10,11,12]", "payloadType": "json", "x": 110, "y": 100, "wires": [ [ "ex1_split" ] ] }, { "id": "ex1_split", "type": "split", "z": "batch_node_guide_tab_1", "name": "1件ずつに分割", "splt": "\\n", "spltType": "str", "arraySplt": 1, "arraySpltType": "len", "stream": false, "addname": "", "property": "payload", "x": 260, "y": 100, "wires": [ [ "ex1_batch" ] ] }, { "id": "ex1_batch", "type": "batch", "z": "batch_node_guide_tab_1", "name": "3個ずつ", "mode": "count", "count": 3, "overlap": 0, "interval": 10, "allowEmptySequence": false, "honourParts": false, "topics": [], "x": 400, "y": 100, "wires": [ [ "ex1_join" ] ] }, { "id": "ex1_join", "type": "join", "z": "batch_node_guide_tab_1", "name": "配列化", "mode": "auto", "build": "array", "property": "payload", "propertyType": "msg", "key": "topic", "joiner": "\\n", "joinerType": "str", "accumulate": false, "timeout": "", "count": "", "x": 550, "y": 100, "wires": [ [ "ex1_debug" ] ] }, { "id": "ex1_debug", "type": "debug", "z": "batch_node_guide_tab_1", "name": "結果", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "payload", "targetType": "msg", "x": 690, "y": 100, "wires": [] } ]

演習2: 移動ウィンドウの実装中級

課題:

オーバラップを使って3要素の移動ウィンドウを作成してください。

💼 使いどころ: 前後のデータを一部重ねながら区切りたい、移動平均などの場面で使います。

要求仕様:

期待される動作:

成功の条件:

💡 ヒント

Inject・Splitノードの設定:

  • 演習1と同じく、Injectで配列 [1,2,3,4,5,6] を送り、Splitで6メッセージに分割する

Batchノードの設定:

  • モード: メッセージ数でグループ化
  • メッセージ数: 3
  • オーバラップ: 2(3-1=2で1要素ずつスライド)
✅ 解答例フロー
[ { "id": "batch_node_guide_tab_2", "type": "tab", "label": "batch_node_guide 例3", "disabled": false, "info": "", "env": [] }, { "id": "ex2_inject", "type": "inject", "z": "batch_node_guide_tab_2", "name": "1〜6を送る", "props": [ { "p": "payload" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "[1,2,3,4,5,6]", "payloadType": "json", "x": 110, "y": 100, "wires": [ [ "ex2_split" ] ] }, { "id": "ex2_split", "type": "split", "z": "batch_node_guide_tab_2", "name": "1件ずつに分割", "splt": "\\n", "spltType": "str", "arraySplt": 1, "arraySpltType": "len", "stream": false, "addname": "", "property": "payload", "x": 260, "y": 100, "wires": [ [ "ex2_batch" ] ] }, { "id": "ex2_batch", "type": "batch", "z": "batch_node_guide_tab_2", "name": "3個(overlap=2)", "mode": "count", "count": 3, "overlap": 2, "interval": 10, "allowEmptySequence": false, "honourParts": false, "topics": [], "x": 420, "y": 100, "wires": [ [ "ex2_join" ] ] }, { "id": "ex2_join", "type": "join", "z": "batch_node_guide_tab_2", "name": "配列化", "mode": "auto", "build": "array", "property": "payload", "propertyType": "msg", "key": "topic", "joiner": "\\n", "joinerType": "str", "accumulate": false, "timeout": "", "count": "", "x": 570, "y": 100, "wires": [ [ "ex2_debug" ] ] }, { "id": "ex2_debug", "type": "debug", "z": "batch_node_guide_tab_2", "name": "結果", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "payload", "targetType": "msg", "x": 710, "y": 100, "wires": [] } ]

演習3: 時間ベースの集約中級

課題:

2秒ごとにメッセージをまとめてください。

💼 使いどころ: 届いたメッセージを一定時間ごとにまとめてから後続の処理に渡す場面で使います。

要求仕様:

期待される動作:

成功の条件:

💡 ヒント

Inject・Splitノードの設定:

  • Injectで配列 [0,1,2,3,4,5,6,7,8,9] を送り、Splitで10メッセージに分割する。分割したメッセージは一度に流れるので、次のDelayで間隔をあける

Delayノードの設定:

  • レート制限モード
  • 2メッセージ/秒(0.5秒間隔)

Batchノードの設定:

  • モード: 時間間隔でグループ化
  • 時間間隔: 2(秒)
✅ 解答例フロー
[ { "id": "batch_node_guide_tab_3", "type": "tab", "label": "batch_node_guide 例4", "disabled": false, "info": "", "env": [] }, { "id": "ex3_inject", "type": "inject", "z": "batch_node_guide_tab_3", "name": "0〜9を送る", "props": [ { "p": "payload" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "[0,1,2,3,4,5,6,7,8,9]", "payloadType": "json", "x": 90, "y": 100, "wires": [ [ "ex3_split" ] ] }, { "id": "ex3_split", "type": "split", "z": "batch_node_guide_tab_3", "name": "1件ずつに分割", "splt": "\\n", "spltType": "str", "arraySplt": 1, "arraySpltType": "len", "stream": false, "addname": "", "property": "payload", "x": 220, "y": 100, "wires": [ [ "ex3_delay" ] ] }, { "id": "ex3_delay", "type": "delay", "z": "batch_node_guide_tab_3", "name": "2msg/sec", "pauseType": "rate", "timeout": "1", "timeoutUnits": "seconds", "rate": "2", "nbRateUnits": "1", "rateUnits": "second", "randomFirst": "1", "randomLast": "5", "randomUnits": "seconds", "drop": false, "x": 360, "y": 100, "wires": [ [ "ex3_batch" ] ] }, { "id": "ex3_batch", "type": "batch", "z": "batch_node_guide_tab_3", "name": "2秒間隔", "mode": "interval", "count": 10, "overlap": 0, "interval": 2, "allowEmptySequence": false, "honourParts": false, "topics": [], "x": 500, "y": 100, "wires": [ [ "ex3_join" ] ] }, { "id": "ex3_join", "type": "join", "z": "batch_node_guide_tab_3", "name": "配列化", "mode": "auto", "build": "array", "property": "payload", "propertyType": "msg", "key": "topic", "joiner": "\\n", "joinerType": "str", "accumulate": false, "timeout": "", "count": "", "x": 630, "y": 100, "wires": [ [ "ex3_debug" ] ] }, { "id": "ex3_debug", "type": "debug", "z": "batch_node_guide_tab_3", "name": "結果", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "payload", "targetType": "msg", "x": 770, "y": 100, "wires": [] } ]

演習4: センサーデータの移動平均上級

課題:

センサー値の3点移動平均を計算してください。

💼 使いどころ: センサーなどの計測値の変動を、直近3点の平均でならして確認する場面で使います。

要求仕様:

期待される動作:

成功の条件:

💡 ヒント

Templateノードの設定:

  • 形式: JSON
  • テンプレート: [10, 20, 30, 40, 50]

Splitノードの設定:

  • 配列を個別メッセージに分割

Batchノードの設定:

  • モード: メッセージ数でグループ化
  • メッセージ数: 3
  • オーバラップ: 2

Joinノードの設定:

  • 動作: 自動(配列に結合)

Changeノード(平均計算)の設定:

  • 「値の代入」で msg.payload に「JSONata式」型の $average(payload) を設定する
  • $average() は配列の平均値を返す JSONata の関数
✅ 解答例フロー
[ { "id": "batch_node_guide_tab_4", "type": "tab", "label": "batch_node_guide 例5", "disabled": false, "info": "", "env": [] }, { "id": "ex4_inject", "type": "inject", "z": "batch_node_guide_tab_4", "name": "実行", "props": [ { "p": "payload" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "", "payloadType": "date", "x": 90, "y": 100, "wires": [ [ "ex4_template" ] ] }, { "id": "ex4_template", "type": "template", "z": "batch_node_guide_tab_4", "name": "センサー値", "field": "payload", "fieldType": "msg", "format": "json", "syntax": "plain", "template": "[10, 20, 30, 40, 50]", "output": "json", "x": 220, "y": 100, "wires": [ [ "ex4_split" ] ] }, { "id": "ex4_split", "type": "split", "z": "batch_node_guide_tab_4", "name": "分割", "splt": "\\n", "spltType": "str", "arraySplt": 1, "arraySpltType": "len", "stream": false, "addname": "", "property": "payload", "x": 350, "y": 100, "wires": [ [ "ex4_batch" ] ] }, { "id": "ex4_batch", "type": "batch", "z": "batch_node_guide_tab_4", "name": "3点ウィンドウ", "mode": "count", "count": 3, "overlap": 2, "interval": 10, "allowEmptySequence": false, "honourParts": false, "topics": [], "x": 480, "y": 100, "wires": [ [ "ex4_join" ] ] }, { "id": "ex4_join", "type": "join", "z": "batch_node_guide_tab_4", "name": "配列化", "mode": "auto", "build": "array", "property": "payload", "propertyType": "msg", "key": "topic", "joiner": "\\n", "joinerType": "str", "accumulate": false, "timeout": "", "count": "", "x": 610, "y": 100, "wires": [ [ "ex4_avg" ] ] }, { "id": "ex4_avg", "type": "change", "z": "batch_node_guide_tab_4", "name": "平均計算", "rules": [ { "t": "set", "p": "payload", "pt": "msg", "to": "$average(payload)", "tot": "jsonata" } ], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 740, "y": 100, "wires": [ [ "ex4_debug" ] ] }, { "id": "ex4_debug", "type": "debug", "z": "batch_node_guide_tab_4", "name": "結果", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "payload", "targetType": "msg", "x": 870, "y": 100, "wires": [] } ]

5. まとめ

Batchノードの重要ポイント

よくある間違い

次のステップ

Batchノードをマスターしたら、以下のノードも学んでみましょう:

6. トラブルシューティング

よくある問題と解決方法

問題 原因 解決方法
グループが出力されない メッセージ数が足りない count数以上のメッセージを送信するか、時間モードを使用
配列にならない Joinノードがない Batchの後にJoinノードを追加
オーバラップが想定と違う オーバラップ値の理解ミス overlap = count - 移動幅 で計算
連結モードが動かない msg.topicが未設定、または msg.parts が無い Changeノードでtopicを設定する。前に Split ノードを置いて、msg.parts の付いた列にする
時間モードで空グループ 空シーケンスを許可がON 必要に応じて設定を変更
グループサイズが不均一 時間モード使用中 数量モードに変更するか、想定内として対応

7. 実務での活用例

ケース1: センサーデータの移動平均

温度センサーから1秒ごとにデータ受信 ↓ Batch(count=5, overlap=4) ↓ 5点移動平均ウィンドウ ↓ Join → Change(平均計算: JSONata の $average) ↓ ノイズを除去した温度値

ケース2: ログの定期バッチ処理

アプリケーションログが随時到着 ↓ Batch(interval=60秒) ↓ 1分間のログをまとめる ↓ Join → データベースに一括保存

ケース3: APIレート制限対策

大量のAPIリクエスト ↓ Batch(count=10) ↓ 10件ずつまとめる ↓ バルクAPIで一括送信 ↓ API呼び出し回数を1/10に削減

ケース4: IoTデバイスからのデータ集約

複数センサーからのデータ(異なるtopic) ↓ Batch(concat mode: temp, humidity, pressure) ↓ センサー種別ごとに順序付けて結合 ↓ 統合レポート生成

8. 追加リソース


このガイドが役に立ちましたら、実際のプロジェクトで練習してみてください!
Batchノードはデータ集約や移動平均計算に使えます。

参照元:NodeREDエディター内サンプルフロー

🏠