Reputation: 3
I am using spring batch to automatically read csv files and map each row into an object. There is code below. The columns in the csv file are ID, field1, field2, field3. And this is the order I wrote in the lineTokenizer. However, the FlatFileItemReader is not reading the file in this order. Rather, it seemingly reads in a random order, reading field2 first, then ID, then field3, then field1. But I want it to read ID first because I'm trying to write a map that maps ID to each field. Is there a way to change the order in which the FlatFileItemReader reads the columns in csv?
<bean id="mappedPosition" class="com.jpmorgan.ib.colopt.input.MappedPosition" scope="prototype" />
<bean id="positionFileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader">
<property name="resource" value="position.csv" />
<property name="lineMapper">
<bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
<property name="lineTokenizer">
<bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
<property name="names" value="ID, field1, field2, field3" />
</bean>
</property>
<property name="fieldSetMapper">
<bean class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
<property name="prototypeBeanName" value="mappedPosition"/>
</bean>
</property>
</bean>
</property>
</bean>
Upvotes: 0
Views: 3274
Reputation: 18413
Spring-batch reads and tokenize fields from CSV in sequence, as expected.
The problem is in BeanWrapperFieldSetMapper
: to inject FieldSet
values into bean's properties mapper pass from an String[]
(ordered with your field names) to a Properties
(look FieldSet.getProperties()
) to match property-value.
I think you can achieve your goal using a StepExecutionListener and intercept item after reading (you will get your mappedPosition
fully popolated)
Upvotes: 1