Why do I need an ItemReader in my job step if I only need to delete rows using ItemWriter

拜拜、爱过 提交于 2021-01-29 10:51:11

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!