Spring Batch Flat File Item Writer Example

  

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.

Batch processing is the execution of a series of programs (“jobs”) on a computer without manual intervention. Spring Batch provides mechanisms for processing large amount of data like transaction management, job processing, resource management, logging, tracing, conversion of data, interfaces, etc. Spring Batch provides readers and writers to read and write data form various file systems/databases such as MongoDB, Neo4j, MySQL, XML, flatfile, CSV, etc. To include a reader in your application, you need to define a bean for that reader, provide values to all the required properties within the bean, and pass the id of such bean as a value to. A job can consist of ‘n’ number of steps. Each step contains Read-Process-Write task or it can have single operation, which is called tasklet.; Read-Process-Write is basically read from a source like Database, CSV etc. Then process the data and write it to a source like Database, CSV, XML etc.

Tools and libraries used

Flat
  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
Flat

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. Nfs most wanted 2005 save game with extra cars.

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

Spring Batch Flat File Item Writer Example Program

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
Spring Batch Flat File Item Writer Example

Download Source Code

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

References

Spring Batch Flat File Item Writer Example Pdf

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.