Spring Batch Flat File Item Writer Example

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

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

I want to create a flat file which has the below format: Col1Name;Col2Name;Col3Name one;2 two;2 As seen, the first line in the flat file are the column names.

Person.java domain object

  • 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.
  • 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. Assume we have created a table in the MySQL database as shown below.
  • Example Spring Batch Example jobs with admin console to launch and manage. This example implements the following two jobs that read/write flat files and an in memory (HSQL) relational database.

PersonBuilder.java

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.

FlatFileWriterTemplate.java

Spring

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

FlatFileWriter.java

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)

Spring Batch Flat File Item Writer Example Program

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.

writerManager

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

In this post we will learn about how to use Spring Batch to read from MySQL database using JdbcCursorItemReader and write to a Flat file using FlatFileItemWriter. We will also witness the usage of JobExecutionListener and itemProcessor. Let’s get going.

Other interesting posts you may like

Following technologies being used:

  • Spring Batch 3.0.1.RELEASE
  • Spring core 4.0.6.RELEASE
  • Spring jdbc 4.0.6.RELEASE
  • MySQL Server 5.6
  • Joda Time 2.3
  • JDK 1.6
  • Eclipse JUNO Service Release 2

Let’s begin.

Step 1: Create project directory structure

Following will be the final project structure:

We will be reading MySQL database and write to a flat file (project/csv/examResult.txt).

Step 2: Create Database Table and populate it with sample data

Create a fairly simple table in MySQL database which maps to our domain model(and sufficient for this example).

Please visit MySQL installation on Local PC in case you are finding difficulties in setting up MySQL locally.

Now let’s add all contents mentioned in project structure in step 1.

Step 3: Update pom.xml to include required dependencies

Following is the updated minimalistic pom.xml

As we need to interact with db this time, we will use spring-jdbc support. We will also need mysql connector to communicate with MySQL, and since we are also using joda-time for any date-time processing we might need, we will include that dependency as well.

Step 4: Create domain object & Mapper (RowMapper implementaion)

We will be mapping the data from database table to properties of our domain object.

com.websystique.springbatch.model.ExamResult

Below class will eventually map the data from database into domain object based on actual properties datatypes.

com.websystique.springbatch.ExamResultRowMapper

Spring Batch Flat File Item Writer Example Pdf

Step 5: Create an ItemProcessor

ItemProcessor is Optional, and called after item read but before item write. It gives us the opportunity to perform a business logic on each item. In our case, for example, we will filter out all the items whose percentage is less than 80. So final result will only have records with percentage >= 80.

com.websystique.springbatch.ExamResultItemProcessor

Spring Batch Flat File Item Writer Example Free

Step 6: Add a Job listener(JobExecutionListener)

Job listener is Optional and provide the opportunity to execute some business logic before job start and after job completed.For example setting up environment can be done before job and cleanup can be done after job completed.

com.websystique.springbatch.ExamResultJobListener

Step 7: Create Spring Context with job configuration

Create dataSource bean needed for database communication

src/main/resources/context-datasource.xml

Create the Spring context with batch job configuration.

src/main/resources/spring-batch-context.xml

As you can see, we have setup a job with only one step. Step uses JdbcCursorItemReader to read the records from MySQL database, itemProcessor to process the record and FlatFileItemWriter to write the records to a flat file. commit-interval specifies the number of items that can be processed before the transaction is committed/ before the write will happen.Grouping several record in single transaction and write them as chunk provides performance improvement. We have also shown the use of jobListener which can contain any arbitrary logic you might need to run before and after the job.

Spring Batch Flat File Item Writer Examples

Step 8: Create Main application to finally run the job

Create a Java application to run the job.

com.websystique.springbatch.Main

Running above program as java application, you will see following output

You can see that we have processed all input records from Database. Below is the generated flat file (txt) found in project/csv folder

Only the records which are meeting specific condition ( percentage >=80 ) are included here, thanks to itemProcessor filtering logic.

That’s it.

Download Source Code


References

If you like tutorials on this site, why not take a step further and connect me on Facebook , Google Plus & Twitter as well? I would love to hear your thoughts on these articles, it will help improve further our learning process.

Related posts: