在 mule 中使用 dataweave 将 CSV 转换为 CSV
CSV to CSV transformation using dataweave in mule
我必须对来自 excel 的输入数据进行一级归一化。[输入 Excel][1].
现在输出应该像[输出文件][2]。我尝试结合 Dataweave 使用 MEL(将方法定义为全局配置)。在 dataweave 中,我试图调用上面定义的方法作为全局配置。这种方法不起作用,因为生成的文件是空白的。谁能在这种方法中提出更好的方法或问题。
下面是代码
@Manik:
<?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>
完整答案在这里-
你能post你的流量xml吗?我 运行 那个代码,它给了我预期的输出。
您可以在您的数据编织表达式中添加一个空处理程序,并检查它是否是未写入任何内容的负载。下面是一个空句柄的例子。
(payload.listAllFlightsResponse.*return default [])
我必须对来自 excel 的输入数据进行一级归一化。[输入 Excel][1].
现在输出应该像[输出文件][2]。我尝试结合 Dataweave 使用 MEL(将方法定义为全局配置)。在 dataweave 中,我试图调用上面定义的方法作为全局配置。这种方法不起作用,因为生成的文件是空白的。谁能在这种方法中提出更好的方法或问题。 下面是代码 @Manik:
<?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>
完整答案在这里-
你能post你的流量xml吗?我 运行 那个代码,它给了我预期的输出。
您可以在您的数据编织表达式中添加一个空处理程序,并检查它是否是未写入任何内容的负载。下面是一个空句柄的例子。
(payload.listAllFlightsResponse.*return default [])