简体   繁体   中英

object to csv conversion dataweave component mule

My input for dataWeave:

Collection(ie; List of 'Order' objects)

    1st Order Object
    ----SampleObject1    (SampleObject is also a List of Objects that is getting repeated)
          --- SampleValue1    (some 'String' attribute of 'Order' object)
          --- SampleValue2    
    ---SampleObject2
          --- SampleValue3    (some 'String' attribute of 'Order' object)
          --- SampleValue4     .
    ------SampleObject3
        --- SampleValue5     .
        --- SampleValue6
    ------SampleObject4
         --- SampleValue7
          --- SampleValue8

.

2nd Order Object
    ----SampleObject1    (SampleObject is also a List of Objects that is getting repeated)
          --- SampleValue1    (some 'String' attribute of 'Order' object)
          --- SampleValue2    
    ---SampleObject2
          --- SampleValue3    (some 'String' attribute of 'Order' object)
          --- SampleValue4     .
    ------SampleObject3
        --- SampleValue5     .
        --- SampleValue6
    ------SampleObject4
         --- SampleValue7
          --- SampleValue8

For each 'Order' object in the collection, I need a record in a CSV.

OutputCSV:

SampleValue1      SampleValue3    SampleValue5 .....   (For 1st'Order' object)
SampleValue1      SampleValue3    SampleValue5 ....   (For 2st'Order' object)
SampleValue1      SampleValue3    SampleValue5 .....  (For 3rd'Order' object)
.
.
.
.

I need the following output. I have tried multiple combinations in my DataWeave component. But couldn't solve it. The problem I am facing is, I need a record in CSV for each Object. But what I am getting is a record for each 'SampleValue'. Please help me how to overcome this issue. Thanks in advance!!

Please find the flow below with the dataweave code inside

<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8082" doc:name="HTTP Listener Configuration"/>
<flow name="testFlow1" >
     <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
    <set-payload value="#['test']" doc:name="Set Payload"/>
    <component class="org.test.MyClass" metadata:id="f70e8945-4d11-40ba-beac-dc2f352cc2ff" doc:name="Java"/> 
    <dw:transform-message doc:name="Transform Message">
        <dw:set-payload><![CDATA[%dw 1.0
%output application/csv separator=" "
---
payload map {
SampleValue1:$.OrderData1,
SampleValue2:$.OrderData2,
SampleValue3:$.OrderData3,
SampleValue4:$.OrderData4,
SampleValue5:$.OrderData5
}]]></dw:set-payload>
    </dw:transform-message>
    <async doc:name="Async">
        <file:outbound-endpoint path="C:\Naveen\OutputDump" responseTimeout="10000" doc:name="File" outputPattern="test.txt"/>
    </async>
    <set-payload value="#['&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;']#[payload]" metadata:id="1e133033-b51d-43a3-a6b5-6496862dd510" doc:name="Set Payload"/>
    <logger message="#[payload]" level="INFO" doc:name="Logger"/>
</flow>

The following is the output from the above flow

SampleValue1    SampleValue2    SampleValue3    SampleValue4    SampleValue5
orderData1  orderData2  orderData3  orderData4  orderData5
orderData1  orderData2  orderData3  orderData4  orderData5
orderData1  orderData2  orderData3  orderData4  orderData5
orderData1  orderData2  orderData3  orderData4  orderData5
orderData1  orderData2  orderData3  orderData4  orderData5

The above code generates the CSV file with the header if you don't require add this at the outuput "header=false" like below.

%output application/csv header=false,separator="    "

The java class is shown as below

package org.test;

import java.util.ArrayList;
import java.util.List;

import org.mule.api.MuleEventContext;
import org.mule.api.lifecycle.Callable;

public class MyClass implements Callable{

    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Override
    public Object onCall(MuleEventContext eventContext) throws Exception {
        MyPojo pojos = new MyPojo();
        pojos.setOrderData1("orderData1");
        pojos.setOrderData2("orderData2");
        pojos.setOrderData3("orderData3");
        pojos.setOrderData4("orderData4");
        pojos.setOrderData5("orderData5");

        List MyArray = new ArrayList();
        MyArray.add(pojos);
        MyArray.add(pojos);
        MyArray.add(pojos);
        MyArray.add(pojos);
        MyArray.add(pojos);
        return MyArray;
    }

  }

The pojo is shown as below

package org.test;

public class MyPojo {

    String OrderData1;
    String OrderData2;
    String OrderData3;
    String OrderData4;
    String OrderData5;

    public String getOrderData1() {
        return OrderData1;
    }
    public void setOrderData1(String orderData1) {
        OrderData1 = orderData1;
    }
    public String getOrderData2() {
        return OrderData2;
    }
    public void setOrderData2(String orderData2) {
        OrderData2 = orderData2;
    }
    public String getOrderData3() {
        return OrderData3;
    }
    public void setOrderData3(String orderData3) {
       OrderData3 = orderData3;
    }
    public String getOrderData4() {
        return OrderData4;
    }
    public void setOrderData4(String orderData4) {
        OrderData4 = orderData4;
    }
    public String getOrderData5() {
        return OrderData5;
    }
    public void setOrderData5(String orderData5) {
        OrderData5 = orderData5;
    }
}

Let me know if you have any issues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM