0
votes

I have a Mule application that needs to produce some CSV output which looks like the following:

[CSV Payload 1]
Data|Data|Data
[CSV Payload 2]
Data|Data|Data|Data|Data|Data|Data|Data|Data
[CSV Payload 3]
Data|Data|Data|Data
[CSV Payload 4]
Data|Data|Data|Data|Data|Data

As you can see, I have a combination of 4 CSV payloads, each with different structures. The first two of these payloads are single line and hard coded. The third is derived from an input file and the fourth is an extract from a database.

My question is: is DataWeave suitable for achieving this or should an alternative method (such as scatter gather) be explored? I've tried to implement this in DataWeave with no luck as I'm struggling to get past the limitation of having to define an output structure.

Please note: the order of the final output needs to be Payload 1 then 2 then 3 then 4. This order cannot be mixed up.

1

1 Answers

0
votes

It is not necessary to define an output structure in DataWeave as long as the structure of the data being mapped is compatible with the MIME type defined in the output directive.

You can use a Message Enricher to obtain Payload 4 as an application/java object, and assign it to a flow variable, called for example additionalData.

Then you can used a DataWeave transformation like this, assuming the input payload is derived from the input file (i.e. Payload 3):

%dw 1.0
%output text/csv separator="|", header=false
---


 {p1-fld1: "Data", p1-fld2: "Data", p1-fld3: "Data"}  +
(
    {p2-fld1: "Data", p2-fld2: "Data", p2-fld3: "Data",
    p2-fld4: "Data", p2-fld5: "Data", p2-fld6: "Data",
    p2-fld7: "Data", p2-fld8: "Data", p2-fld9: "Data"
  } + 
  (payload + flowVars.additionalData ) 
  )

This should produce the target format you need (Payload 1 and Payload 2 are hardcoded objects in the transformation).