SpringBatch:
开源、全面、轻量级的批处理框架,通过SpringBatch可以实现强大的批处理应用程序的开发。
功能:
- 记录/跟踪
- 事务管理
- 作业处理统计
- 作业重启
- 资源管理
SpringBatch还提供了ItemReader、ItemProcessor、ItemWritter完成数据的读取、处理和写出操作,将批处理的执行状态持久化到数据库。
SpringBoot整合SpringBatch:
1.添加依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
添加数据库依赖是为了将批处理的执行状态持久化到数据库中。
2. 配置基本信息:
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.username=root
spring.datasource.password=数据库密码
spring.datasource.url=jdbc:mysql:///数据库名?serverTimezone=UTC
#项目启动创建数据表的sql脚本,由spring batch提供
spring.datasource.schema=classpath:/org/springframework/batch/core/schema-mysql.sql
#项目启动是时执行建表sql
spring.batch.initialize-schema=always
#禁止springbatch自动执行
spring.batch.job.enabled=false
#默认情况下,当项目启动就会执行配置号的批处理操作,如上配置后则不会自动执行,需要手动触发执行。
3.配置批处理:
4.准备data.csv,后面编码将csv的内容批处理持久化到mysql数据库:
5.配置批处理:
package com.yinlei.batch;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
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.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.batch.item.file.transform.LineTokenizer;
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 javax.sql.DataSource;
@Configuration
public class CSVBatchJobConfig {
@Autowired
JobBuilderFactory jobBuilderFactory;
@Autowired
StepBuilderFactory stepBuilderFactory;
@Autowired
DataSource dataSource;
@Bean
@StepScope
FlatFileItemReader<User> itemReader(){
FlatFileItemReader<User> reader = new FlatFileItemReader<User>();
reader.setLinesToSkip(1);
reader.setResource(new ClassPathResource("data.csv"));
reader.setLineMapper(new DefaultLineMapper<User>(){{
setLineTokenizer(new DelimitedLineTokenizer(){{
setNames("id","username","address","gender");
setDelimiter("\t");
}});
setFieldSetMapper(new BeanWrapperFieldSetMapper<User>(){{
setTargetType(User.class);
}});
}});
return reader;
}
@Bean
JdbcBatchItemWriter jdbcBatchItemWriter(){
JdbcBatchItemWriter writer = new JdbcBatchItemWriter();
writer.setDataSource(dataSource);
writer.setSql("insert into uesr(id,username,address,gender)"+"values(:id,:username,:address,:gender)");
writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>());
return writer;
}
@Bean
Step csvStep(){
return stepBuilderFactory.get("csvStep")
.<User,User>chunk(2)
.reader(itemReader())
.writer(jdbcBatchItemWriter())
.build();
}
@Bean
Job csvJob(){
return jobBuilderFactory.get("csvJob")
.start(csvStep())
.build();
}
}
package com.yinlei.batch;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class User {
private Integer id;
private String username;
private String address;
private String gender;
}
编写控制器:
package com.yinlei.batch;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
JobLauncher jobLauncher;
@Autowired
Job job;
@GetMapping("/hello")
public void hello(){
try {
jobLauncher.run(job,new JobParameters());
}catch (Exception e){
e.printStackTrace();
}
}
}
当运行项目后,该数据库会生成一涨User表,内容是data.csv的内容