【发布时间】:2021-11-09 19:40:40
【问题描述】:
我有一个从输入文件(分隔)读取的工作,其中我有不同记录类型的行:
如果记录type = A 我想映射到object ObjectA,如果记录type = B 我想映射到ObjectB。ObjectA 的属性是ObjectB 的列表。
在我的输入文件中,一条记录 type A 后跟几条记录 type B:
> AS;1111111;A;otherinfo
S;11111;B;otherinfo;otherinfo1;otherinfo2
S;11111;B;otherinfo;otherinfo1;otherinfo2
AT;1111112;A;otherinfo
S;11112;B;otherinfo;otherinfo1;otherinfo2
S;11112;B;otherinfo;otherinfo1;otherinfo2
我的读者的每一个返回的项目必须是ObjectA。
我使用PatternMatchingCompositeLineMapper 作为lineMapper 实现了FlatFileItemReader。最后一个有一个list of (2) LineTokenizer 和一个list of (2) FieldSetMapper,与输入文件中每种类型的行进行特定匹配。
这很好用。
现在,为了返回 ObjectA 类型的每个项目以及 ObjectB 的相关列表,我正在尝试实现一个自定义 SingleItemPeekableItemReader 我上面的读者应该委托给......但我不知道如何根据记录类型管理peek() 应该return ObjectA 或ObjectB 的事实。
逻辑是:if peek() returns ObjectA,它是一个新项目,所以返回当前项目,if peek() returns ObjectB 这与当前项目相关,所以read() 和add ObjectB 实例到当前项目中的ObjectB 列表。
关于如何处理这种多行阅读器实现的任何建议?
也许正确的方法是使用here 描述的多行记录而不使用SingleItemPeekableItemReader?
【问题讨论】:
标签: spring spring-boot spring-batch batch-processing