Java Reference
In-Depth Information
until the transaction is
committed.
Unlike the
LineMapper
of
FlatFileItemReader
, the
LineAggregator
doesn't have any hard
dependencies. However, a related interface to be aware of is
org.springframework.batch.item.file.transform.FieldExtractor
. This interface is used in most of the
provided
LineAggregator
implementations as a way to access the required fields from a given item.
Spring Batch provides two implementations of the
FieldExtractor
interface:
org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor
, which uses the getters
on the class to access the properties per the JavaBean spec, and
org.springframework.batch.item.file.transform.PassThroughFieldExtractor
, which returns the item
(useful for items that are just a
String
, for example).
You look at a few of the
LineAggregator
implementations over the rest of this section. Let's begin
with using
FlatFileItemWriter
with
FormatterLineAggregator
to create formatted files.
Formatted Text Files
When you looked at text files from the input side, you had three different types: fixed width, delimited,
and XML. From the output side of things, you still have delimited and XML, but fixed width isn't just
fixed width. In this case, it's really a formatted record. This section looks at how to construct batch
output as a formatted text file.
Why the difference between a fixed-width input file and a formatted output file? Well, technically
there is no difference. They're both files that contain a fixed format record of some kind. However,
typically input files have records that contain nothing but data and are defined via columns, whereas
output files can be either fixed width or more robust (as you see later in this chapter with the statement
job).
This example generates a list of customers and where they live. To begin, let's look at the input
you're working with. Listing 9-2 shows an example of the
customer.csv
file.
Listing 9-2.
customer.csv
Richard,N,Darrow,5570 Isabella Ave,St. Louis,IL,58540
Warren,L,Darrow,4686 Mt. Lee Drive,St. Louis,NY,94935
Barack,G,Donnelly,7844 S. Greenwood Ave,Houston,CA,38635
Ann,Z,Benes,2447 S. Greenwood Ave,Las Vegas,NY,55366
Erica,Z,Gates,3141 Farnam Street,Omaha,CA,57640
Warren,M,Williams,6670 S. Greenwood Ave,Hollywood,FL,37288
Harry,T,Darrow,3273 Isabella Ave,Houston,FL,97261
Steve,O,Darrow,8407 Infinite Loop Drive,Las Vegas,WA,90520
As Listing 9-2 shows, you're working with a file similar to the customer files you've been using up to
this point in the topic. However, the output for this job will be slightly different. In this case, you want to
output a full sentence for each customer: “Richard Darrow lives at 5570 Isabella Ave in St. Louis, IL.”
Listing 9-3 shows an example of what the output file looks like.