Spring Batch 2.0 – Part III – From Database to Flat File

In Part-II of this series on Spring Batch, I went through an example of reading from a flat file and persisting into the database. In this article I will go through the reverse. Read 200,000 rows from the database and export it into a comma separated flat file.

The export from the database to the flat file took around 10 seconds. That is excellent for Java-based batch processing. Again I must point out that this is relatively fast since I am using a local MySQL database and there is no processing related logic being performed during the entire process.

The file is a comma separated file with format => receiptDate,memberName,checkNumber,checkDate,paymentType,depositAmount,paymentAmount,comments

DDL for the database table :

Here is the spring application context xml file…

  • 1 through 4 are the same as the previous blog.
  • 5 – Defines the step job and its steps.
  • 6 – Registers a JdbcCursorItemReader which will read the rows from the database. It will then write them out to the flat file. The latest version also has a new reader JdbcPagingItemReader. This is a better option since it will read a predefined set of rows rather than making round trips for each row.
  • 8 – Configure the writer to write to a flat file

Here is the Java code for the mapper LedgerRowMapper.java

Here is test driver class (a the junit test case).

After running the test case, you will see a file c:\temp\ledgers-output.txt with 200,000 rows.

You can download the Maven project from GitHub – https://github.com/thomasma/springbatch3part.