0

我对 dataWeave 的输入:

集合(即;“订单”对象列表)

    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

对于集合中的每个“订单”对象,我需要 CSV 中的记录。

输出CSV:

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

我需要以下输出。我在我的DataWeave组件中尝试了多种组合。但无法解决。我面临的问题是,我需要每个对象的 CSV 记录。但我得到的是每个“SampleValue”的记录。请帮助我如何克服这个问题。提前致谢!!

4

1 回答 1

0

请在下面找到带有 dataweave 代码的流程

<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>

以下是上述流程的输出

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

如果您不需要在输出中添加此文件,则上述代码会生成带有标题的 CSV 文件,"header=false"如下所示。

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

java类如下图所示

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;
    }

  }

pojo如下图所示

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;
    }
}

如果您有任何问题,请告诉我。

于 2015-09-19T17:29:46.540 回答