Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore unwanted columns in CSV using FlatFileItemReader in spring batch

I have run into an issue where I have a CSV file with 10 columns and need only selected columns to be mapped to my Java objects. However the CSV is header column and data position is fixed. So I know that only column 1 to 3 are useful to me and rest has to be ignored. For eg: CSV is : A1,A2,A3,A4,A5,A6,A7,A8,A9,A10

I only need to have A1 till A3 columns to be mapped to my pojo. I'm sure this is not the right way but I tried doing something like this but it isn't working. Spring batch is trying to map all column values to my pojo.

<property name="lineTokenizer">
    <bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
        <property name="names" value="Name,Department,Age,,,,,,," />
    </bean>
</property>

<property name="fieldSetMapper">
    <bean class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
        <property name="prototypeBeanName" value="EmployeeDO" />
    </bean>

I haven't explored all the features of spring batch but is there something that can be achieved easily? Any help would be appreciated

like image 383
TuW Avatar asked Nov 28 '15 14:11

TuW


1 Answers

DelimitedLineTokenizer.setIncludedFields() should be the right call to solve your problem.

The fields to include in the output by position (starting at 0). By default all fields are included, but this property can be set to pick out only a few fields from a larger set. Note that if field names are provided, their number must match the number of included fields.

like image 126
Luca Basso Ricci Avatar answered Nov 06 '22 08:11

Luca Basso Ricci