Spring Book – Chapter 21 – Spring Batch

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

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

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

Page Visitors: 26012

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently working as Principal Architect at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Architect to facilitate incorporating Spring as Corporate Standard in the organization.
Tomcy John

Latest posts by Tomcy John (see all)

2 thoughts on “Spring Book – Chapter 21 – Spring Batch

  1. Thanks for sharing the information but what’s the difference between CursorItemReader’s setFetchSize() and PagingItemReader’s setPageSize()? isn’t it the same?

  2. 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

Leave a Reply

Your email address will not be published. Required fields are marked *