Flat files
One of the most common and ancient mechanisms for interchanging bulk data has always been the flat file. For reading a flat file you must understand ahead in time exactly as to how the file is structured. All flat files fall into two types:
- Delimited files – files in which fields are separated by a delimiter, such as a comma
- Fixed Length files – files which have fields that are of a set length
Figure 23-10. Working of flat file ItemReader and ItemWriter
FlatFileItemReader
Reading flat files in the Spring Batch framework is facilitated by the class FlatFileItemReader, which provides basic functionality for reading and parsing flat files. The required dependencies of FlatFileItemReader are Resource and LineMapper. Spring Batch provides a default implementation for LineMapper interface in the form of DefaultLineMapper. The FlatFileItemReader delegates to LineTokenizer and FiledSetMapper if the LineMapper used is DefaultLineMapper as shown in Figure 23-11 below.
Figure 23-11. Functioning of FlatFileReader
Given a line of input the LineTokenizer returns a FieldSet representing the line. This FieldSet can then be passed to a FieldSetMapper. Spring Batch contains the multiple implementations of LineTokenizer as summarized below:
- DelmitedLineTokenizer – Used for files where fields in a record are separated by a delimiter. The most common delimiter is a comma, but pipes or semicolons are often used as well.
- FixedLengthTokenizer – Used for files where fields in a record are each of a ‘fixed width’. The width of each field must be defined for each record type.
- PatternMatchingCompositeLineTokenizer – Determines which among a list of LineTokenizer’s should be used on a particular line by checking against a pattern.
The FieldSetMapper interface defines a single method, mapFieldSet, which takes a FieldSet object and maps its contents to an object. This object may be a custom DTO, a domain object as needed by your application. The FieldSetMapper is used in conjunction with the LineTokenizer to translate a line of data from a resource into an object of the desired type. Configuring a FlatFileItemReader is as shown in Listing 23-8 below.
Listing 23-8. Configuring FlatFileItemReader in the Spring configuration file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
… <bean id="sampleFile" class="org.springframework.core.io.FileSystemResource" scope="step"> … </bean> <bean id="sampleFileReader" class="org.springframework.batch.item.file.FlatFileItemReader"> <property name="resource" ref="sampleFile"/> <property name="lineMapper" ref="lineMapper"/> </beans:bean> <bean id="lineTokenizer" class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer"> <property name="names" value="…"/> <property name="delimiter" value=","/> </bean> <bean id="fieldSetMapper" class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper"> <property name="prototypeBeanName" value="sample"/> </bean> <bean id="lineMapper" class="org.springframework.batch.item.file.mapping.DefaultLineMapper"> <property name="lineTokenizer" ref="lineTokenizer"/> <property name="fieldSetMapper" ref="fieldSetMapper"/> </bean> … |
Page Visitors: 25940
Tomcy John
Latest posts by Tomcy John (see all)
- A Guide to Continuous Improvement for Architects - February 2, 2023
- Cloud-first Architecture Strategy - January 26, 2023
- Architecture Strategy and how to create One - January 24, 2023
Thanks for sharing the information but what’s the difference between CursorItemReader’s setFetchSize() and PagingItemReader’s setPageSize()? isn’t it the same?
Please provide an example for StoredProcedureItem Reader – which returns cursor and how to process cursor in processor – I am new to Spring Batch as part of my work I need to invoke Oracle Stored proc which takes one param as input and returns result set which I need to process in Spring Batch Processor.
Thanks
Laxmi