Merge 节点 · 4 种合流模式图解
第 4 章 · 第 2 节
Merge 是新手翻车率最高的节点之一——光”合并”两个字背后藏着 5 种不同语义。这一节把每种模式的输入 → 输出画清楚,看完不再踩坑。
Merge 节点的两个输入
Section titled “Merge 节点的两个输入”每个 Merge 节点有 Input 1 和 Input 2 两个端口,分别接两个上游:
🔀 merge-shape.txt
[Source A] ──→ Input 1 ┐
│── Merge ──→ output
[Source B] ──→ Input 2 ┘
输出长什么样完全取决于你选哪种 mode。
模式 1 · Append(最常用、最直观)
Section titled “模式 1 · Append(最常用、最直观)”把 Input 2 的 items 接到 Input 1 后面。1+1 → 2。
➕ merge-append.txt
Input 1: [ {a:1}, {a:2} ]
Input 2: [ {b:3}, {b:4} ]
Mode: Append
Output: [ {a:1}, {a:2}, {b:3}, {b:4} ]
──────────── ────────────
来自 Input 1 来自 Input 2
适合场景: 两条分支跑完后简单汇总(如”无论 VIP 还是免费用户都要邮件通知”)。
模式 2 · Combine by Position(按位置配对)
Section titled “模式 2 · Combine by Position(按位置配对)”第 1 个跟第 1 个配对,第 2 个跟第 2 个配对……合并成新对象。
🧩 merge-position.txt
Input 1: [ {name:"Alice"}, {name:"Bob"}, {name:"Carol"} ]
Input 2: [ {age:30}, {age:25}, {age:28} ]
Mode: Combine → By Position
Output: [ {name:"Alice", age:30}, {name:"Bob", age:25}, {name:"Carol", age:28} ]
适合场景: 两个来源数据一一对应(如先并行查”用户信息”和”用户余额”,再合并)。
⚠ 注意:要求两边 items 数量相同。不等长时缺的一边会用空值填补。
模式 3 · Combine by Matching Fields(按字段匹配,类 SQL join)
Section titled “模式 3 · Combine by Matching Fields(按字段匹配,类 SQL join)”按某个字段的值配对(类似 SQL 的 INNER JOIN)。
🔗 merge-fields.txt
Input 1: [ {id:1, name:"Alice"}, {id:2, name:"Bob"} ]
Input 2: [ {id:1, age:30}, {id:3, age:99} ]
Mode: Combine → By Matching Fields
Field from Input 1: id
Field from Input 2: id
Output: [ {id:1, name:"Alice", age:30} ]
────────────────────────────────
只保留两边 id 都匹配的(INNER JOIN)
Output mode 还有:
- Keep Unmatched From Input 1 (LEFT JOIN)
- Keep Unmatched From Both (FULL JOIN)
适合场景: 把两个有共同标识的数据集合并(如”用户表”和”订单表”按 user_id 关联)。
模式 4 · Combine All(笛卡尔积)
Section titled “模式 4 · Combine All(笛卡尔积)”每个 Input 1 跟每个 Input 2 全配对——1×N → 1*N。
✖ merge-all.txt
Input 1: [ {a:1}, {a:2} ]
Input 2: [ {b:3}, {b:4} ]
Mode: Combine → All
Output: [ {a:1, b:3}, {a:1, b:4}, {a:2, b:3}, {a:2, b:4} ]
共 2 × 2 = 4 条
适合场景: 你确实需要”组合所有可能”的少数场景(如”3 个用户 × 5 个产品 → 15 条试发邮件”)。慎用——数据量爆炸。
模式 5 · SQL Query(最灵活)
Section titled “模式 5 · SQL Query(最灵活)”直接用 SQL 写连接条件——内部用 SQLite 引擎跑。
🗄 merge-sql.txt
Mode: SQL Query
SQL:
SELECT a.id, a.name, b.age, b.balance
FROM input1 a
LEFT JOIN input2 b ON a.id = b.user_id
WHERE b.balance > 1000
Output: 按 SQL 执行结果
适合场景: 复杂联表查询,前面几种模式搞不定的。
Merge 决策树
Section titled “Merge 决策树” 🌳 merge-decision.txt
Q1: 你想要怎样的输出?
[全部 items 拼起来] ────→ Append
[两边一一对应] ─────────→ Combine by Position
[按某字段关联] ─────────→ Combine by Matching Fields
[所有组合] ────────────→ Combine All (谨慎)
[需要 SQL 级灵活度] ──→ SQL Query
本节要点回顾
Section titled “本节要点回顾”- Merge 有 5 种 mode,选错就翻车
- Append = 拼接(最常用)
- Combine by Position = 一一对应
- Combine by Matching Fields = SQL JOIN 思路
- Combine All = 笛卡尔积(慎用)
- SQL Query = 终极灵活
下一节SplitInBatches,反向操作——把大数组切成小批次。