0

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?

Somebody
  • 2,667
  • 14
  • 60
  • 100
  • If that isn't provided by that specific reader you simply cannot skip it. Also I wonder if you should actually use this library has looking at the sources it hasn't had an upgrade in 10 years (so I'm actuyally suprised it still works with the current Spring and Spring Batch version). As a hack you could try setting `setCurrentItemCount` to 1, which should skip a line, but this will skew your metrics/lines read and might not work. – M. Deinum Jul 28 '23 at 14:39
  • @M.Deinum yep, it seems setting `setCurrentItemCount` to 1 does the trick. I was also considering the option to create another class to hold the header and mapping this in the `beanio-configuration.xml` but that was going to be there for nothing in my case, just to get the header and do nothing with it, so your suggestion is better. Please provide your answer to give you the credit. – Somebody Jul 28 '23 at 15:08

1 Answers1

1

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!

M. Deinum
  • 115,695
  • 22
  • 220
  • 224