Using custom delimiter while reading and writing batch file

In all previous Spring Batch posts, the DelimitedLineAggregator(which creates String format of java objects) and DelimitedLineTokenizer (which extracts fields from each record) assume ‘,’ as delimiter as shown below

EMP_ID0,EMP_NAME0,EMP_STATUS0,0
EMP_ID1,EMP_NAME1,EMP_STATUS1,1
EMP_ID2,EMP_NAME2,EMP_STATUS2,2
EMP_ID3,EMP_NAME3,EMP_STATUS3,3

We can use other symbols as delimiter and inform DelimitedLineAggregator and DelimitedLineTokenizer about this.

At the time of reading and writing, the default delimiter is ‘,’. We can change this as shown below

Reader


1   <bean id="reader" class="org.springframework.batch.item.file.FlatFileItemReader">
2       <property name="resource" value="file:FileInput.txt"/>
3       <property name="lineMapper">
4           <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
5               <property name="lineTokenizer">
6                   <bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
7                       <property name="delimiter" value="|"/>
8                       <property name="names" value="id,name,status,salary"/>
9                   </bean>
10              </property>
11              <property name="fieldSetMapper">
12                  <bean class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
13                      <property name="prototypeBeanName" value="employee"/>
14                  </bean>
15              </property>
16          </bean>
17      </property> 
18  </bean>

In the reader xml code, the custom delimiter ‘|’ is specified at line 7. We set the delimiter property of DelimitedLineTokenizer class. In this way, we are
specifying that in the input file, the delimiter ‘|’ is used between the fields in the record as shown below

EMP_ID0|EMP_NAME0|EMP_STATUS0|0

Writer


1   <bean id="writer" class="org.springframework.batch.item.file.FlatFileItemWriter">
2       <property name="resource" value="file:FileOutput.txt"/>
3       <property name="lineAggregator">
4           <bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
5               <property name="delimiter" value="|" />
6               <property name="fieldExtractor">
7                   <bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
8                       <property name="names" value="id,name,status,salary"/>
9                   </bean>
10              </property>
11          </bean>
12      </property>
13  </bean>

In the writer xml code, the custom delimiter ‘|’ is specified at line 7. We set the delimiter property of DelimitedLineAggregator class. In this way, we are
specifying that in the output file,use the delimiter ‘|’ instead of the default ‘,’

Below is the complete xml code for your reference


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:batch="http://www.springframework.org/schema/batch"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd">
    
    <bean id="employee" class="package14.Employee" scope="prototype"/>
    
    <bean id="reader" class="org.springframework.batch.item.file.FlatFileItemReader">
        <property name="resource" value="file:FileInput.txt"/>
        <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="delimiter" value="|"/>
                        <property name="names" value="id,name,status,salary"/>
                    </bean>
                </property>
                <property name="fieldSetMapper">
                    <bean class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
                        <property name="prototypeBeanName" value="employee"/>
                    </bean>
                </property>
            </bean>
        </property> 
    </bean>
    
    <bean id="writer" class="org.springframework.batch.item.file.FlatFileItemWriter">
        <property name="resource" value="file:FileOutput.txt"/>
        <property name="lineAggregator">
            <bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
                <property name="delimiter" value="|" />
                <property name="fieldExtractor">
                    <bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
                        <property name="names" value="id,name,status,salary"/>
                    </bean>
                </property>
            </bean>
        </property>
    </bean>
    
    <batch:job id="importProductsJob">
        <batch:step id="readWriteProducts">
            <batch:tasklet>
                <batch:chunk reader="reader" writer="writer" commit-interval="50"/>
            </batch:tasklet>
        </batch:step>
    </batch:job>
    
    <bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
    
    <bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
        <property name="transactionManager" ref="transactionManager"/>
    </bean>
    
    <bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
        <property name="jobRepository" ref="jobRepository"/>
    </bean>
</beans>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s