Spring Batch入门教程篇
程序员文章站
2023-12-20 14:34:16
springbatch介绍:
springbatch 是一个大数据量的并行处理框架。通常用于数据的离线迁移,和数据处理,⽀持事务、并发、流程、监控、纵向和横...
springbatch介绍:
springbatch 是一个大数据量的并行处理框架。通常用于数据的离线迁移,和数据处理,⽀持事务、并发、流程、监控、纵向和横向扩展,提供统⼀的接⼝管理和任务管理;springbatch是springsource和埃森哲为了统一业界并行处理标准为广大开发者提供方便开发的一套框架。
官方地址:github.com/spring-projects/spring-batch
- springbatch 本身提供了重试,异常处理,跳过,重启、任务处理统计,资源管理等特性,这些特性开发者看重他的主要原因;
- springbatch 是一个轻量级的批处理框架;
- springbatch 结构分层,业务与处理策略、结构分离;
- 任务的运行的实例状态,执行数据,参数都会落地到数据库;
快速入门
pom.xml 添加
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-batch</artifactid> </dependency>
创建batchconfig(可以是其他类名)
@configuration @enablebatchprocessing public class batchconfig { // tag::readerwriterprocessor[] @bean public flatfileitemreader<person> flatfileitemreader() { flatfileitemreader<person> reader = new flatfileitemreader<>(); reader.setresource(new classpathresource("sample-data.csv")); fixedlengthtokenizer fixedlengthtokenizer = new fixedlengthtokenizer(); reader.setlinemapper(new defaultlinemapper<person>() {{ setlinetokenizer(new delimitedlinetokenizer() {{ setnames(new string[]{"firstname", "lastname"}); }}); setfieldsetmapper(new beanwrapperfieldsetmapper<person>() {{ settargettype(person.class); }}); }}); return reader; } @bean public jdbcpagingitemreader<person> jdbcpagingitemreader(datasource datasource) { jdbcpagingitemreader<person> reader = new jdbcpagingitemreader<>(); reader.setdatasource(datasource); reader.setfetchsize(100); reader.setqueryprovider(new mysqlpagingqueryprovider() {{ setselectclause("select person_id,first_name,last_name"); setfromclause("from people"); setwhereclause("last_name=:lastname"); setsortkeys(new hashmap<string, order>() {{ put("person_id", order.ascending); }}); }}); reader.setparametervalues(new hashmap<string, object>() {{ put("lastname", "doe"); }}); reader.setrowmapper(new beanpropertyrowmapper<>(person.class)); return reader; } @bean public jdbcbatchitemwriter<person> jdbcbatchitemwriter(datasource datasource) { jdbcbatchitemwriter<person> writer = new jdbcbatchitemwriter<>(); writer.setitemsqlparametersourceprovider(new beanpropertyitemsqlparametersourceprovider<>()); writer.setsql("insert into people (first_name, last_name) values (:firstname, :lastname)"); writer.setdatasource(datasource); return writer; } /*@bean public flatfileitemwriter<person> flatfileitemwriter(datasource datasource) { flatfileitemwriter<person> writer = new flatfileitemwriter<>(); writer.setappendallowed(true); writer.setencoding("utf-8"); // writer.set(datasource); return writer; }*/ // end::readerwriterprocessor[] // tag::jobstep[] @bean public job importuserjob(jobbuilderfactory jobbuilderfactory, jobcompletionnotificationlistener listener, step step) { return jobbuilderfactory.get("importuserjob") .incrementer(new runidincrementer()) .listener(listener) .start(step) .build(); } @bean public step step1(stepbuilderfactory stepbuilderfactory, personitemprocessor processor, itemwriter jdbcbatchitemwriter, itemreader flatfileitemreader) { /*compositeitemprocessor compositeitemprocessor = new compositeitemprocessor(); compositeitemprocessor.setdelegates(lists.newarraylist(processor, processor));*/ return stepbuilderfactory.get("step1") .<person, person>chunk(10) .reader(flatfileitemreader) .processor(processor) .writer(jdbcbatchitemwriter) .build(); } // end::jobstep[] }
spring batch的分层架构
- insfrastructure 策略管理:包括任务的失败重试,异常处理,事务,skip,以及数据的输入输出(文本文件,db,message)
- core: springbatch 的核心,包括joblauch,job,step等等
- application: 业务处理,创建任务,决定任务的执行方式(定时任务,手动触发等)
spring batch执行流程
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。