As an example think of processing a large flat file composed of records and writing the output to a database. Each line in the file corresponds to an item: it will be read, transformed and output separately. Spring Batch provides a solid foundation to implement item-oriented jobs and includes a collection of ready-to-use objects for common.

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

  1. 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.
  2. Spring Batch is a powerful framework for developing robust batch applications. In our previous tutorial, we introduced Spring Batch. In this tutorial, we'll build on the previous one and learn how to set up and create a basic batch-driven application using Spring Boot.
  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.

Spring batch flat file item writer example free

2. Multiple CSV Files

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

Spring Batch Flat File Item Writer Example Program

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.

Batch processing is the execution of a series of programs (“jobs”) on a computer without manual intervention.

Spring Batch Flat File Item Writer Example

Spring Batch provides mechanisms for processing large amount of data like transaction management, job processing, resource management, logging, tracing, conversion of data, interfaces, etc.
These functionalities are available out of the box and can be reused by applications containing the Spring Batch framework.

In this tutorial, we will show you how to configure a Spring Batch job to read CSV file into a CSV file, and filter out the record before writing with ItemProcessor. Its a very easy program for beginners.

Tools and libraries used

Spring Batch Flat File Item Writer Example Free

  1. Maven 3
  2. Eclipse Luna
  3. JDK 1.7
  4. Spring Core 3.2.2.RELEASE
  5. Spring Batch 2.2.0.RELEASE
  6. Spring OXM 3.2.2.RELEASE

1. Create a maven project . I named my project as SpringBatchProject.

2. Project Dependencies –

3. Project Structure –

4. CSV file resources/files/input.csv

5. Read CSV file resources/jobs/job-report.xml

6. The csv file mapped to Pojo Report.java

7. Spring batch Core Settings

Define jobRepository and jobLauncher

8. Spring batch Jobs

A Spring batch job, read the report.csvfile, map it to Report object, and write it into a csv file.

9. Spring Batch – ItemProcessor

Spring Batch Flat File Item Writer Examples

In Spring batch, the wired Processor will be fired before writing to any resources, so, this is the best place to handle any conversion, filtering and business logic. In this example, the Report object will be ignored (not write to csv file) if its’ age is greater than equal to 30.

10. I have scheduled this process which will run in every 5 seconds through cron jobs.

Spring Batch Flat File Item Writer Example

RunScheduler.java

11. Run the Main class now

Spring Batch Flat File Item Writer Example Pdf

12. output csv file i.e. report.csv