SpringBoot+SpringBatch一个配置文件搞定ETL处理和任务调度

SpringBatch Configuration

@Configuration
@EnableBatchProcessing
public class JobConfiguration {
    //注入创建任务对象的对象
    @Autowired
    private JobBuilderFactory jobBuilderFactory ;

    //注入创建setp对象的对象
    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    private DataSource dataSource ;

    //创建任务对象
    @Bean
    public Job deciderJob(){
        return jobBuilderFactory.get("deciderJob3")
                .listener(new MyJobListener())
                .start(step1())
                .next(firstDecider())
                .from(firstDecider()).on("step2").to(step2()).next(step4())
                .from(firstDecider()).on("step3").to(step3()).next(step4())
                .end()
                .build();
    }

    //创建step对象
    @Bean
    public Step step1() {
        TaskletStep step1 = stepBuilderFactory.get("step1").listener(new MyStepListener()).tasklet(new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                System.out.println("hello, the batch job start..............");
                System.out.println(chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext());
                System.out.println(chunkContext.getStepContext().getJobExecutionContext());
                chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext().put("info","huwenchao2");
                return RepeatStatus.FINISHED;
            }
        }).build();
        step1.setAllowStartIfComplete(true);
        return step1;
    }

    @Bean
    public Step step2() {
        TaskletStep step2 = stepBuilderFactory.get("step2").listener(new MyStepListener()).tasklet(new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                System.out.println("hello, this is the step 2 ");
                System.out.println("jobParameters:info= " + chunkContext.getStepContext().getJobExecutionContext().get("info"));
                return RepeatStatus.FINISHED;
            }
        }).build();
        step2.setAllowStartIfComplete(true);
        return step2;
    }

    @Bean
    public Step step3() {
        TaskletStep step3 = stepBuilderFactory.get("step3").listener(new MyStepListener()).tasklet(new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                System.out.println("hello, this is the step 3 ");
                System.out.println("jobParameters:info= " + chunkContext.getStepContext().getJobExecutionContext().get("info"));
                return RepeatStatus.FINISHED;
            }
        }).build();
        step3.setAllowStartIfComplete(true);
        return step3;
    }

    @Bean
    @StepScope
    public ItemReader<User> itemReader(){
        JdbcPagingItemReader itemReader = new JdbcPagingItemReader();

        itemReader.setDataSource(dataSource);
        itemReader.setFetchSize(1);
        itemReader.setRowMapper(new RowMapper() {
            @Override
            public User mapRow(ResultSet rs, int rowNum) throws SQLException {
                User user = new User();
                user.setId(rs.getInt(1));
                user.setName(rs.getString(2));
                user.setAge(rs.getInt(3));
                user.setEmail(rs.getString(4));
                return user;
            }
        });

        MySqlPagingQueryProvider provider = new MySqlPagingQueryProvider();
        provider.setSelectClause("id, name , age, email");
        provider.setFromClause("from hwc_users");
        Map<String, Order> sort = new HashMap<>();
        sort.put("name", Order.DESCENDING);
        provider.setSortKeys(sort);


        itemReader.setQueryProvider(provider);

        return itemReader;
    }

    @Bean
    public Step step4() {
        TaskletStep step4 = stepBuilderFactory.get("step4")
                .listener(new MyStepListener())
                .<User, User>chunk(2)
                .reader(itemReader())
                .writer(new ItemWriter<User>() {
                    @Override
                    public void write(List<? extends User> items) throws Exception {
                        for(User user : items){
                            System.out.println(user);
                        }
                    }
                })
                .build();
        step4.setAllowStartIfComplete(true);
        return step4;
    }

    @Bean
    public JobExecutionDecider firstDecider(){
        return new JobExecutionDecider() {
            @Override
            public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution) {
                int random = new Random().nextInt();
                System.out.println("random number is : " + random);
                if(random % 2 == 0)
                    return new FlowExecutionStatus("step2");
                return new FlowExecutionStatus("step3");
            }
        };
    }

}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring框架和大数据平台在处理ETL(提取、转换和加载)过程时有一些区别。下面是它们之间的主要区别: 1. 功能范围:Spring框架是一个轻量级的Java开发框架,主要用于构建企业级应用程序。它提供了一组库和工具,用于简化Java应用程序的开发和管理。而大数据平台是一种用于处理大规模数据集的技术架构,包括分布式存储、计算和处理引擎等组件。 2. 数据规模:Spring框架通常用于处理中小规模的数据集,适合小型企业或中小型应用程序。而大数据平台专注于处理大规模数据集,可以处理海量数据,适合大型企业或需要处理大数据量的应用场景。 3. 数据处理方式:Spring框架主要通过编写代码来实现ETL过程,开发人员需要手动编写代码来完成数据的提取、转换和加载等操作。而大数据平台通常使用分布式计算和处理引擎,如Hadoop、Spark等,可以自动化地进行并行化的数据处理。 4. 生态系统支持:Spring框架具有丰富的生态系统,提供了许多扩展和集成库,可以与其他技术栈无缝集成。而大数据平台也有自己的生态系统,包括各种数据存储和处理技术,如Hadoop、Hive、HBase等。 总的来说,Spring框架更适合处理中小规模的数据集和应用程序,而大数据平台更适合处理大规模的数据集和需要进行并行化处理的场景。选择哪种方式取决于您的具体需求和应用场景。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值