Java Reference
In-Depth Information
This method of formatting output can be used for a number of different requirements. Whether it's
formatting items into human-readable output as you did here, or formatting them into a fixed-width file
as you used for input in Chapter 7, all that needs to change is the format String you configure for the
LineAggregator .
The other main type of flat file you see on a regular basis is the delimited file. customer.csv is a
comma-delimited file, for example. The next section looks at how to output files that contain delimited
output.
Delimited Files
Unlike the formatted files you looked at in the previous section, delimited files don't have a single
predefined format. Instead, a delimited file consists of a list of values separated by a predefined
separator character. This section looks at how to use Spring Batch to generate a delimited file.
To see how generating a delimited file works, you use the same input for this job. For the output,
you refactor the ItemWriter to generate the new, delimited output. In this case, you change the order of
the fields and change the delimiter from a comma (,) to a semicolon (;). Listing 9-10 shows some sample
output with the updated formatJob .
Listing 9-10. Output for Delimited formatJob
58540;IL;St. Louis;5570 Isabella Ave;Darrow;Richard
94935;NY;St. Louis;4686 Mt. Lee Drive;Darrow;Warren
38635;CA;Houston;7844 S. Greenwood Ave;Donnelly;Barack
55366;NY;Las Vegas;2447 S. Greenwood Ave;Benes;Ann
57640;CA;Omaha;3141 Farnam Street;Gates;Erica
37288;FL;Hollywood;6670 S. Greenwood Ave;Williams;Warren
97261;FL;Houston;3273 Isabella Ave;Darrow;Harry
90520;WA;Las Vegas;8407 Infinite Loop Drive;Darrow;Steve
To generate the output in Listing 9-10, all you need to do is update the configuration of the
LineAggregator . Instead of using FormatterLineAggregator , you use Spring Batch's
org.springframework.batch.item.file.transform.DelimitedLineAggregator implementation. Using the
same BeanWrapperFieldExtractor to extract an Object array, the DelimitedLineAggregator concatenates
the elements of the array with the configured delimiter between each element. Listing 9-11 shows the
updated configuration for the ItemWriter.
Listing 9-11. flatFileOutputWriter Configuration
<beans:bean id="flatFileOutputWriter"
class="org.springframework.batch.item.file.FlatFileItemWriter">
<beans:property name="resource" ref="outputFile"/>
<beans:property name="lineAggregator" ref="delimitedLineAggregator"/>
</beans:bean>
<beans:bean id="delimitedLineAggregator"
class="org.springframework.batch.item.file.transform.
DelimitedLineAggregator">
<beans:property name="fieldExtractor">
<beans:bean class="org.springframework.batch.item.file.transform.
BeanWrapperFieldExtractor">
 
Search WWH ::




Custom Search