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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With