1

I did some searching, but couldn't find an example code. Spring batch reading from REST api (which I have done) and writing multiple records for one read to a single DB table using JdbcBatchItemWriter. Below is my BatchConfig code, but it writes only one record. I think I have to make my processor return a List of Registration object and the JDBCItemWriter has to write multiple records

code

@Configuration
@EnableBatchProcessing
public class BatchConfiguration {

    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    @Autowired
    private Environment environment;

    @Bean
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
    //my reader
    @Bean
    ItemReader<EmployeeEmploymentDTO> restEmployeeReader(Environment 
    environment, 
                                             RestTemplate restTemplate) {
        return new RESTEmployeeReader(             

  environment.getRequiredProperty("rest.api.to.listemployees.ugs.api.url"), 
            restTemplate
        );
    }
 //my processor which is a separate class
    @Bean
    public RegistrationItemProcessor processor() {
        return new RegistrationItemProcessor();
    }
   //my writer which now only inserts one record for a read but i want to 
  insert multiple varying number of records for a read   

    @Bean
    public JdbcBatchItemWriter<Registration> writer(DataSource dataSource) {
        return new JdbcBatchItemWriterBuilder<Registration>()
            .itemSqlParameterSourceProvider(new 
     BeanPropertyItemSqlParameterSourceProvider<>())
            .sql("INSERT INTO registration //.....*ommitted insert statement
            .dataSource(dataSource)
            .build();
    }

    @Bean
    public Job importUserJob(JobCompletionNotificationListener listener, 
    Step step1) {
        return jobBuilderFactory.get("importUserJob")
            .incrementer(new RunIdIncrementer())
            .listener(listener)
            .flow(step1)
            .end()
            .build();
    }

    @Bean
    public Step step1(JdbcBatchItemWriter<Registration> writer) {
        return stepBuilderFactory.get("step1")
            .<EmployeeEmploymentDTO, Registration> chunk(10)
            .reader(restEmployeeReader(environment,restTemplate()))           
            .processor(processor())
            .writer(writer)
            .build();
    }

 } 
coder
  • 8,346
  • 16
  • 39
  • 53
Nandhini
  • 798
  • 8
  • 13
  • I saw https://stackoverflow.com/questions/7836689/spring-batch-multi-line-record-item-writer-with-variable-number-of-lines-per-rec/7841510#comment9568144_7841510 and https://stackoverflow.com/questions/7894445/read-one-record-item-and-write-multiple-records-items-using-spring-batch?noredirect=1&lq=1 but couldn't find some code that could help me – Nandhini May 01 '18 at 15:55

1 Answers1

0

My processor returned a list of List

My writer as below

public class MultiOutputItemWriter implements ItemWriter<List<Registration>> {
    ItemWriter<Registration> itemWriter;
    @Autowired
    NamedParameterJdbcTemplate namedParamJdbcTemplate;

     @Override
     public void write(List<? extends List<Registration>> items) throws Exception {
         for (List<Registration> registrations : items) {

         final String SQL_INSERT_INTO_REGISTRATION="INSERT INTO registration (employee_id, ....";

             final List<MapSqlParameterSource> params = new ArrayList<>();
             for (Registration registration : registrations) {
                 MapSqlParameterSource param = new MapSqlParameterSource();

                 param.addValue("employeeId", registration.getEmployeeId());
                 param.addValue("startDate", registration.getStartDate());
                 param.addValue("user", registration.getUser());
                 param.addValue("endTime", registration.getEndTime());

                 params.add(param);          

             }

             namedParamJdbcTemplate.batchUpdate(SQL_INSERT_INTO_REGISTRATION,params.toArray(new MapSqlParameterSource[params.size()]));
          }
    }
}
Nandhini
  • 798
  • 8
  • 13