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.
1111,ATUL KUMAR,17,300000.0,delhi
3333,ASHUTOSH RAJPUT,21,400000.0,delhi
4444,Adesh Verma,27,80000.0,Kanpur
5555,Dinesh Rajput,37,300000.0,Noida
2222,ATUL KUMAR,17,300000.0,delhi
6666,ASHUTOSH RAJPUT,21,400000.0,delhi
7777,Adesh Verma,27,80000.0,Kanpur
8888,Dinesh Rajput,37,300000.0,Noida
<bean id="employee" class="com.doj.batch.bean.Employee" scope="prototype"></bean> <bean id="cvsItemReader" class="org.springframework.batch.item.file.FlatFileItemReader"> <!-- Read a csv file --> <property name="resource" value="classpath:csv/employees.csv" /> <property name="lineMapper"> <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper"> <!-- split it --> <property name="lineTokenizer"> <bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer"> <property name="names" value="empid,name,age,salary,address" /> </bean> </property> <property name="fieldSetMapper"> <!-- map to an object --> <bean class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper"> <property name="prototypeBeanName" value="employee" /> </bean> </property> </bean> </property> </bean>
Employee.java
package com.doj.batch.bean; /** * @author Dinesh Rajput * */ public class Employee { private int empid; private String name; private int age; private float salary; private String address; /** * @return the empid */ public int getEmpid() { return empid; } /** * @param empid the empid to set */ public void setEmpid(int empid) { this.empid = empid; } /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } /** * @return the age */ public int getAge() { return age; } /** * @param age the age to set */ public void setAge(int age) { this.age = age; } /** * @return the salary */ public float getSalary() { return salary; } /** * @param salary the salary to set */ public void setSalary(float salary) { this.salary = salary; } /** * @return the address */ public String getAddress() { return address; } /** * @param address the address to set */ public void setAddress(String address) { this.address = address; } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd"> <!-- connect to database --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/DAVDB" /> <property name="username" value="root" /> <property name="password" value="root" /> </bean> <bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" /> </beans>
Define jobRepository and jobLauncher.
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager"/> <bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher"> <property name="jobRepository" ref="jobRepository"/> </bean> <bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean"> <property name="transactionManager" ref="transactionManager"/> </bean> <bean id="simpleJob" class="org.springframework.batch.core.job.SimpleJob" abstract="true"> <property name="jobRepository" ref="jobRepository" /> </bean> </beans>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:batch="http://www.springframework.org/schema/batch" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.0.xsd"> <import resource="applicationContext.xml"/> <import resource="ApplicationDB.xml"/> <bean id="employee" class="com.doj.batch.bean.Employee" scope="prototype"></bean> <bean id="cvsItemReader" class="org.springframework.batch.item.file.FlatFileItemReader"> <!-- Read a csv file --> <property name="resource" value="classpath:csv/employees.csv" /> <property name="lineMapper"> <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper"> <!-- split it --> <property name="lineTokenizer"> <bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer"> <property name="names" value="empid,name,age,salary,address" /> </bean> </property> <property name="fieldSetMapper"> <!-- map to an object --> <bean class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper"> <property name="prototypeBeanName" value="employee" /> </bean> </property> </bean> </property> </bean> <bean id="databaseItemWriter" class="org.springframework.batch.item.database.JdbcBatchItemWriter"> <property name="dataSource" ref="dataSource" /> <property name="sql"> <value> <![CDATA[ insert into EMPLOYEET(empname,empAge,salary,empaddress) values (:name,:age,:salary,:address) ]]> </value> </property> <!-- It will take care matching between object property and sql name parameter --> <property name="itemSqlParameterSourceProvider"> <bean class="org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider" /> </property> </bean> <batch:job id="simpleDojJob" job-repository="jobRepository" parent="simpleJob"> <batch:step id="step1"> <batch:tasklet transaction-manager="transactionManager"> <batch:chunk reader="cvsItemReader" writer="databaseItemWriter" commit-interval="2"/> </batch:tasklet> </batch:step> </batch:job> </beans>
This is the main xml file to configure the Spring batch job. This simple-job.xml file define a job to read a employees.csv file, match it to employee plain pojo and write the data into MySQL database Employeet table.
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, we will be ignored all employees whose have salaried less than 70000 i.e. not write to database as well as ignored those employee whose age less than 22 years.
DataFilterProcessor.java
package com.doj.batch.processor; import org.springframework.batch.item.ItemProcessor; import com.doj.batch.bean.Employee; /** * @author Dinesh Rajput * */ public class DataFilterProcessor implements ItemProcessor<Employee, Employee> { @Override public Employee process(Employee emp) throws Exception { if(emp.getSalary() > 70000.0 && emp.getAge() > 22){ return emp; }else{ return null; } } }
Spring Batch comes with a simple utility class called CommandLineJobRunner which has a main() method which accepts two arguments. First argument is the spring application context file containing job definition and the second is the name of the job to be executed.
Now run as a java application with both two arguments.
org.springframework.batch.core.launch.support.CommandLineJobRunner
simple-job.xml simpleDojJob
Output. The Spring Batch metadata tables are created, and the content of employees.cvs is inserted into database table “EMPLOYEET”.
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…