Overview

Spring Batch video tutorial

Setting up Database

CREATE DATABASE `springbatch` /*!40100 DEFAULT CHARACTER SET utf8 */; DROP TABLE IF EXISTS `springbatch`.`user`; CREATE TABLE `springbatch`.`user` ( `id` int(11) NOT NULL auto_increment, `name` varchar(45) NOT NULL default '', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

Project Structure

Creating the Project

Maven Dependencies

Add the following to your project's pom.xml

<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.0.4</version> </dependency>

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.jackrutorial</groupId> <artifactId>SpringBatchCsvToDatabase</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>SpringBatchCsvToDatabase</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.10.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-batch</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.0.4</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.batch</groupId> <artifactId>spring-batch-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

Create the CSV file

jackrutorial1 jackrutorial2 jackrutorial3

Model

package com.jackrutorial.bean; public class User { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }

Processor

package com.jackrutorial; import org.springframework.batch.item.ItemProcessor; import com.jackrutorial.bean.User; public class UserItemProcessor implements ItemProcessor<User, User> { @Override public User process(User user) throws Exception { return user; } }

Spring Batch Annotations Configuration

package com.jackrutorial; import javax.sql.DataSource; import org.springframework.batch.core.Job; import org.springframework.batch.core.Step; import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; import org.springframework.batch.core.launch.support.RunIdIncrementer; import org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider; import org.springframework.batch.item.database.JdbcBatchItemWriter; import org.springframework.batch.item.file.FlatFileItemReader; import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper; import org.springframework.batch.item.file.mapping.DefaultLineMapper; import org.springframework.batch.item.file.transform.DelimitedLineTokenizer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; import org.springframework.jdbc.datasource.DriverManagerDataSource; import com.jackrutorial.bean.User; @Configuration @EnableBatchProcessing public class BatchConfiguration { @Autowired public JobBuilderFactory jobBuilderFactory; @Autowired public StepBuilderFactory stepBuilderFactory; @Autowired public DataSource dataSource; @Bean public DataSource dataSource() { final DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost/springbatch"); dataSource.setUsername("root"); dataSource.setPassword("root"); return dataSource; } @Bean public FlatFileItemReader<User> reader(){ FlatFileItemReader<User> reader = new FlatFileItemReader<User>(); reader.setResource(new ClassPathResource("users.csv")); reader.setLineMapper(new DefaultLineMapper<User>() {{ setLineTokenizer(new DelimitedLineTokenizer() {{ setNames(new String[] { "name" }); }}); setFieldSetMapper(new BeanWrapperFieldSetMapper<User>() {{ setTargetType(User.class); }}); }}); return reader; } @Bean public UserItemProcessor processor(){ return new UserItemProcessor(); } @Bean public JdbcBatchItemWriter<User> writer(){ JdbcBatchItemWriter<User> writer = new JdbcBatchItemWriter<User>(); writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<User>()); writer.setSql("INSERT INTO user(name) VALUES (:name)"); writer.setDataSource(dataSource); return writer; } @Bean public Step step1() { return stepBuilderFactory.get("step1").<User, User> chunk(3) .reader(reader()) .processor(processor()) .writer(writer()) .build(); } @Bean public Job importUserJob() { return jobBuilderFactory.get("importUserJob") .incrementer(new RunIdIncrementer()) .flow(step1()) .end() .build(); } }

Run Spring Boot Batch Job

References

In this tutorial, we show you how to read information from a CSV file and write to MySQL Database using Spring Batch Boot and Spring tools Eclipse. Using Spring Batch Flat file reader to read CSV file and Jdbc Batch Item Writer to write MySQL Database.Execute the following MySQL script in order to create a database namedwith a table namedThe following screenshot shows final structure of the project.Open Eclipse Oxygen java and install the Spring STS Eclipse Go toSelectunder Spring Boot category then click Next as shown belowIn the next screen, you enter the content as shown below then clickIn the next step, you chooseand choose the Batch dependencies, then click FinishThefileWe create thefile infolder and write the following content in it.Create aclass underpackage and write the following code in it.A custom user processor class that will process each and every User object. Create aclass underpackage and write the following code in it.Create a Job which will read users CSV file and write into Mysql Database. Create aclass underpackage and write the following code in it.Right click to theclass, select. This application will read data fromfile and write records in user table.And finally, check the user table in database. Below is the snapshot from database after this job.