输入文件的 Spring Batch 自定义分区器
我正在尝试对包含 ;
分隔项目的平面输入文件进行分区。 一行中的第一项表示一个类别,我想对此类别进行分区,以便为每个类别创建一个分区,该分区将由专用线程处理。 但我对如何在自定义 Partitioner
中实现此分区逻辑感到困惑。 分区似乎发生在面向块的步骤之前,因此在读取、写入之前,所以看起来我需要逐行读取自定义分区器中的文件,从该行获取类别字段并收集具有相同类别的行并创建每个集合都有一个 ExecutionContext?
我的方向正确吗? 有经验的人可以提供一个使用文件的小例子(可能是伪代码)吗?
I'm trying to partition a flat input file containing ;
separated items.
the first item on a line indicates a category and I would like to partition on this category so that for each category a partition is created that will be handled by a dedicated thread.
But I'm puzzled to how I can implement this partitioning logic in a custom Partitioner
.
The partitioning seems to happen before the chunk oriented step, thus before reading, writing, so it looks like I need to read the file in the custom partitioner line by line, get the category field from the line and collect lines with equal categories and create an ExecutionContext
for each of these collections?
Am I looking in the right direction?
Can someone with experience provide a small example using a file (may be pseudo code)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我自己刚刚提出了这个问题。我认为自定义 Partitioner 需要与自定义 ItemReader 配对。 ItemReader 使用从属步骤中的数据(由分区器放置在那里)进行初始化,因此仅读取适合该步骤的项目。
I've just hit this question myself. I think that custom Partitioner needs to be paired with a custom ItemReader. ItemReader gets initialized with data from slave step (put there by the Partitioner) and consequently only reads the items that are right for that step.
您可以在这些链接中找到一些自定义分区器实现 此处 和 此处 以获得总体想法。但我认为您无法逃避每个分区的 ExecutionContext 创建。
You can find some custom partitioner implementations in these links here and here to get an overall idea. But I think you can't flee from the ExecutionContext creation for each partition.