欢迎访问 生活随笔!

凯发k8官方网

当前位置: 凯发k8官方网 > 前端技术 > javascript >内容正文

javascript

springbatch顺序读取多文件(multiresourceitemreader)和顺序写文件(multiresourceitemwriter)(二) -凯发k8官方网

发布时间:2025/1/21 javascript 16 豆豆
凯发k8官方网 收集整理的这篇文章主要介绍了 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 {@overridepublic 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 {@autowiredprivate jobbuilderfactory jobbuilderfactory;@autowiredprivate stepbuilderfactory stepbuilderfactory;@autowiredprivate studentprocessor studentprocessor;@beanpublic job studentmultijob() {return jobbuilderfactory.get("studentmultijob").start(studentmulitstep()).build();}@beanpublic step studentmulitstep() {return stepbuilderfactory.get("studentmulitstep").chunk(10).reader(peoplecommonmultiresourceitemreader()).processor(studentprocessor).writer(studentcommonmultifileitemwriter()).build();}@bean@stepscopepublic commonmultiresourceitemreader peoplecommonmultiresourceitemreader(){return new commonmultiresourceitemreader<>(people.class);}@bean@stepscopepublic commonmultiresourceitemwriter studentcommonmultifileitemwriter() {return new commonmultiresourceitemwriter<>(student.class);} }

五、运行结果

这是读取多文件的文件个数,每个文件有10条数据

写文件文件条数总共是30条。

以上就是multiresourceitemreader和multiresourceitemwriter的基本用法

总结

以上是凯发k8官方网为你收集整理的springbatch顺序读取多文件(multiresourceitemreader)和顺序写文件(multiresourceitemwriter)(二)的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得凯发k8官方网网站内容还不错,欢迎将凯发k8官方网推荐给好友。

网站地图