In DataWeave, is it possible to ignore a particular row in a CSV file if a condition is met.
I'm transforming a file where some rows need to be ignored because a column has a value indicating a particular row shouldn't be mapped to the output. What I want to do in this scenario is skip that row, log (if possible) and then move on to mapping the next row.
I've looked through the docs and the closest I've found to an answer is wrapping the mapping in a when otherwise
clause. I can't find anything about calling a logger or even passing that particular row to a flow for processing (so it can be logged...)
You can do filtering and logging using dataweave but that would need some additional code -
Here is my sample input csv -
Here is the complete Mule Flow, we will go over details below -
Set a flow Variable of type list, we will use this to hold filtered records.
Define a global MEL function that takes a record and some other data to create condition. In sample, filterRecords() function takes record and genderType to filter on. This is configured by adding
Configuration
element in mule-config. Global Functions. This Global function must return true or false. If the Gender type matches with the one we passed in then it will returntrue
for including record in transformation otherwise it will add that record infilteredRecords
flow variable and then returnfalse
.Now lets write a dataweave code to filter and log. Below code uses
filter
function to filter payload. For filter criteria, we will call the global function that we just created. For every record that matched the criteria in function, there should be a row in dataweave output.After dataweave transformation, we can access
filteredRecords
flow variable to find out records that are filtered and then you can do whatever you want with them.Here is the output of last two loggers in flow. You can see filtered list has all Female records while your payload i.e. DW output has all Male records because we filtered on Male.
- Hope This Helps, Cheers!
P.S.: Thanks for the question, it forced me to figure out how to it because I never did it before :)