Pentaho中如何对比两表找差异行?Merge Rows能否无合并取差异?
Hey there! Let's break down your two Pentaho data comparison questions with practical, actionable steps— I’ve worked through these exact scenarios more times than I can count.
The core here is to first align rows using a unique identifier (like a primary key) and then check for mismatches across other columns. Here are two reliable approaches:
Approach 1: Use a Join + Filter/Calculator
- Drag two
Table Inputcomponents onto the canvas, one for each table. Make sure each pulls in the unique key column plus all columns you want to compare. - Add an
Inner Joincomponent, connect both table inputs to it. Configure the join to match rows on your unique identifier (e.g.,table1.id = table2.id). - Add a
Calculatorcomponent after the join. For every pair of columns you want to compare (e.g.,table1.namevstable2.name), create a new field with an expression likeIF(table1.name <> table2.name, 'Mismatch', 'Match'). - Use a
Filter Rowscomponent to keep only rows where at least one of the mismatch fields equals 'Mismatch'. This gives you all rows with inconsistent content.
- Drag two
Approach 2: Use Merge Rows (diff) Tool
- Again, start with two
Table Inputcomponents for your tables. Ensure both include the unique key and all columns to compare. - Add a
Sort Rowscomponent after each table input, sorting by the unique identifier (and any other columns that help align rows consistently). - Connect both sorted streams to a
Merge Rows (diff)component. Set the "Main step" to your baseline table, and the "Compare step" to the table you're checking against. - The tool will add a
flagcolumn with values likeidentical,changed,new,deleted. Use aFilter Rowscomponent to keep only rows whereflag = 'changed'—these are your inconsistent rows.
- Again, start with two
Absolutely—you don’t have to write the merged data back to the target table. The Merge Rows (diff) tool is perfect for this; you just need to focus on extracting the difference rows instead of performing a merge write. Here's how:
- Set up the base streams: As above, use
Table Inputfor both tables, addSort Rowsto each (critical for the diff tool to work correctly), and connect them toMerge Rows (diff). - Configure the diff tool:
- Set "Main step" to your original/target table, "Compare step" to the table you're comparing against.
- Under "Fields to compare", select all columns you want to check for differences.
- Extract only the difference rows:
- Add a
Filter Rowscomponent right afterMerge Rows (diff). Configure it to keep rows where theflagcolumn is eitherchanged(rows that exist in both but have mismatched content) ornew(rows present in the compare table but not the target) — adjust based on which differences you care about.
- Add a
- Output the results: Instead of connecting to a
Table Outputcomponent that writes to the target table, you can send the filtered rows to aText File Output,Excel Output, or even just aPreviewstep to inspect the differences. No data is written back to your original target table unless you explicitly add that step.
Pro tip: If you only care about rows that are missing from the target table or exist only in the target, you can also filter for new or deleted flags respectively.
内容的提问来源于stack exchange,提问作者Bommu

