How to print record values without repeating certain fields in dataweave in CSV format based on specific conditions

巧了我就是萌 提交于 2019-12-12 03:46:51

问题


Please consider the following input sample in CSV

Company,FirstName,LastName,Email
Rate,Manule,Reaya,Reaya@egetmetus.org
Rate,sholy,Bonvgy,Bonvage@mollis.org

The output should be as follows :

Company,FirstName,LastName,Email
Rate,Manule,Reaya,Reaya@egetmetus.org
,sholy,Bonvgy,Bonvage@mollis.org

The condition would be : if the company name repeats for different records in the input, it should be null in output CSV.

Please let me know if this can be handled in Dataweave component in Mule

Below is updated code

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd">

    <configuration doc:name="Configuration">
     <expression-language>
         <global-functions>
            def getCompanyName(Company){
                 if (flowVars.companyList contains Company) {
                    return "";
                 }  else {
                    flowVars.companyList.add(Company);
                    return Company;
                 }
            }
          </global-functions>
     </expression-language>
 </configuration>
    <file:connector name="File" autoDelete="true" streaming="true" validateConnections="true" doc:name="File"/>
    <file:connector name="File1" autoDelete="true" outputAppend="true" streaming="true" validateConnections="true" doc:name="File"/>
    <flow name="csvtocsvrecordemptyFlow">
        <file:inbound-endpoint path="src\test\resources\input" connector-ref="File" responseTimeout="10000" doc:name="File"/>
        <set-variable variableName="companyList" value="[[]]" doc:name="Variable"/>
        <dw:transform-message doc:name="Transform Message">
            <dw:set-payload><![CDATA[%dw 1.0
%output application/csv header=true
---
payload map {
    CompanyName: getCompanyName($.Company),
    name:$.FirstName
}]]></dw:set-payload>
        </dw:transform-message>

        <file:outbound-endpoint path="src\test\resources\output" outputPattern="test.csv" connector-ref="File1" responseTimeout="10000" doc:name="File"/>
    </flow>
</mule>

回答1:


AFAIK, problem would be to store the used company names within memory by using just DataWeave. I don't think it can be done. But if you combine, MEL Global functions and DataWeave, you should be able to achieve this.

Add a global MEL function in mule config like below -

<configuration doc:name="Configuration">
     <expression-language>
         <global-functions>
            def getCompanyName(name){
                 if (flowVars.companyList contains name) {
                    return "";
                 }  else {
                    flowVars.companyList.add(name);
                    return name;
                 }
            }
          </global-functions>
     </expression-language>
 </configuration>

In your flow, initialize an empty array before dataweave.

<set-variable value="#[[]]" variableName="companyList" doc:name="Variable" />

Then, in dataweave, use the MEL function -

...
companyName: getCompanyName($.name)
...

You can write any custom logic in MEL function that can evaluate the name and return some value to DataWeave.




回答2:


@Akil Karthik : When I am trying the solution it is still showing the coerce binary to array error. I have already provided the MIME type as application/csv in the variable declaration.



来源:https://stackoverflow.com/questions/37112128/how-to-print-record-values-without-repeating-certain-fields-in-dataweave-in-csv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!