带有 mule 的平面文件中的混合记录格式
Mixed record formats in a flat file with mule
我又一次把自己搞糊涂了。
我必须接受供应商提供的平面文件,但无法重新格式化该文件。该文件包含大量固定宽度字段的记录,非常适合 mule 数据映射器。这工作正常,映射到 java POJO 以创建要处理的记录集合。
问题:文件中的最后一条记录是格式完全不同的摘要记录。问题是,格式不仅改变了数据布局,记录长度也改变了。这会导致数据映射器异常,因为记录对于格式定义来说太短了。
是否有关于如何在文件中允许这种类型的混合格式的任何想法,或者我是否需要制作自定义转换器来拆分记录并对记录长度做出反应以分离格式而不是标准的 mule 功能?
是的,我知道这种平面文件的使用方式已经过时,但处理遗留系统需要接受生成的数据,直到系统可以退役。
您可以在 csv 属性中将错误策略设置为宽松。
我对其他解决方案持开放态度,但我怀疑开箱即用的数据映射器能否实现这一点。对于我认为的一般解决方案来说有点太深奥了。
等待听到出色的解决方法,我目前的方法是使用 java 组件按记录拆分文件并提取控件值,然后通过选择控件按类型分隔记录。 Java 然后调用组件来映射字段并在每个分支中分别为特殊记录和正文记录执行业务逻辑,例如:
<file:connector name="File" autoDelete="true" streaming="true"
validateConnections="true" doc:name="File"/>
<spring:beans>
<spring:bean name="sumRec" class="com.mypackage.SummaryRecord" />
<spring:bean name="detailRec" class="com.mypackage.DetailRecord" />
</spring:beans>
<flow name="FlatToFile">
<file:inbound-endpoint path="/SourceDir/Work"
moveToDirectory="/SourceDir/Archive"
doc:name="FlatFileIn" connector-ref="File" >
<file:filename-wildcard-filter pattern="*.txt" caseSensitive="false" />
</file:inbound-endpoint>
<component class="com.mypackage.FlatFileReader" doc:name="Split file and dispatch to VM" />
</flow>
<flow name="processRecords">
<vm:inbound-endpoint exchange-pattern="one-way" path="in" doc:name="VM" />
<object-to-string-transformer doc:name="Object to String"/>
<set-variable variableName="recType" value="#message.payload.substring(0,7)]" doc:name="Variable"/>
<choice doc:name="Choice">
<when expression="flowVars.recType == '9999999'" >
<expression-component doc:name="Expression">
message.outboundProperties.recCount = app.registry.sumRec.process(message.payload);
</expression-component>
</when>
<otherwise>
<expression-component doc:name="Expression">
app.registry.detailRec.process(message.payload);
</expression-component>
</otherwise>
</choice>
</flow>
</mule>
VM 端点由 FlatFileReader Java 调用。
我又一次把自己搞糊涂了。
我必须接受供应商提供的平面文件,但无法重新格式化该文件。该文件包含大量固定宽度字段的记录,非常适合 mule 数据映射器。这工作正常,映射到 java POJO 以创建要处理的记录集合。
问题:文件中的最后一条记录是格式完全不同的摘要记录。问题是,格式不仅改变了数据布局,记录长度也改变了。这会导致数据映射器异常,因为记录对于格式定义来说太短了。
是否有关于如何在文件中允许这种类型的混合格式的任何想法,或者我是否需要制作自定义转换器来拆分记录并对记录长度做出反应以分离格式而不是标准的 mule 功能?
是的,我知道这种平面文件的使用方式已经过时,但处理遗留系统需要接受生成的数据,直到系统可以退役。
您可以在 csv 属性中将错误策略设置为宽松。
我对其他解决方案持开放态度,但我怀疑开箱即用的数据映射器能否实现这一点。对于我认为的一般解决方案来说有点太深奥了。
等待听到出色的解决方法,我目前的方法是使用 java 组件按记录拆分文件并提取控件值,然后通过选择控件按类型分隔记录。 Java 然后调用组件来映射字段并在每个分支中分别为特殊记录和正文记录执行业务逻辑,例如:
<file:connector name="File" autoDelete="true" streaming="true"
validateConnections="true" doc:name="File"/>
<spring:beans>
<spring:bean name="sumRec" class="com.mypackage.SummaryRecord" />
<spring:bean name="detailRec" class="com.mypackage.DetailRecord" />
</spring:beans>
<flow name="FlatToFile">
<file:inbound-endpoint path="/SourceDir/Work"
moveToDirectory="/SourceDir/Archive"
doc:name="FlatFileIn" connector-ref="File" >
<file:filename-wildcard-filter pattern="*.txt" caseSensitive="false" />
</file:inbound-endpoint>
<component class="com.mypackage.FlatFileReader" doc:name="Split file and dispatch to VM" />
</flow>
<flow name="processRecords">
<vm:inbound-endpoint exchange-pattern="one-way" path="in" doc:name="VM" />
<object-to-string-transformer doc:name="Object to String"/>
<set-variable variableName="recType" value="#message.payload.substring(0,7)]" doc:name="Variable"/>
<choice doc:name="Choice">
<when expression="flowVars.recType == '9999999'" >
<expression-component doc:name="Expression">
message.outboundProperties.recCount = app.registry.sumRec.process(message.payload);
</expression-component>
</when>
<otherwise>
<expression-component doc:name="Expression">
app.registry.detailRec.process(message.payload);
</expression-component>
</otherwise>
</choice>
</flow>
</mule>
VM 端点由 FlatFileReader Java 调用。