Mar 07, 2014 Previous Next In this tutorial, we will discuss about an application where we see how to configure a Spring Batch job to read CSV file by FlatFileReader library write into a MySQL database, and also we can filter out some of the records of employees which have less salary or under age employee before writing with ItemProcessor. Tools and libraries used Spring Tool Suite (STS) JDK 1.6 Spring.
- Spring Batch Tutorial
- Spring Batch Useful Resources
- Selected Reading
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.
Assume we have inserted the following records into it.
Writer − The Writer we are using in the application is FlatFileItemWriter to write the data to flatfile (.txt).
Processor − The Processor we are using in the application is a custom processor which just prints the records read from the CSV file.
jobConfig.xml
Following is the configuration file of our sample Spring Batch application. In this file, we will define the Job and the Steps. In addition to these, we also define the beans for ItemReader, ItemProcessor, and ItemWriter. (Here, we associate them with respective classes and pass the values for the required properties to configure them.)
Context.xml
Following is the context.xml of our Spring Batch application. In this file, we will define the beans like job repository, job launcher, and transaction manager.
CustomItemProcessor.java
Following is the Processor class. In this class, we write the code of processing in the application. Here, we are printing the contents of each record.
TutorialRowMapper.java
Following is the TutorialRowMapper class which sets the data to the Tutorial class.
Tutorial.java
Following is the Tutorial class. It is a simple Java class with setter and getter methods. In this class, we are using annotations to associate the methods of this class with the tags of the XML file.
App.java
Following is the code which launces the batch process. In this class, we will launch the Batch Application by running the JobLauncher.
On executing this application, it will produce the following output.
This will generate a .txt file with the following contents.
SPRING BATCH: Write data to a H2 database
WithSpring Batch, you can define and run jobs. The jobs may have a step or many steps.
Typically Batch Jobs are long-running, non-interactive and process large volumes of data, more than fits in memory or a single transaction. During a step, the job do something, a particular task, named tasklet. When you define a step you can define the next step you have to execute. A job could execute steps in a defined order.
When you define a step you have 2 models:
1) run a simple task (tasklet)
2) run a step using the following pattern READ-PROCESS-WRITE
This tutorial explains you how to use the second approach. The first model can be seen when you read the article named 'SPRING BATCH: Create a Step'.
We can have in Spring Batch: sequential steps and parallel steps. We can execute some tasks (steps) when a condition is true or false. Because of these things, you can create workflows using Spring Batch.
For more information related to Spring Batch model you can take a look to the article named 'SPRING BATCH: The Main Concepts'.
In this tutorial you can see how you can read a CSV file and write the content into a H2 database all this in one step. This step follow the pattern READ-PROCESS-WRITE, but in this example I skip the 'PROCESS' activity for creating a simpler example.
In order to create a simple example using the pattern READ-PROCESS-WRITE in Spring Batch, first you have to create a simple Maven Spring project with the pom.xml file having the following dependencies:
At this point it is supposed that the Job Repository has been configured already. Take a look at the article named 'SPRING BATCH: Infrastructure Configuration (Using H2)'.
In order to see how to create a READ-PROCESS_WRITE step, you have to create a batch.properties file with the following content :
and add the following classes:
The registration.csv file has the following content:
In H2 database Console, you have to create the following table:
Now you have to run the Spring application and you can see the following information added into the BATCH.REGISTRATION table from the H2 database:
If you want, you can take a look at the following articles as well : SPRING BATCH: Sequential Steps, SPRING BATCH: Concurrent Steps.
Note:
This article was inspired from the book named 'Spring 5 Recipes: A problem-Solution Approach', Fourth Edition, written by Marten Deinum, Daniel Rubio and Josh Long.