Corporativo
Corporativo

Reputation: 351

Spring Batch multiple insert for a one read

I've a Spring Batch process that read Report objects from a CSV and insert Analytic objects into a MySQL DB correctly, but the logical has changed for a more than one Analytics insert for each Report readed.

I'm new in Spring Batch and the actually process was very difficult for me, and I don't know how to do this change.

I haven't XML configuration, all is with annotations. Report and Analytics classes have a getter and a setter for two fields, adId and value. The new logic has seven values for an adId and I need to insert seven rows into table.

I hide, delete or supress some code that not contribute for the question.

Here is my BatchConfiguration.java:

@Configuration
@EnableBatchProcessingpublic
class BatchConfiguration {
    @Autowired
    private transient JobBuilderFactory jobBuilderFactory;

    @Autowired
    private transient StepBuilderFactory stepBuilderFactory;

    @Autowired
    private transient DataSource dataSource;

    public FlatFileItemReader<Report> reader() {
        // The reader from the CSV works fine.
    }

    @Bean
    public JdbcBatchItemWriter<Analytic> writer() {
        final JdbcBatchItemWriter<Analytic> writer = new JdbcBatchItemWriter<Analytic>();
        writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Analytic>());
        writer.setSql("INSERT INTO TABLE (ad_id, value) VALUES (:adId, :value)");
        writer.setDataSource(dataSource);
        return writer;
    }

    @Bean
    public AnalyticItemProcessor processor() {
        return new AnalyticItemProcessor();
    }

    @Bean
    public Step step() {
        return stepBuilderFactory.get("step1").<Report, Analytic> chunk(10000).reader(reader()).processor(processor()).writer(writer()).build();
    }

    @Bean
    public Job process() {
        final JobBuilder jobBuilder = jobBuilderFactory.get("process");

        return jobBuilder.start(step()).build();
    }
}

Then the AnalyticItemProcessor.java

public class AnalyticItemProcessor implements ItemProcessor<Report, Analytic> {
    @Override
    public Analytic process(final Report report) {
        // Creates a new Analytic call BeanUtils.copyProperties(report, analytic) and returns analytic.
    }
}

And the Process:

@SpringBootApplication
public class Process {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(Process.class, args);
    }
}

How can I do this change? Maybe with ItemPreparedStatementSetter or ItemSqlParameterSourceProvider? Thanks.

Upvotes: 1

Views: 3749

Answers (1)

Michael Minella
Michael Minella

Reputation: 21493

If I'm understanding your question correctly, you can use the CompositeItemWriter to wrap multiple JdbcBatchItemWriter instances (one per insert you need to accomplish). That would allow you to insert multiple rows per item. Otherwise, you'd need to write your own ItemWriter implementation.

Upvotes: 4

Related Questions