在第二个 reader 中使用第一个 reader 的输出
Use the output of first reader in the second reader
我正在使用 reader 在上下文中获取存储的 RequestDto
:
public class ItemReaderStoredData implements ItemReader<List<RequestDto>> {
private List<RequestDto> requestListDto = new ArrayList<>();
@BeforeStep
public void retrieveInterStepData(StepExecution stepExecution) {
JobExecution jobExecution = stepExecution.getJobExecution();
ExecutionContext jobContext = jobExecution.getExecutionContext();
this.requestListDto = (List<RequestDto>)jobContext.get("directRequestListDto");
}
@Override
public List<RequestDto> read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
return requestListDto;
}
}
我想检查数据库中 RequestDto
列表的每个 uuid
是否存在,方法是在 stepConfig
中使用 cursorItemReader
。目的是将非冗余项写入文件。
@Bean
public JdbcCursorItemReader<HistoricDto> cursorItemReader() {
JdbcCursorItemReader<HistoricDto> reader = new JdbcCursorItemReader();
reader.setSql("select * from tab");
reader.setDataSource(this.dataSource);
reader.setRowMapper(new TableMapperDto());
return reader;
}
如何在同一步骤中使用 reader 一个在 reader 两个中的输出?或者是否可以将两个代码合并到一个单独的 class?
编辑: 我正在考虑合并两者:将 JdbcCursorItemReader
与 @BeforeStep
放在单独的 class 中21=] 会解决我的问题。但我对 Datasource
Error
有疑问,即 Null
。以这种方式合并它们安全吗?如何操作?
不建议在执行上下文中存储项目列表,因为它在 chunk/step 边界处持久化(这在性能方面可能很昂贵)。
I want to read xlsx file (input), populate a DTO, Check redunduncy in the database, filter my DTO List, Write in a a file1 new item (or database) and log redundant Item in a file2'(output).
有几种方法可以实现此 而无需 将项目存储在列表中并通过执行上下文共享它们。这里有几个选项:
- 选项 1:使用项目处理器过滤掉现有项目。可以使用
ItemProcessListener
将现有项目写入 file2。处理器不会过滤不存在的项目,可以使用步骤的项目编写器将其写入数据库。
- 选项 2:与选项 1 相同,但使用
SkipListener
而不是 ItemProcessListener
。在这种情况下,处理器将为现有项目(应声明为可跳过)抛出异常,而不是为现有项目返回 null
(即过滤它们)。
- 选项 3:使用项目处理器进行检查并将项目标记为存在或不存在。然后可以使用
ClassifierCompositeItemWriter
对项目进行分类并在适当的地方写入它们。
还有其他技术,例如在暂存中加载文件 table 并在两个 table 之间进行比较,但这些取决于用例(输入大小、比较逻辑等) .
我正在使用 reader 在上下文中获取存储的 RequestDto
:
public class ItemReaderStoredData implements ItemReader<List<RequestDto>> {
private List<RequestDto> requestListDto = new ArrayList<>();
@BeforeStep
public void retrieveInterStepData(StepExecution stepExecution) {
JobExecution jobExecution = stepExecution.getJobExecution();
ExecutionContext jobContext = jobExecution.getExecutionContext();
this.requestListDto = (List<RequestDto>)jobContext.get("directRequestListDto");
}
@Override
public List<RequestDto> read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
return requestListDto;
}
}
我想检查数据库中 RequestDto
列表的每个 uuid
是否存在,方法是在 stepConfig
中使用 cursorItemReader
。目的是将非冗余项写入文件。
@Bean
public JdbcCursorItemReader<HistoricDto> cursorItemReader() {
JdbcCursorItemReader<HistoricDto> reader = new JdbcCursorItemReader();
reader.setSql("select * from tab");
reader.setDataSource(this.dataSource);
reader.setRowMapper(new TableMapperDto());
return reader;
}
如何在同一步骤中使用 reader 一个在 reader 两个中的输出?或者是否可以将两个代码合并到一个单独的 class?
编辑: 我正在考虑合并两者:将 JdbcCursorItemReader
与 @BeforeStep
放在单独的 class 中21=] 会解决我的问题。但我对 Datasource
Error
有疑问,即 Null
。以这种方式合并它们安全吗?如何操作?
不建议在执行上下文中存储项目列表,因为它在 chunk/step 边界处持久化(这在性能方面可能很昂贵)。
I want to read xlsx file (input), populate a DTO, Check redunduncy in the database, filter my DTO List, Write in a a file1 new item (or database) and log redundant Item in a file2'(output).
有几种方法可以实现此 而无需 将项目存储在列表中并通过执行上下文共享它们。这里有几个选项:
- 选项 1:使用项目处理器过滤掉现有项目。可以使用
ItemProcessListener
将现有项目写入 file2。处理器不会过滤不存在的项目,可以使用步骤的项目编写器将其写入数据库。 - 选项 2:与选项 1 相同,但使用
SkipListener
而不是ItemProcessListener
。在这种情况下,处理器将为现有项目(应声明为可跳过)抛出异常,而不是为现有项目返回null
(即过滤它们)。 - 选项 3:使用项目处理器进行检查并将项目标记为存在或不存在。然后可以使用
ClassifierCompositeItemWriter
对项目进行分类并在适当的地方写入它们。
还有其他技术,例如在暂存中加载文件 table 并在两个 table 之间进行比较,但这些取决于用例(输入大小、比较逻辑等) .