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.
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.
- Maven 3
- Eclipse 4.2
- JDK 1.6
- Spring Core 3.2.2.RELEASE
- 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
There are 3 csv files, later we will use MultiResourceItemReader
to read it one by one.
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
.
Spring Batch Flat File Item Writer Example Free
Spring Batch Flat File Item Writer Examples
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.
Download Source Code
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.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 –
- Read 3 CSV files from
input/*.csv
usingMultiResourceItemReader
. - Write whole data to
output/outputData.csv
file usingFlatFileItemWriter
.
Write data CSV files with FlatFileItemWriter
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 Program
Maven Dependency
Look at project dependencies.
Spring Batch Flat File Item Writer Example Pdf
Demo
Before running the application, look at complete code of App.java
which run the application as Spring boot application.
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 !!