CSV file read and write using Spring Batch

KALYANI GOLLA
5 min readNov 5, 2019

READ THE DATA FROM CSV FILE AND SEND THE DATA TO MYSQL DATABASE

We show you how to configure Spring Batch Boot Job to read information from Mysql Database and write to CSV file using SpringBoot.Spring Batch Boot to read from MySQL database using JdbcCursorItemReader and write to a Flat file using FlatFileItemWriter.

Create Database Table and Populate it with Sample data:

CREATE DATABASE employees;

CREATE TABLE employees.employee(name varchar(225),address varchar(225),salary varchar(225));

Project Structure:

The below screenshot shows the final structure of the project

Creating the Project:

Open SpringBoot app Go to File -> New -> Other ….. Select the Spring Starter web project then click on Next as shown in below figure

In the next screen, you enter the Name of the Project and also add the name of package then Click on Next

In the Next Step, you want to choose SpringBoot Version is 2.2.0 and also choose the Batch dependencies I/O -> Batch and also SQL -> MysqlDriver, then Click on FINISH button.

Application.properties:

In this, we have written the properties values for the spring-boot application under the src/main/resources folder.

Create the CSV File :

  • We create the Employee.java file in the src/main/java folder.
  • Create an Employee.java class in com.springbatch.csv.db package and write the following code in it for the Employee class. Employee class is nothing but Bean or Pojo class.

Processor:

A custom user processor class that will process each and every user object. Create EmployeeProcessor.java class under com.springbatch.csv.db package and the following code :

Spring Batch Boot Configurations:

Create a job that will read from the database and write it into a CSV file. Create BatchConfiguration.java class under com. springbatch.csv.DB package and the following code:

Now Create users.csv file. Go to File -> New -> Other … Select the General -> File then Click on the Next button.

From the above figure write the File Name and Click on Finish Button. In this users.csv write the data on the file as shown in the below figure.

Now run the SpringBatchExampleFromCsvtoDatabaseApplication class, select Run As -> Spring Boot App.

Now Tomato server is running at port 8080 and also took at the following status it shows the COMPLETED. Now Go to the Mysql Database and check the database table as shown in the below figure.

READ THE DATA FROM MYSQL DATABASE WRITE INTO A CSV FILE

We show you how to configure Spring Batch Boot Job to read information from a CSV file and write to MySQL Database using SpringBoot.Spring Batch Boot to read from MySQL database using JdbcCursorItemReader and write to a Flat file using FlatFileItemWriter.

Create Database Table and Populate it with Sample data:

CREATE DATABASE employees;

CREATE TABLE employees.employee(name varchar(225),address varchar(225),salary varchar(225));

INSERT INTO employees.employee(name,address,salary) VALUES(Kalyani’,’Hyderabad’,’10000');

INSERT INTO employees.employee(name,address,salary) VALUES(‘sweety’,Chennai’,’20000');

INSERT INTO test3.user(name,address,salary) VALUES(‘Sree’,’Vizag’,’30000');

Project Structure:

The following screenshot shows the final structure of the project:

Creating the Project :

Open SpringBoot app Go to File -> New -> Other ….. Select the Spring Starter web project then click on Next as shown in below figure

In the next screen, you enter the Name of the Project and also add the name of package then Click on Next.

In the Next Step, you want to choose SpringBoot Version is 2.2.0 and also choose the Batch dependencies I/O -> Batch and also SQL -> MysqlDriver, then Click on FINISH button.

Application.properties:

In this, we have written the properties values for the spring-boot application under the src/main/resources folder.

Create the CSV File:

  • We create the users.csv file in the src/main/resources folder. Model
  • Create a User class under com.springBoot_batch package and write the following code in it.

Processor :

A custom user processor class that will process each and every User object. Create a UserItemProcessor class under com.spring-boot package and write the following code in it.

And also create a UserRowMapper class under com.spring-boot package and write the following code in it.

SPRING BATCH BOOT CONFIGURATION:

Create a Job that will read from the database and write into the CSV file. Create a BatchConfiguration class under com.spring-boot package and write the following code in it.

Run Spring Batch Boot :

Right-click to the SpringBootDatabaseToCsv Application Class , select Run As -> Spring Boot App. This application will read the data from the user table and write it into the CSV file.

If we want to check whether the values are stored in user.csv file than Go To -> ProjectFolder \ spring-boot \ SpringBootDatabaseToCsv \ target \ classes \ users.

--

--