Spring Batch Flat File Item Writer Example

  1. Spring Batch Flat File Item Writer Example Program
  2. Spring Batch Flat File Item Writer Example Free
  3. Spring Batch Flat File Item Writer Examples
  4. Spring Batch Flat File Item Writer Example Pdf

In this tutorial, we will show you how to read items from multiple resources (multiple csv files), and write the items into a single csv file.

Tools and libraries used

In this chapter, we will create a Spring Batch application which uses an MySQL Reader and a Flatfile Writer (.txt ). Reader − The Reader we are using in the application is JdbcCursorItemReader to read data from MySQL database. 15-writing-items-to-db - writing items to a jdbc database source; 16-writing-items-to-fs - writing items to a flat file; 17-writing-items-to-xml - writing items to an XML file; 18-writing-to-multiple-files - writing items to multiple files/formats; Processors. 19-processing-basic - processing an item, basic example. Java JdbcBatchItemWriter.setSql - 4 examples found. These are the top rated real world Java examples of org.springframework.batch.item.database.JdbcBatchItemWriter.setSql extracted from open source projects. You can rate examples to help us improve the quality of examples.

  1. Maven 3
  2. Eclipse 4.2
  3. JDK 1.6
  4. Spring Core 3.2.2.RELEASE
  5. Spring Batch 2.2.0.RELEASE

P.S This example – 3 CSV files (reader) – combine into a single CSV file (writer).

1. Project Directory Structure

Review the final project structure, a standard Maven project.

2. Multiple CSV Files

Spring batch flat file item writer example free

There are 3 csv files, later we will use MultiResourceItemReader to read it one by one.

csv/inputs/domain-1-3-2013.csv
csv/inputs/domain-3-3-2013.csv

3. Spring Batch Jobs

A job to read resources that matches this pattern csv/inputs/domain-*.csv, and write it into a single cvs file domain.all.csv.

resources/spring/batch/config/context.xml

4. Run It

Create a Java class and run the batch job.

Output. The content of three csv files is read and combine into a single csv file.

csv/outputs/domain.all.csv

Download Source Code

Download it – SpringBatch-MultiResourceItemReader-Example.zip (12 kb)

References

mkyong

Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

In this article I explained how to use FlatFileItemWriter class to write a flat file without using a complete Spring batch flow.

Person.java domain object

PersonBuilder.java

Spring batch flat file item writer example free

This is a builder pattern for constructing a Person object:

HeaderCopyCallback.java

This is a class for writing header into flat file. For more details, see the file-writer.xml Spring bean context file:

FlatFileRecordsWriter.java

This is an interface for callback. See the implementation in FlatFileWriterTemplate.java, FileWriter.java class.

Flat

FlatFileWriterTemplate.java

Spring Batch Flat File Item Writer Example Program

Basically this template class is wrapping FlatFileItemWriter reference and also handling file opening,writing, and closing. This class is similar like JdbcTemplate

FlatFileWriter.java

Spring Batch Flat File Item Writer Example Free

FlatFileWriter class has two methods: write and writeAll. The first write method takes one argument, i.e. a Person object, and writes it to a flat file. Another writeAll method takes the list of Person objects as an argument and writes them into a flat file.

FlatFileWriterTest.java

The below JUnit class shows how to use to FlatFileWriter's write and writeAll methods.

file-writer.xml

flatFileWriter

This class is an item writer that writes data to a file or stream. The writer also provides a restart. The location of the output file is defined by a Resource and must represent a writable file. Uses buffered writers to improve performance.

The implementation is not thread-safe. (Spring doc)

DelimitedLineAggregator

A LineAggregator implementation that converts an object into a delimited list of strings. The default delimiter is a comma (from Spring doc).

BeanWrapperFieldExtractor

This is a field extractor for a Java bean. Given an array of property names, it will reflectively call getters on the item and return an array of all the values(from Spring docs). It extract Person's firstName,lastName, and middleName values and return to FlatFileItemWriter.

headerCopier

See the HeaderCopyCallback.java above for implementation. The bean provides header columns for the flat file.

outputResource

ClasspathResource-> Resource implementation for class path resources. Uses either a given ClassLoader or a given Class for loading resources (from Spring doc). This bean points to the flat file location after writing header and records.

Spring batch flat file item writer examples

Spring Batch Flat File Item Writer Examples

writerManager

Spring Batch Flat File Item Writer Example Pdf

This bean has a FlatFileWriterTempate reference and the client is going to invoke a writerManager.write/writeAll method. See the above class FlatFileWriter.java and FlatFilwWriterTest.java for example.

For the complete code please visit this link: https://github.com/upenderc/flatfile-hack