java - 在Spring Batch中,如何以给定的项目列表作为参数,在批量读取项目列表后插入一段代码?

标签 java spring-batch

我在 block 模式下使用 Spring Batch 来处理项目。 我是批量读的(批量6000条),一一处理,然后全部写下来。我通过 JdbcCursorItemReader 读取它们,这对于批量处理和读取非常方便。 问题是,一旦读取,我需要从另一个来源检索附加数据。最简单的方法是在处理器中执行此操作,调用自定义方法,如 getAdditionalDataById(String id) 。 这样做的错误在于它消耗了很多时间。所以我也想批量检索这些附加数据:在读取 6000 个项目后,获取它们的 id,然后调用类似的内容 getAllAdditionalDataByIdIn(List<String> ids) 。 但我不知道在哪里可以插入我的代码,因为 @AfterRead 注释位于每个项目之后,而不是批量读取之后。 @BeforeProcess 也是如此。 我现在唯一能得到的解决方案是在处理器中不执行任何操作,并在编写器中获取附加信息,在编写器中处理项目,然后将它们写入编写器中(这是一个自定义编写器)。

任何帮助将不胜感激。

我正在使用 Spring Batch 4.0.1,从 sqlserver 读取数据,然后写入 Elasticsearch。附加数据也存储在 Elasticsearch 中。 我在代码中搜索了一些,在文档中搜索了很多,但可以看到任何注释或任何其他可以帮助我的内容。

最佳答案

The problem is that once read, I need to retrieve additional data from another source. Simplest way is to do it in the processor, calling custom method like getAdditionalDataById(String id). The wrong thing in this is that it consume a lot of times.

这被称为 driving query pattern其中项目处理器用于使用附加数据(例如来自另一个数据源)来丰富项目。这种模式确实会引入一些性能问题,因为它需要对每个项目进行额外的查询。

So I would like to retrieve those additionnal data by bulk too : just after reading 6000 items, get their ids, and call something like getAllAdditionalDataByIdIn(List ids).

最接近的值是 ItemWriteListener#beforeWrite您可以在编写项目之前访问项目列表。通过范围内的项目列表,您可以获得它们的 ID 并调用您的 getAllAdditionalDataByIdIn(List<String> ids)方法。

关于java - 在Spring Batch中,如何以给定的项目列表作为参数,在批量读取项目列表后插入一段代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56546455/

相关文章:

java - Spring批量验证

crash - Spring Batch 重新启动崩溃的作业

java - Spring Batch 状态更改查询

java - 这个 Java 函数从哪里推断出它的泛型类型?

java - 无法将Gradle项目导入Eclipse

java - Java 中的类型转换是如何工作的?

java - Spring Batch - 使用带有列表列表的 ItemWriter

java - 将MYSQL插入JAVA数组

java - 在 JFrame 中添加字符串的最佳方法

java - 如何用 spring batch 解析 json 文件?