将Mule 4管道分隔的文本文件转换为JSON

时间:2018-07-31 05:59:20

标签: json mule dataweave

我需要在Mule 4中将管道分隔文本文件转换为JSON。我尝试使用以下代码。

`<file:listener doc:name="Fetch Input from Geometry SFTP" doc:id="1151a602-6748-43b7-b491-2caedf6f7010" directory="C:\Vijay\My Projects\AM-Mexico\US\AIM-65\Input" autoDelete="true" outputMimeType="text/csv; separator=|" recursive="false">
            <non-repeatable-stream />
            <scheduling-strategy >
                <fixed-frequency frequency="10" timeUnit="SECONDS"/>
            </scheduling-strategy>
        </file:listener>
        <ee:transform doc:name="Transform Message" doc:id="79dcddf9-34a0-4005-88b4-3a395544be8c" >
            <ee:message >
                <ee:set-payload ><![CDATA[%dw 2.0
output application/json
input payload text/csv
---
payload]]></ee:set-payload>
            </ee:message>
        </ee:transform>`

如果我尝试执行代码,则会出现如下异常。

Message               : "Internal execution exception while executing the script this is most probably a bug.
Caused by:
java.nio.channels.ClosedChannelException
    at sun.nio.ch.FileChannelImpl.ensureOpen(FileChannelImpl.java:110)
    at sun.nio.ch.FileChannelImpl.read(FileChannelImpl.java:147)
    at sun.nio.ch.ChannelInputStream.read(ChannelInputStream.java:65)
    at sun.nio.ch.ChannelInputStream.read(ChannelInputStream.java:109)
    at sun.nio.ch.ChannelInputStream.read(ChannelInputStream.java:103)
    at java.io.BufferedInputStream.read1(BufferedInputStream.java:284)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:345)
    at $java.io.InputStream$$FastClassByCGLIB$$31b19c4e.invoke(<generated>)
    at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.mule.extension.file.common.api.stream.AbstractFileInputStream.lambda$createLazyStream$0(AbstractFileInputStream.java:41)
    at $java.io.InputStream$$EnhancerByCGLIB$$55e4687e.read(<generated>)
    at org.apache.commons.io.input.ProxyInputStream.read(ProxyInputStream.java:98)
    at org.mule.weave.v2.io.DefaultSeekableStream.readUntil(SeekableStream.scala:193)
    at org.mule.weave.v2.io.DefaultSeekableStream.delegate$lzycompute(SeekableStream.scala:202)
    at org.mule.weave.v2.io.DefaultSeekableStream.delegate(SeekableStream.scala:201)
    at org.mule.weave.v2.io.DefaultSeekableStream.seek(SeekableStream.scala:234)
    at org.mule.weave.v2.io.SeekableStream.resetStream(SeekableStream.scala:17)
    at org.mule.weave.v2.io.SeekableStream.resetStream$(SeekableStream.scala:16)
    at org.mule.weave.v2.io.DefaultSeekableStream.resetStream(SeekableStream.scala:186)
    at org.mule.weave.v2.model.values.BinaryValue$.getBytesFromSeekableStream(BinaryValue.scala:84)
    at org.mule.weave.v2.model.values.BinaryValue$.getBytes(BinaryValue.scala:68)
    at org.mule.weave.v2.model.values.BinaryValue.equals(BinaryValue.scala:26)
    at org.mule.weave.v2.model.values.BinaryValue.equals$(BinaryValue.scala:25)
    at org.mule.weave.v2.module.pojo.reader.JavaBinaryValue.equals(JavaBinaryValue.scala:11)
    at org.mule.weave.v2.model.values.wrappers.DelegateValue.equals(DelegateValue.scala:38)
    at org.mule.weave.v2.model.values.wrappers.DelegateValue.equals$(DelegateValue.scala:37)
    at org.mule.weave.v2.model.values.wrappers.LazyValue.equals(DelegateValue.scala:65)
    at org.mule.weave.v2.model.types.Type.$anonfun$acceptsSchema$2(Type.scala:203)
    at org.mule.weave.v2.model.types.Type.$anonfun$acceptsSchema$2$adapted(Type.scala:198)

对此将提供任何帮助。

1 个答案:

答案 0 :(得分:1)

您需要为separator设置阅读器属性:

%dw 2.0
input payload application/csv separator='|'
output application/json
---
payload

相关文档here