Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set delimiter value in FlatFileReader of spring batch?

I am new on spring batch. My task is to develop code which read a flat file where values are separated by pipe "|". I found that I can do it by FlatFileReader. But I am not getting how to set delimiter value during read and how to map each column with my POJO member variables.

Sample values in flat file are following .

3345|742|0|N

3346|743|1|A

3347|742|0|N

3348|742|1|C

3345|743|0|K

Thanks in advance.

like image 631
Ashok Singh Avatar asked Jul 10 '15 12:07

Ashok Singh


1 Answers

The FlatFileItemReader delegates the parsing of each record to a LineTokenizer (via the LineMapper). I'm assuming you're using the DelimitedLineTokenizer which allows you to set a delimiter (the default is a ,). To configure this with a pipe as the delimiter:

<bean id="reader" class="org.springframework.batch.item.file.FlatFileItemReader">
    <property name="resource" value="<SOME_VALUE>"/>
    <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="<LIST_OF_VALUES>"/>
                    <property name="delimiter" value="|"/>
                </bean>
            </property>
            <property name="fieldSetMapper" ref="myFieldSetMapper"/>
        </bean>
    </property>
</bean>

Where <SOME_VALUE> is the pattern for your file to be read and <LIST_OF_VALUES> is the list of column names in your delimited file.

like image 170
Michael Minella Avatar answered Sep 26 '22 03:09

Michael Minella