欢迎访问 生活随笔!

凯发ag旗舰厅登录网址下载

当前位置: 凯发ag旗舰厅登录网址下载 > 前端技术 > javascript >内容正文

javascript

springbatch 读取json(jsonitemreader)用法(十一) -凯发ag旗舰厅登录网址下载

发布时间:2025/1/21 javascript 14 豆豆
凯发ag旗舰厅登录网址下载 收集整理的这篇文章主要介绍了 springbatch 读取json(jsonitemreader)用法(十一) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

文章目录

    • 一、抽取顺序读取数据库公共reader
    • 二、processor
    • 三、配置读取json数据job
    • 四、执行job

前言:在一些业务场景中,可能需要读取json,来做业务逻辑处理,springbatch已经帮我们封装好了读取json的reader

springbatch其它文章直通车:

  • springbatch读单个文件(flatfileitemreader)和写单个文件(flatfileitemwriter)(一)
  • springbatch顺序读取多文件(multiresourceitemreader)和顺序写文件(multiresourceitemwriter)(二)
  • springbatch读数据库(mybatispagingitemreader)(三)
  • springbatch读文件(flatfileitemreader)写据库(mybatisbatchitemwriter)(四)
  • springbatch 监听器之job监听器(jobexecutionlistener)和step监听器(stepexecutionlistener)(五)
  • springbatch 监听器之chunk监听器(chunklistener)和skip监听器(skiplistener)(六)
  • springbatch 多线程(taskexecutor)启动job详解 (七)
  • springbatch 配置并行启动job详解 (八)
  • springbatch 批处理分区(partitioner )分片(九)
  • springbatch tasklet实现和用法(十)

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

一、抽取顺序读取数据库公共reader

该jsonitemreader是json实现 org.springframework.batch.item.json.jsonobjectreader接口。此接口旨在通过使用流api以块的形式读取json对象来实现。目前提供两种实现方式:

jaskjson: org.springframework.batch.item.json.jacksonjsonobjectreader

gson:org.springframework.batch.item.json.gsonjsonobjectreader

json 数据格式为:

[{"id": "12","catname": "小白","catage": "1","cataddress": "foo"},{"id": "13","catname": "小嘿","catage": "2","cataddress": "boo"} ]

commonjsonitemreader继承jsonitemreader

package com.sl.common;import org.springframework.batch.item.json.jsonitemreader; import org.springframework.batch.item.json.jsonobjectreader; import org.springframework.core.io.resource;/*** @author shuliangzhao* @title: commonjsonitemreader* @projectname spring-boot-learn* @description: todo* @date 2019/9/17 20:00*/ public class commonjsonitemreader extends jsonitemreader {public commonjsonitemreader(resource resource, jsonobjectreader jsonobjectreader) {super(resource, jsonobjectreader);setjsonobjectreader(jsonobjectreader);setname("tradejsonitemreader");setresource(resource);} }

二、processor

package com.sl.processor;import com.sl.common.commonprocessor; import com.sl.entity.cafecat; import com.sl.entity.cat; import org.springframework.batch.core.configuration.annotation.stepscope; import org.springframework.stereotype.component;/*** @author shuliangzhao* @title: catjsonprocessor* @projectname spring-boot-learn* @description: todo* @date 2019/9/17 20:14*/ @component @stepscope public class catjsonprocessor extends commonprocessor {@overridepublic void processor(cafecat o, cat cat) {o.setcatname(cat.getcatname());o.setcatage(cat.getcatage());o.setcataddress(cat.getcataddress());} }

三、配置读取json数据job

package com.sl.config;import com.sl.common.commonfileitemwriter; import com.sl.common.commonjsonitemreader; import com.sl.entity.cafecat; import com.sl.entity.cat; import com.sl.processor.catjsonprocessor; 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.batch.item.json.jacksonjsonobjectreader; import org.springframework.beans.factory.annotation.autowired; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; import org.springframework.core.io.filesystemresource;/*** json文件处理* @author shuliangzhao* @title: catjsonconfiguration* @projectname spring-boot-learn* @description: todo* @date 2019/9/17 19:59*/ @configuration @enablebatchprocessing public class catjsonconfiguration {@autowiredprivate jobbuilderfactory jobbuilderfactory;@autowiredprivate stepbuilderfactory stepbuilderfactory;@autowiredprivate catjsonprocessor catjsonprocessor;@beanpublic job catjsonjob() {return jobbuilderfactory.get("catjsonjob").start(catjsonstep()).build();}@beanpublic step catjsonstep() {return stepbuilderfactory.get("catjsonstep").chunk(10).reader(commonjsonitemreader()).processor(catjsonprocessor).writer(cafecatjsoncommonfileitemwriter()).build();}@bean@stepscopepublic commonjsonitemreader commonjsonitemreader() {filesystemresource filesystemresource = new filesystemresource("d:\\aplus\\shuqian\\target\\" cat.class.getsimplename() ".json");return new commonjsonitemreader(filesystemresource,new jacksonjsonobjectreader<>(cat.class));}@bean@stepscopepublic commonfileitemwriter cafecatjsoncommonfileitemwriter() {return new commonfileitemwriter<>(cafecat.class);} }

四、执行job

执行job ,写文件

总结

以上是凯发ag旗舰厅登录网址下载为你收集整理的springbatch 读取json(jsonitemreader)用法(十一)的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得凯发ag旗舰厅登录网址下载网站内容还不错,欢迎将凯发ag旗舰厅登录网址下载推荐给好友。

网站地图