Reading different CSV files using FlatFileItemReader Spring Batch

702 views Asked by At

I tried to find a solution for the problem I am looking for, but I dint find it or a chance that I might skipped. Help me if you can redirect me to the solution page.

Input to the batch: I have TRADES.csv, PORTFOLIO.csv files which are located at different paths.

How I am implemented it currently: Currently I wrote a class CSVReader has two FlatFileItemReaders which are defined as below.

<beans:bean id="porfolioReader"
    class="org.springframework.batch.item.file.FlatFileItemReader">
    <beans:property name="lineMapper" ref="lineMapperForPortfolio"></beans:property>
    <beans:property name="strict" value="false"></beans:property>
    <beans:property name="recordSeparatorPolicy"
        ref="csvRecordSeparatorPolicy"></beans:property>
    <beans:property name="linesToSkip" value="1"></beans:property>
    <beans:property name="encoding" value="ISO-8859-1"></beans:property>
</beans:bean>

<beans:bean id="tradeReader"
    class="org.springframework.batch.item.file.FlatFileItemReader">
    <beans:property name="lineMapper" ref="lineMapperForTrades"></beans:property>
    <beans:property name="strict" value="false"></beans:property>
    <beans:property name="recordSeparatorPolicy"
        ref="csvRecordSeparatorPolicy"></beans:property>
    <beans:property name="linesToSkip" value="1"></beans:property>
    <beans:property name="encoding" value="ISO-8859-1"></beans:property>
</beans:bean>

So based on the input file path I am opening the corresponding reader and making the FieldSet.

Case I am looking for: I am looking to read these CSV files in single step and make a FieldSet so that in processor I can again split the data into list of TRADES and PORTFOLIO objects. Is there a way I can make the FlatFileItemReader capable of finding what is the CSV picked, and choose the corresponding linemapper..?

Job Definition:

<batch:step id="tradeStep1" allow-start-if-complete="true">
        <batch:tasklet>
            <batch:chunk reader="csvReader"
                processor="csvProcessor" writer="csvWriter"
                commit-interval="1" />
        </batch:tasklet>
        <batch:next on="*" to="tradeStep2" />
        <batch:fail on="FAILED" />
</batch:step>

tradeStep2 will archive processed CSV files.

0

There are 0 answers