spring 批处理多线程在 reader 中与 writer 相同
spring batch multithread same sort in reader as writer
我们正在试验 spring 批量单线程到 spring 批量多线程。
设置相当简单:
- Reader : 读取一些特定排序的数据。
- 处理器:获取一些额外的值。
- Writer : 按读取顺序全部写入csv文件。
因此我们将 reader 更改为 JdbcPagingItemReader
并将排序从
转换为
order by firstname, lastname, id;
到
Map<String, Order> sortConfiguration = new HashMap<>();
sortConfiguration.put("firstname", Order.ASCENDING);
sortConfiguration.put("lastname", Order.ASCENDING);
sortConfiguration.put("id", Order.ASCENDING);
commit-interval
设置为 200。
批处理运行良好,但我们的 csv 完全出错了。
我假设 spring 会在每次提交后写入文件(并希望他按顺序逐页写入),但混乱大于 200 行。
例如,我得到第 1、3 和 5 行应该在一个线程中,而第 2 和 4 行应该在另一个线程中。
是否有保留顺序的选项或者是放弃多线程的唯一方法?
多线程与排序不兼容。如果您使用多线程步骤,项目将以未定义的顺序读取、处理和写入。参考文档的Multi-threaded Step部分提到了这一点:
The result of the above configuration is that the Step executes by reading, processing,
and writing each chunk of items (each commit interval) in a separate thread of execution.
Note that this means there is no fixed order for the items to be processed, and a chunk
might contain items that are non-consecutive compared to the single-threaded case.
我们正在试验 spring 批量单线程到 spring 批量多线程。
设置相当简单:
- Reader : 读取一些特定排序的数据。
- 处理器:获取一些额外的值。
- Writer : 按读取顺序全部写入csv文件。
因此我们将 reader 更改为 JdbcPagingItemReader
并将排序从
order by firstname, lastname, id;
到
Map<String, Order> sortConfiguration = new HashMap<>();
sortConfiguration.put("firstname", Order.ASCENDING);
sortConfiguration.put("lastname", Order.ASCENDING);
sortConfiguration.put("id", Order.ASCENDING);
commit-interval
设置为 200。
批处理运行良好,但我们的 csv 完全出错了。
我假设 spring 会在每次提交后写入文件(并希望他按顺序逐页写入),但混乱大于 200 行。
例如,我得到第 1、3 和 5 行应该在一个线程中,而第 2 和 4 行应该在另一个线程中。
是否有保留顺序的选项或者是放弃多线程的唯一方法?
多线程与排序不兼容。如果您使用多线程步骤,项目将以未定义的顺序读取、处理和写入。参考文档的Multi-threaded Step部分提到了这一点:
The result of the above configuration is that the Step executes by reading, processing,
and writing each chunk of items (each commit interval) in a separate thread of execution.
Note that this means there is no fixed order for the items to be processed, and a chunk
might contain items that are non-consecutive compared to the single-threaded case.