问题
I have a step in my batch job that I want to use only to delete rows from a table.
The step looks like this:
@Bean
public Step step2(StepBuilderFactory factory,
PurgeAggBalanceWriter writer,
DataSource dataSource,
PlatformTransactionManager platformTransactionManager){
return stepBuilderFactory.get("step2")
.transactionManager(platformTransactionManager)
.<Assessment,Assessment>chunk(10)
.reader(getReader(dataSource, READER_QUERY2, "AggBalanceMapper", new AggBalanceMapper()))
.writer(writer)
.build();
}
I am using this writer class with a jdcb template to run the delete statement:
public class PurgeAggBalanceWriter implements ItemWriter<Assessment> {
private JdbcTemplate jdbcTemplate;
private static final String DELETE_QUERY = "DELETE FROM TABLE WHERE COLUMN = 'TEST'";
public PurgeAggBalanceWriter(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
@Override
public void write(List<? extends Assessment> list) {
jdbcTemplate.update(DELETE_QUERY);
}
The step completes successfully but I dont see why an ItemReader is required as it states when I try to remove the .reader() from step2.
Is there a way to avoid using a reader/mapper and just using the writer since all I have to do is run a delete query?
回答1:
all I have to do is run a delete query
In this case, you don't need a chunk-oriented tasklet. A simple tasklet is enough, something like:
public class DeletionTasklet implements Tasklet {
private static final String DELETE_QUERY = "DELETE FROM TABLE WHERE COLUMN = 'TEST'";
private JdbcTemplate jdbcTemplate;
public DeletionTasklet(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) {
jdbcTemplate.update(DELETE_QUERY);
return RepeatStatus.FINISHED;
}
}
来源:https://stackoverflow.com/questions/64164282/why-do-i-need-an-itemreader-in-my-job-step-if-i-only-need-to-delete-rows-using-i