简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM