如何在Mule中读取样本DAT文件并按起始标识拆分每条记录
Mule平台按记录前缀拆分DAT文件的流程实现
核心思路
先读取输入文件并按行拆分每条记录,再根据记录开头的D/A/V前缀路由到不同的输出文件。
1. 搭建基础流程
- 输入源配置:用
File连接器作为触发源,指定SEC.DAT所在的目录,设置合适的读取策略(比如一次性读取或定时轮询)。 - 拆分记录:添加
Splitter组件,用表达式#[payload.split('\n')]把整个文件内容拆分成单条记录的消息流。如果文件里有空白行,可额外加个过滤步骤,用#[payload.trim() != '']排除空记录。
2. 按前缀路由记录
添加Choice路由组件,针对每条记录的前缀做分支判断:
- 第一个分支:判断条件写
#[payload startsWith('D')],处理所有D开头的记录 - 第二个分支:判断条件写
#[payload startsWith('A')],处理所有A开头的记录 - 第三个分支:判断条件写
#[payload startsWith('V')],处理所有V开头的记录
3. 生成目标输出文件
每个分支下配置File输出连接器:
- D分支:指定输出路径为
SEC1.DAT,写入模式选CREATE_NEW(若允许覆盖则用OVERWRITE) - A分支:指定输出路径为
SEC2.DAT - V分支:指定输出路径为
SEC3.DAT
完整流程XML示例
<mule xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="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"> <!-- 文件系统配置,替换成你的实际路径 --> <file:config name="File_Config" doc:name="File Config" > <file:connection workingDir="/your/working/directory"/> </file:config> <flow name="split-sec-dat-flow"> <!-- 监听输入目录,读取SEC.DAT --> <file:listener config-ref="File_Config" path="/input/path" doc:name="Read SEC.DAT"> <scheduling-strategy> <fixed-frequency frequency="30000"/> <!-- 每30秒轮询一次,按需调整 --> </scheduling-strategy> <file:matcher filenamePattern="SEC.DAT"/> </file:listener> <!-- 按换行拆分记录 --> <splitter expression="#[payload.split('\n')]" doc:name="Split into individual records"/> <!-- 过滤空白行 --> <filter expression="#[payload.trim() != '']" doc:name="Filter empty lines"/> <!-- 按前缀路由到不同输出文件 --> <choice doc:name="Route by record prefix"> <when expression="#[payload startsWith('D')]"> <file:write config-ref="File_Config" path="/output/path/SEC1.DAT" mode="CREATE_NEW" doc:name="Write to SEC1.DAT"/> </when> <when expression="#[payload startsWith('A')]"> <file:write config-ref="File_Config" path="/output/path/SEC2.DAT" mode="CREATE_NEW" doc:name="Write to SEC2.DAT"/> </when> <when expression="#[payload startsWith('V')]"> <file:write config-ref="File_Config" path="/output/path/SEC3.DAT" mode="CREATE_NEW" doc:name="Write to SEC3.DAT"/> </when> </choice> </flow> </mule>
关键注意点
- 确保
File_Config里的路径是你实际的文件目录,且Mule应用对该目录有读写权限。 - 写入模式
CREATE_NEW会在目标文件已存在时抛出异常,若需要覆盖现有文件,把mode改成OVERWRITE即可;如果需要追加记录,用APPEND。 - 如果输入文件编码特殊,可在File连接器中指定对应的编码格式,避免乱码。
内容的提问来源于stack exchange,提问作者user19603743
相关产品推荐
相关产品推荐

