【问题标题】:CSV to CSV transformation using dataweave in mule在 mule 中使用 dataweave 进行 CSV 到 CSV 转换
【发布时间】:2023-03-19 00:20:02
【问题描述】:

我必须对来自 excel 的输入数据进行一级标准化。[输入 Excel][1]。

现在输出应该类似于[输出文件][2]。我尝试结合 Dataweave 使用 MEL(将方法定义为全局配置)。在 dataweave 内部,我试图将上面定义的方法称为全局配置。这种方法不起作用,因为生成的文件是空白的。任何人都可以在这种方法中提出更好的方法或问题。 下面是代码 @马尼克:

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

<mule xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw"
    xmlns:file="http://www.mulesoft.org/schema/mule/file" 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.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd
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">

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


  <file:connector name="File" autoDelete="true" streaming="true" validateConnections="true" doc:name="File" readFromDirectory="C:\Users\Desktop\Mule-Doc"/>
    <file:connector name="File1" autoDelete="true" outputAppend="true" streaming="true" validateConnections="true" doc:name="File"/>


    <flow name="csvnormalizationFlow">
         <file:inbound-endpoint path="C:\Users\Desktop\Mule-Doc" connector-ref="File" responseTimeout="10000" doc:name="File"/>
        <set-variable value="#[[]]" variableName="companyList" doc:name="Variable" mimeType="application/csv"/>
        <logger message="The payload is-----------------------#[payload]" level="INFO" doc:name="Logger"/>
        <dw:transform-message doc:name="Transform Message">
            <dw:set-payload><![CDATA[%dw 1.0

%output application/csv header=true
---
payload map {
    CompanyName: getCompanyName($.Company),
    location:$.Location
}]]></dw:set-payload>
        </dw:transform-message>

        <file:outbound-endpoint path="C:\Users\Desktop\Output" outputPattern="test.csv" connector-ref="File1" responseTimeout="10000" doc:name="File"/>
    </flow>
</mule>

【问题讨论】:

  • 能否请您发布您的 MEL 函数和 dataweave 脚本
  • MEL 函数是 def getCompanyName(name){ if (flowVars.companyList contains name) { return "" ; } else { flowVars.companyList.add(name);返回名称; } }
  • 数据编织代码是

标签: mule dataweave


【解决方案1】:

完整答案在这里 -https://stackoverflow.com/a/37123291/5616671

您可以在这里发布您的流程 xml 吗?我运行了该代码,它给了我预期的输出。

【讨论】:

  • 我已添加代码供您查看。我正在开发社区版。
  • 您的记录器是否在数据编织之前打印有效载荷?另请从 set-variable 声明中删除 mime-type。
  • 我将&lt;file:inbound-endpoint path="C:\Users\Desktop\Mule-Doc" 更改为&lt;file:inbound-endpoint path="input"。还将输出路径更改为工作区中的 output 文件夹。然后对于带有数据Company,FirstName,LastName,Email,Location Rate,Manule,Reaya,Reaya@egetmetus.org,Test1 Rate,sholy,Bonvgy,Bonvage@mollis.org,Test2 的输入 csv,我上面的代码为我生成了输出 csv,例如 -CompanyName,location Rate,Test1 ,Test2 。您在阅读 CSV 还是 .XLS?
  • 当我运行程序时,它说“无法将二进制强制转换为数组”并且 test.csvCSV 没有数据。
  • 你的输入文件是什么? EXCEL 还是 CSV?如果您的输入是 excel (.xls/.xlsx),您将收到该错误,它将被读取为带有二进制数据的输入流。
【解决方案2】:

您可以在 Data weave 表达式中添加一个 null 处理程序,并检查它是否是不写入任何内容的有效负载。下面是一个空句柄的例子。

 (payload.listAllFlightsResponse.*return default [])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-12
    • 1970-01-01
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    • 2016-03-04
    • 1970-01-01
    • 2020-04-13
    相关资源
    最近更新 更多