【问题标题】:How to print record values without repeating certain fields in dataweave in CSV format based on specific conditions如何根据特定条件打印记录值而不重复 CSV 格式的 dataweave 中的某些字段
【发布时间】:2016-05-12 16:21:32
【问题描述】:

请考虑以下 CSV 格式的输入样本

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

输出应该如下:

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

条件是:如果公司名称在输入中的不同记录中重复,则在输出 CSV 中应该为空。

请让我知道这是否可以在 Mule 的 Dataweave 组件中处理

下面是更新代码

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

【问题讨论】:

    标签: csv mule-studio dataweave


    【解决方案1】:

    AFAIK,问题是仅使用 DataWeave 将使用的公司名称存储在内存中。我不认为它可以做到。但是如果你把 MEL Global 函数和 DataWeave 结合起来,你应该可以实现这一点。

    在 mule 配置中添加一个全局 MEL 函数,如下所示 -

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

    在您的流程中,在 dataweave 之前初始化一个空数组。

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

    然后,在 dataweave 中,使用 MEL 函数 -

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

    您可以在 MEL 函数中编写任何自定义逻辑,以评估名称并将一些值返回给 DataWeave。

    【讨论】:

    • 嗨 Manik 感谢您的回复,但我得到的是空白文件。检查我下面的代码。检查我下面的代码
    • 已更正了 else 部分中的全局函数以及变量 init 以使其列出。请立即检查。
    • 仍然创建空白文件,我在这里怀疑函数调用是否正在发生?当我调试控件时甚至没有去那里。我已经更新了上面的代码。请检查并让我知道是否有错误。
    • 嗨@Akilkarthik,请将变量声明值从value=[[]] 更改为value=#[[]](我在复制时错过了)。我可以根据此代码的要求生成输出。我们定义的函数是一个全局 MEL 函数,您可以在应用程序中的任何位置使用它。可以把它想象成 #[xpath3(...)] 内置函数,但这个是由你定义的。
    • 全局函数文档 - docs.mulesoft.com/mule-user-guide/v/3.7/…。您可能无法将该函数作为 MEL 进行调试。
    【解决方案2】:

    @Akil Karthik:当我尝试解决方案时,它仍然显示强制二进制到数组错误。我已经在变量声明中提供了 MIME 类型为 application/csv。

    【讨论】:

      猜你喜欢
      • 2015-02-17
      • 2019-08-15
      • 1970-01-01
      • 1970-01-01
      • 2022-01-04
      • 2019-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多