JsonRecordSeparatorPolicy example

In this post under Spring Batch, I will explain the purpose and how to use JsonRecordSeparatorPolicy class with an example.

JsonRecordSeparatorPolicy class is a concrete implementation of org.springframework.batch.item.file.separator.RecordSeparatorPolicy interface.

In one of my previous post I have explained the purpose of RecordSeparatorPolicy and how to use it.

For recap, RecordSeparatorPolicy interface is used to tell the reader what data represents a single record. The data can be in a single line or span multiple lines.

Its up to RecordSeparatorPolicy interface to decide what data makes up a record.

RecordSeparatorPolicy interface concrete implementation JsonRecordSeparatorPolicy is used to tell reader what data spanning multiple line forms a single valid JSON record.

A valid JSON record has equal number of opening braces ({) and closing braces (}), possibly nested.

The below xml code shows how to integrate JsonRecordSeparatorPolicy bean to reader bean


1   <bean id="customRecordSeparatorPolicy" class="org.springframework.batch.item.file.separator.JsonRecordSeparatorPolicy"/>
2   
3   <bean id="reader" class="org.springframework.batch.item.file.FlatFileItemReader">
4       <property name="resource" value="file:JsonFileInput2.txt"/>
5       <property name="recordSeparatorPolicy" ref="customRecordSeparatorPolicy"/>
6       <property name="lineMapper">
7           <bean class="package20.JsonLineMapperWrapper">
8               <property name="delegate">
9                   <bean class="org.springframework.batch.item.file.mapping.JsonLineMapper"/>
10              </property>
11          </bean>
12      </property> 
13  </bean>

In the above xml code, at line 1 a bean of type JsonRecordSeparatorPolicy is created.

At line 5, the JsonRecordSeparatorPolicy bean is integrated with the reader bean by setting the property “recordSeparatorPolicy”.

JsonLineMapper vs JsonRecordSeparatorPolicy

1) JsonLineMapper doesn’t figure out what data (spanning single or multiple lines) constitute a record. It just converts the record obtained by an concrete implementation of RecordSeparatorPolicy interface to a map.

2) JsonRecordSeparatorPolicy figure out what data (spanning single or multiple lines) constitute a record.

Below is the complete xml configuration


<?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="package20.Employee" scope="prototype"/>
    
    <bean id="customRecordSeparatorPolicy" class="org.springframework.batch.item.file.separator.JsonRecordSeparatorPolicy"/>
    
    <bean id="reader" class="org.springframework.batch.item.file.FlatFileItemReader">
        <property name="resource" value="file:JsonFileInput2.txt"/>
        <property name="recordSeparatorPolicy" ref="customRecordSeparatorPolicy"/>
        <property name="lineMapper">
            <bean class="package20.JsonLineMapperWrapper">
                <property name="delegate">
                    <bean class="org.springframework.batch.item.file.mapping.JsonLineMapper"/>
                </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="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="importEmployees" job-repository="jobRepository">
        <batch:step id="readWriteEmployees">
            <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>

Check these products on Amazon
Advertisements

Leave a Reply