SpringBatch ItemWriter java

时间:2018-02-24 06:48:47

标签: java spring spring-boot spring-batch

我正在尝试使用spring batch撰写SpringBoot。首先,程序将从数据库中读取数据,然后将其写入.csv格式文件。

这是我试过的代码

阅读器

    @Bean
    public ItemReader<A> Reader() throws Exception {
        List list = new ArrayList<>();
        JdbcCursorItemReader<A> reader = new JdbcCursorItemReader<A>();
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

        String sql = "SELECT ID AS id FROM TABLE_A ";
        list = jdbcTemplate.query(sql, new Mapper()); // store all retrieved data to list
        reader.setSql(sql);
        reader.setDataSource(dataSource);
        reader.setRowMapper(new Mapper());

        if (list != null) {
            for (A c : (List<A>) list) {
                c.setId("123");  // overwrite the id
                c.setState("Active");             
            }
        }
        return reader;
    }

映射

   public class Mapper implements RowMapper<A> {

        @Override
        public A mapRow(ResultSet rs, int rowNum) throws SQLException {

            A c = new A();
            c.setId(rs.getString("id"));
    }

作家

    @Bean
    public ItemWriter<A> Writer() {
        FlatFileItemWriter<A> csvFileWriter = new FlatFileItemWriter<>();
        String exportFileHeader = "ID" + delimiter + "State";
        StringHeaderWriter headerWriter = new StringHeaderWriter(exportFileHeader);
        csvFileWriter.setHeaderCallback(headerWriter);
        headerWriter.checkRepository();

        String exportFilePath = "/home/xxx/Desktop/outputs/" + fileName + time() + ".csv";
        csvFileWriter.setResource(new FileSystemResource(exportFilePath));
        LineAggregator<A> lineAggregator = createStudentLineAggregator();
        csvFileWriter.setLineAggregator(lineAggregator);
        return csvFileWriter;
    }

BatchConfig

  @Bean
    public Job job() throws Exception { 
        return jobBuilderFactory.get("job")
                .incrementer(new RunIdIncrementer())
                .start(step1())
                .build();
    }


    @Bean
    public Step step1() throws Exception {
        return stepBuilderFactory.get("step1")
                .<A, A>chunk(1)
                .reader(Reader())
                .processor(new Processor())
                .writer(Writer())
                .build();
    }

处理器

public class Processor implements ItemProcessor<A, A> {

    @Override
    public A process(A i) throws Exception {
        System.out.println("Processing..." + i);
        return i;
    }
}

当我检查生成的文件时,我可以看到数据库中的值id,但不能看到123. State的值也是空的。如何覆盖映射值?

1 个答案:

答案 0 :(得分:1)

春天批量阅读器中的

负责读取数据。如果你想操纵数据,它必须在处理器中完成。这样:

public class Processor implements ItemProcessor<A, A> {

    @Override
    public A process(A i) throws Exception {
        System.out.println("Processing..." + i);
        i.setId("123");
        i.setStatus("Active");
        return i;
    }
}

这部分代码

    list = jdbcTemplate.query(sql, new Mapper()); // store all retrieved data to list

    if (list != null) {
        for (A c : (List<A>) list) {
            c.setId("123");  // overwrite the id
            c.setState("Active");             
        }
    }

实际上什么都不做。在Reader()中,您应该只返回已配置的阅读器。您可以通过以下方式正确配置:

    reader.setSql(sql);
    reader.setDataSource(dataSource);
    reader.setRowMapper(new Mapper());