0

I have a Spring Batch job for reading a CSV file.

How to avoid processing the first line?

I want the Processor not reading the first line, like a heading. In this case, I want the CustomerItemProcessor not reading the first line of my CSV file.

Here the code I wrote:

@Configuration
public class ImportJobConfig {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Bean
    public Job importUserJob(ItemReader<Customer> importReader) {
        return jobBuilderFactory.get("importUserJob")
            .incrementer(new RunIdIncrementer())
            .flow(step1(importReader))
            .end()
            .build();
    }

    @Bean
    public Step step1(ItemReader<Customer> importReader) {
        return stepBuilderFactory.get("step1")
            .<Customer, Customer>chunk(10)
            .reader(importReader)
            .processor(processor())
            .build();
    }

    @Bean
    @Scope(value = "step", proxyMode = ScopedProxyMode.TARGET_CLASS)
    public FlatFileItemReader<Customer> importReader1(@Value("#{jobParameters[fullPathFileName]}") String pathToFile) {
        FlatFileItemReader<Customer> reader = new FlatFileItemReader<>();
        reader.setResource(new FileSystemResource(pathToFile));
        reader.setLineMapper(new DefaultLineMapper<Customer>() {{
            // doing some stuff
        }});
        return reader;
    }

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

}
VLAZ
  • 26,331
  • 9
  • 49
  • 67
Jacel
  • 307
  • 4
  • 10
  • 24
  • 2
    Possible duplicate of [How to skip lines with ItemReader in Spring-Batch?](https://stackoverflow.com/questions/27228809/how-to-skip-lines-with-itemreader-in-spring-batch) – daniu Apr 24 '18 at 13:40
  • 2
    Possible duplicate of [spring batch ItemReader FlatFileItemReader set cursor to start reading from a particular line or set linestoskip dynamically](https://stackoverflow.com/questions/21477314/spring-batch-itemreader-flatfileitemreader-set-cursor-to-start-reading-from-a-pa) – Luca Basso Ricci Apr 24 '18 at 13:41

1 Answers1

3

You could use the FlatFileItemReader's setLinesToSkip method.

https://docs.spring.io/spring-batch/trunk/apidocs/org/springframework/batch/item/file/FlatFileItemReader.html#setLinesToSkip-int-

Garreth Golding
  • 985
  • 2
  • 11
  • 19