Spring Batch Flat File Item Writer Example

Wraps a ResourceAwareItemWriterItemStream and creates a new output resource when the count of items written in current resource exceeds #setItemCountLimitPerResource.

Spring Batch Flat File Item Writer Example

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.
These functionalities are available out of the box and can be reused by applications containing the Spring Batch framework.

  • The following examples show how to use org.springframework.batch.item.ItemWriter.These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
  • Flat File- Flat File Item Readers read lines of data from a flat file that typically describe records with fields of data defined by fixed positions in the file or delimited by some special character (e.g. When working with flat files in Spring Batch. Fixed Width File Writing Example.

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

  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
Spring batch flat file item writer example pdf

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

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.

RunScheduler.java

11. Run the Main class now

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

Learn to write CSV data using FlatFileItemWriter. It is an item writer that writes data to a file or stream. The location of the output file is defined by a Resource and must represent a writable file.

Project Structure

In this project, we will learn to –

Spring Batch Flat File Item Writer Example Program

  1. Read 3 CSV files from input/*.csv using MultiResourceItemReader.
  2. Write whole data to output/outputData.csv file using FlatFileItemWriter.

Write data CSV files with FlatFileItemWriter

Item

You need to use FlatFileItemWriter to write lines which were read from CSV files. It write the content to any Resource passed to writer.setResource() method.

Spring Batch Flat File Item Writer Example Free

Maven Dependency

Look at project dependencies.

Demo

Before running the application, look at complete code of App.java which run the application as Spring boot application.

Spring Batch Flat File Item Writer Example Pdf

Run the application

Run the application as Spring boot application, and watch the console. Batch job will start at start of each minute. It will read the input file, and print the read values in console.

Drop me your questions in comments section.

Happy Learning !!