简体   繁体   English

如何在Mule 3.8中读取纯文本文件

[英]How to read plain text file in mule 3.8

In mule 3.8, when I use file connector to process text file , I am getting exception as below, please help. 在m子3.8中,当我使用文件连接器处理文本文件时,出现以下异常,请提供帮助。

Here is flow xml 这是流xml

file:connector name="File" autoDelete="true"  streaming="true" validateConnections="true" doc:name="File"/>

flow name="DW-FixedWidth-Processing">

file:inbound-endpoint path="D:/mule/input" connector-ref="File" responseTimeout="10000" doc:name="File"/>

file:file-to-string-transformer doc:name="File to String"/>

dw:transform-message doc:name="Transform Message">

dw:input-payload />

dw:set-payload><![CDATA[%dw 1.0
%output application/csv header=false
---
    ((payload splitBy /\n/)[0..8]) map {
    location:trim $[0..14],
    sku:trim $[15..39],
    dtc:trim $[40..42],
    tt:trim $[43..44],
    txnqty:trim $[45..54],
    um:trim $[55..56],
    rcd:trim $[57..59],
    te:trim $[60..89],
    ul:trim $[90..104],
    date:trim $[105..114],
    time:trim $[115..120]
} ]]>
dw:set-payload>

dw:transform-message>

logger message="#[message.payloadAs(java.lang.String)]" level="INFO" doc:name="Logger"/>

file:outbound-endpoint path="D:/mule/output" responseTimeout="10000" doc:name="File"/>

Exception 例外

Root Exception stack trace: 根异常堆栈跟踪:

java.lang.IllegalStateException: No schema set at com.mulesoft.weave.module.flatfile.FlatFileSettings.loadSchema(FlatFileSettings.scala:45) at com.mulesoft.weave.module.flatfile.FlatFileReader.ffParser(FlatFileReader.scala:42) java.lang.IllegalStateException:com.mulesoft.weave.module.flatfile.FlatFileReader.ffParser(FlatFileReader.scala:42)处的com.mulesoft.weave.module.flatfile.FlatFileSettings.loadSchema(FlatFileSettings.scala:45)上未设置任何模式)

When you disable streaming in the file connector, you no longer need the 在文件连接器中禁用流式传输时,您不再需要

<object-to-string/>

You can configure DataWeave to use a flat file schema, as already said in the other answer. 您可以将DataWeave配置为使用平面文件架构,如其他答案中所述。

When you want to use it the way, you have to tell the correct mime type, in this case it is 当您想使用它时,必须告诉正确的mime类型,在这种情况下

application/java

You can do this on any message processor before the DataWeave, in this case the file:inbound-endpoint. 您可以在DataWeave之前的任何消息处理器上执行此操作,在本例中为file:inbound-endpoint。 Or you can do this in the DataWeave itself (unfortunately this is only visible when you switch Studio to XML). 或者,您可以在DataWeave本身中执行此操作(不幸的是,仅当您将Studio切换为XML时才可见)。

For me everything worked with the following flow: 对我来说,一切都按照以下流程进行:

    <file:connector name="File" autoDelete="false" streaming="false" validateConnections="true" doc:name="File"/>

    <flow name="dw-testFlow">
        <file:inbound-endpoint path="in" moveToDirectory="processed" connector-ref="File" responseTimeout="10000" mimeType="application/java" doc:name="File">
            <file:filename-regex-filter pattern=".*\.txt" caseSensitive="true"/>
        </file:inbound-endpoint>
        <dw:transform-message doc:name="Transform Message">
            <dw:set-payload><![CDATA[%dw 1.0
%output application/java
---
((payload splitBy /\n/)) map {
location:trim $[0..14],
sku:trim $[15..39],
dtc:trim $[40..42],
tt:trim $[43..44],
txnqty:trim $[45..54],
um:trim $[55..56],
rcd:trim $[57..59],
te:trim $[60..89],
ul:trim $[90..104],
date:trim $[105..114],
time:trim $[115..120]
}
]]></dw:set-payload>
        </dw:transform-message>
        <logger message="#[payload]" level="INFO" doc:name="Logger"/>
    </flow>

The result of the DataWeave is a List of HashMap. DataWeave的结果是HashMap的列表。

DW does not currently support mapping from an unstructured string, even if you declare MIME Type = text/plain upstream and you set the input payload metadata to String in the Transform Message processor. DW当前不支持从非结构化字符串进行映射,即使您在上游声明MIME Type =文本/纯文本,并且在“转换消息”处理器中将输入有效负载元数据设置为String也不例外。

The data formats supported (as per Mule documentation ) are Java, XML, JSON, CSV, Flat File, Excel, Fixed Width, and Cobol copybook. 支持的数据格式(根据Mule文档 )是Java,XML,JSON,CSV,平面文件,Excel,固定宽度和Cobol抄写本。

The case here is clearly a Fixed Width format. 这里的情况显然是固定宽度格式。

@Prasad, generally the error No Schema Set at throws when the input mime type is no set. @Prasad,通常在未设置输入mime类型时抛出错误No Schema Set In your case, it seems like you are dealing with fixed width data hence you either define a fixed width schema. 在您的情况下,似乎正在处理固定宽度的数据,因此您可以定义一个固定宽度的模式。 this article might help. 本文可能会有所帮助。

Instead of going to dataweave just use an groovy component to split payload by new line('\\n'). 除了使用datagroave之外,还可以使用常规组件通过新行('\\ n')分割有效负载。 Don't use file to string transformer it kills the Java-VM. 不要使用文件对字符串进行转换,这会杀死Java-VM。 Also kindly brief what you need to achieve finally in your question. 还请简要介绍您最终要达到的目标。 Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM