javaspring-batchflatfilereader

Spring batch - How to write data multiple rows after processing single record?


I have bean class as below -

public class Account {
    private String strAccNumber = "";

    private List<Account> accountList = new ArrayList<Account>();

     // getter setter
....
...
@Override
    public String toString() {
        // code for PassThroughLineAggregator
        String data="";
                    for (int j=0; j<accountList.size(); j++) {
            Account bean = accountList.get(j);
            data = data + bean.getStrAccNumber();
            if (j<(accountList.size()-1)) {
                data = data + "\n";
                                }       
        }
        return data;
    }
}

to write data I want to set accountList in my config file. I'm using below code to set bean property.

  <bean id="flatFileReader" class="org.springframework.batch.item.file.FlatFileItemReader"
    scope="step">
    <property name="resource" value="classpath:springBatch.csv" />
    <property name="strict" value="false"></property>
    <property name="linesToSkip" value="1" />
    <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="ACC#" />
                </bean>
            </property>
            <property name="fieldSetMapper">
                <bean
                    class="com.abc.reader.AccountDetailsFieldSetMapper" />
            </property>
        </bean>
    </property>
</bean>
 <bean id="flatFileProcessor"
    class="com.abc.processor.AccountItemProcessor"
    scope="step"></bean>
<bean id="flatFileWriter"
        class="com.abc.FlatFileItemWriterListener"
        scope="step">
        <property name="resource" value="classpath:springBatch1.csv" />

        <property name="lineAggregator">
            <bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
                <property name="fieldExtractor">
                    <bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
                        <property name="names" value="strAccNumber" />
                    </bean>
                </property>
            </bean>
        </property> 

    </bean>

AccountItemProcessor -

 public class AccountItemProcessor implements ItemProcessor<Account, List<Account>>{

@Override
public List<Account> process(Account accObj) throws Exception {
  // logic..
}

After processing single record in the process step I want to write multiple items(list of item) , how can I write multiple item at a time using arraylist. In my case I want to write data from accountlist.


Solution

  • You'll want to implement your own LineAggregator. That is what provides the String to be written to the file. In your case, you'll write one that returns a String that is really multiple lines.

    You can read more about the LineAggregator interface in the documentation here: http://docs.spring.io/spring-batch/apidocs/org/springframework/batch/item/file/transform/LineAggregator.html