SpringBoot批处理的实现示例
短信预约 -IT技能 免费直播动态提醒
Spring Batch是一个开源 的、全面的、轻量级的批处理框架,通过Spring Boot 可以实现强大的批处理应用程序开发。
Spring Batch提供了ItemReader、ItemProcessor和ItemWriter来完成数据的读取、处理及写出操作,并且可以将批处理的执行状态 持久化到数据库中。
https://spring.io/projects/spring-batch
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>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
2.添加配置 信息
server.port=9000
spring.http.encoding.force-response=true
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql:///batch
spring.datasource.username = root
spring.datasource.password = kangxg198811
#项目启动时创建数据表的SQL脚本
spring.datasource.schema=classpath:/org/springframework/batch/core/schema-mysql.sql
#项目启动时执行建表SQL
spring.batch.initialize-schema=always
#配置后不会自动执行
spring.batch.job.enabled=false
3.在项目启动类上添加@EnableBatchProcessing注解开开启Spring Batch支持
@SpringBootApplication
@EnableBatchProcessing
public class BatchApplication {
public static void main(String[] args) {
SpringApplication.run(BatchApplication.class, args);
}
}
4.配置批处理
@Configuration
public class CsvBatchJobConfig {
@Autowired
JobBuilderFactory jobBuilderFactory;
@Autowired
StepBuilderFactory stepBuilderFactory;
@Autowired
DataSource dataSource;
class CustomLineCallbackHandler implements LineCallbackHandler{
@Override
public void handleLine(String result)
{
System.out.println(result);
}
}
@Bean
ItemReader<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(new String[]{"id","username","address","gender"});
//setDelimiter("\t");
}});
setFieldSetMapper(new BeanWrapperFieldSetMapper<User>(){{
setTargetType(User.class);
}});
}
});
reader.setSkippedLinesCallback(new CustomLineCallbackHandler());
return reader;
}
@Bean
JdbcBatchItemWriter jdbcBatchItemWriter()
{
JdbcBatchItemWriter writer = new JdbcBatchItemWriter();
writer.setDataSource(dataSource);
writer.setSql("insert into user(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();
}
}
5.实体类
public class User {
private Integer id;
private String username;
private String address;
private String gender;
public User(Integer id,String username,String address,String gender)
{
this.username= username;
this.id = id;
this.address = address;
this.gender = gender;
}
public User()
{
super();
}
public void setId(Integer id) {
this.id = id;
}
public void setUsername(String username) {
this.username = username;
}
public void setGender(String gender) {
this.gender = gender;
}
public void setAddress(String address) {
this.address = address;
}
public String getUsername() {
return username;
}
public String getGender() {
return gender;
}
public Integer getId() {
return id;
}
public String getAddress() {
return address;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", address='" + address + '\'' +
", gender='" + gender + '\'' +
'}';
}
}
6.创建Controller
@RestController
public class BatchController {
@Autowired
JobLauncher jobLauncher;
@Autowired
Job job;
@GetMapping("/batch")
public void batch()
{
try {
jobLauncher.run(job,new JobParametersBuilder().toJobParameters());
}catch (Exception e)
{
e.printStackTrace();
}
}
}
这里配置就结束了,数据库会增加一些自动创建到表,但是User表不能自动创建和字段需要提前设置,否则会出现错误,还需要进一步处理,待完成。暂做标记!
到此这篇关于SpringBoot批处理的实现示例的文章就介绍到这了,更多相关SpringBoot批处理内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341