SpringBatch顺序读取多文件(MultiResourceItemReader)和顺序写文件(MultiResourceItemWriter)(二)

文章目录

    • 一、抽取顺序读取多文件公共reader
    • 二、抽取写文件公共基于MultiResourceItemWriter
    • 三、实现自己的ResourceSuffixCreator
    • 四、读取多文件job
    • 五、运行结果

前言:上一章我们讲解了SpringBatch读取单个文件和写文件的具体用法,这一张我们继续讲解读取多个文件的用法。

我们在一些特殊的业务场景中,需要顺序读取多个文件,幸运的是SpringBatch已经给我们提供了公共Reader MultiResourceItemReader

代码已上传GitHub上面地址:https://github.com/FadeHub/spring-boot-learn/tree/master/spring-boot-springbatch

SpringBatch其它文章直通车:

SpringBatch读单个文件(FlatFileItemReader)和写单个文件(FlatFileItemWriter)(一)
SpringBatch顺序读取多文件(MultiResourceItemReader)和顺序写文件(MultiResourceItemWriter)(二)
SpringBatch读数据库(MyBatisPagingItemReader)(三)
SpringBatch读文件(FlatFileItemReader)写据库(MyBatisBatchItemWriter)(四)
SpringBatch 监听器之Job监听器(JobExecutionListener)和Step监听器(StepExecutionListener)(五)
SpringBatch 监听器之Chunk监听器(ChunkListener)和Skip监听器(SkipListener)(六)

一、抽取顺序读取多文件公共reader

CommonMultiResourceItemReader继承MultiResourceItemReader

/**
 * 顺序读取多个文件
 * @author shuliangzhao
 * @Title: CommonMultiResourceItemReader
 * @ProjectName spring-boot-learn
 * @Description: TODO
 * @date 2019/9/8 12:34
 */
public class CommonMultiResourceItemReader  extends MultiResourceItemReader {

    public CommonMultiResourceItemReader(Class clz) {
        setResources(getResource());
        setDelegate(new CommonFileItemReader<>(clz));
    }

    private Resource[] getResource() {
        List resourceList = new ArrayList<>();
        String filePath = "D:\\aplus\\shuqian\\target";
        File file = new File(filePath);
        if (file.isDirectory()) {
            String[] list = file.list();
            if (list != null) {
                for (String str : list) {
                    String resource = file.getPath() + "\\" + str;
                    FileSystemResource fileSystemResource = new FileSystemResource(resource);
                    resourceList.add(fileSystemResource);
                }
            }
        }
        Resource[] resources = new Resource[resourceList.size()];
        return resourceList.toArray(resources);
    }
}

敲黑板:setDelegate(new CommonFileItemReader<>(clz));这个CommonFileItemReader是上一章公共读取单个文件的reader

二、抽取写文件公共基于MultiResourceItemWriter

CommonMultiResourceItemWriter继承MultiResourceItemWriter

**
 * @author shuliangzhao
 * @Title: CommonMultiResourceItemWriter
 * @ProjectName spring-boot-learn
 * @Description: TODO
 * @date 2019/9/8 13:05
 */
public class CommonMultiResourceItemWriter extends MultiResourceItemWriter {

    public CommonMultiResourceItemWriter(Class clz) {
        setDelegate(new CommonFileItemWriter<>(clz));
        setResource(getResource(clz));
        setResourceSuffixCreator(new CsvResourceSuffixCreator());
    }

    private Resource getResource(Class clz) {
       FileSystemResource fileSystemResource = new FileSystemResource("D:\\aplus\\shuqian\\source\\" +clz.getSimpleName());
        return fileSystemResource;
    }

}

三、实现自己的ResourceSuffixCreator

CsvResourceSuffixCreator 实现ResourceSuffixCreator 接口

package com.sl.resource;

import org.springframework.batch.item.file.ResourceSuffixCreator;

/**
 * @author shuliangzhao
 * @Title: CsvResourceSuffixCreator
 * @ProjectName spring-boot-learn
 * @Description: TODO
 * @date 2019/9/8 13:20
 */
public class CsvResourceSuffixCreator implements ResourceSuffixCreator {
    @Override
    public String getSuffix(int i) {
        return i + ".csv" ;
    }
}

四、读取多文件job

读取多文件job使用上一章job改造之后的

package com.sl.config;

import com.sl.common.CommonFileItemWriter;
import com.sl.common.CommonMultiResourceItemReader;
import com.sl.common.CommonMultiResourceItemWriter;
import com.sl.entity.People;
import com.sl.entity.Student;
import com.sl.processor.StudentProcessor;
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.configuration.annotation.StepScope;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author shuliangzhao
 * @Title: StudentMultiConfiguration
 * @ProjectName spring-boot-learn
 * @Description: TODO
 * @date 2019/9/8 12:41
 */
@Configuration
@EnableBatchProcessing
public class StudentMultiConfiguration {
    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    private StudentProcessor studentProcessor;

    @Bean
    public Job studentMultiJob() {
        return jobBuilderFactory.get("studentMultiJob")
                .start(studentMulitStep())
                .build();
    }

    @Bean
    public Step studentMulitStep() {
        return stepBuilderFactory.get("studentMulitStep")
                .chunk(10)
                .reader(peopleCommonMultiResourceItemReader())
                .processor(studentProcessor)
                .writer(studentCommonMultiFileItemWriter())
                .build();
    }

    @Bean
    @StepScope
    public CommonMultiResourceItemReader peopleCommonMultiResourceItemReader(){
        return new CommonMultiResourceItemReader<>(People.class);
    }

    @Bean
    @StepScope
    public CommonMultiResourceItemWriter studentCommonMultiFileItemWriter() {
        return new CommonMultiResourceItemWriter<>(Student.class);
    }
}

五、运行结果

这是读取多文件的文件个数,每个文件有10条数据
SpringBatch顺序读取多文件(MultiResourceItemReader)和顺序写文件(MultiResourceItemWriter)(二)_第1张图片
写文件文件条数总共是30条。
SpringBatch顺序读取多文件(MultiResourceItemReader)和顺序写文件(MultiResourceItemWriter)(二)_第2张图片
以上就是MultiResourceItemReader和MultiResourceItemWriter的基本用法

你可能感兴趣的:(java技能提升,springboot系列文章,springbatch)