How to print record values without repeating certa

2019-09-05 01: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>

2条回答
该账号已被封号
2楼-- · 2019-09-05 02:35

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.

查看更多
戒情不戒烟
3楼-- · 2019-09-05 02:48

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

查看更多
登录 后发表回答