Search code examples
javaspring-bootspring-batchbean-io

BeanIOFlatFileItemReader skip header using spring batch with spring boot


I haven't been able to find a way to skip the header for a csv file using: BeanIOFlatFileItemReader, there's no linesToSkip here, and when trying to do the below in the beanio-configuration.xml file it gives: Comments require reader.markSupported() to return true itemreader spring batch error. Probably because of this.

<stream name="contact-data" format="delimited">
    <parser>
        <property name="delimiter" value="|" />
        <!-- ignore header line -->
        <property name="comments" value="name" />
    </parser>
    <record name="content" class="com.person.Contact">
        <field name="name" />
        <field name="age" />
        <field name="country" />
    </record>       
</stream>

ItemReader:

@Bean
@StepScope
public BeanIOFlatFileItemReader<Contact> reader(@Value("#{jobParameters['fileName']}") String fileName) {               
    
    BeanIOFlatFileItemReader<Contact> reader = new BeanIOFlatFileItemReader<>();
    
    try {           
        reader.setUseSpringExceptions(true);
        reader.setResource(new FileSystemResource(fileName));           
        reader.setStreamName(inputContactStreamName);
        reader.setStreamMapping(new ClassPathResource(beanIoConfigurationXmlPath));
        reader.setStreamFactory(StreamFactory.newInstance());
        reader.getLineNumber();         
    } catch (Exception e) {
        log.error("ERROR: An issue occurred in the BeanIO Item Reader:: {} {}", e.getMessage(), e.getStackTrace());
    }
    
    return reader;      
}

CSV file:

name|age|country
john|25|USA
Mike|22|CANADA

How to skip the header using BeanIOFlatFileItemReader?


Solution

  • The regular FlatFileItemReader has a setLinesToSkip method which this BeanIOFlatFileItemReader seems to lack. As a workaround/hack you could try using the setCurrentItemCount and set it to 1. Although it is meant for restarting a step/job and restore the state you could abuse it for this.

    NOTE: Another thing, while checking the library, this seems to has had no update for the last ~10 years. I'm a bit surprised it still works (which is a testament for the Spring and Spring Batch maintainers for backwards compatibility). But it probably stop working somewhere down the line!