欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

SpringBoot批处理的实现示例

时间:2022-10-30 11:08:59|栏目:JAVA代码|点击:

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表不能自动创建和字段需要提前设置,否则会出现错误,还需要进一步处理,待完成。暂做标记!

上一篇:Java 递归重难点分析详解与练习

栏    目:JAVA代码

下一篇:jmeter如何自动生成测试报告

本文标题:SpringBoot批处理的实现示例

本文地址:http://www.codeinn.net/misctech/217734.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有