2019独角兽企业重金招聘Python工程师标准>>>

Spring Batch_ItemReaders and ItemWriters

All batch processing can be described in its most simple form as reading in large amounts of data, performing some type of calculation or transformation, and writing the result out. Spring Batch provides three key interfaces to help perform bulk reading and writing: ItemReader, ItemProcessor and ItemWriter.

ItemReader

Although a simple concept, an ItemReader is the means for providing data from many different types of input. The most general examples include:

  1. Flat File- Flat File Item Readers read lines of data from a flat file that typically describe records with fields of data defined by fixed positions in the file or delimited by some special character (e.g. Comma).

  2. XML - XML ItemReaders process XML independently of technologies used for parsing, mapping and validating objects. Input data allows for the validation of an XML file against an XSD schema.

  3. Database - A database resource is accessed to return resultsets which can be mapped to objects for processing. The default SQL ItemReaders invoke a RowMapper to return objects, keep track of the current row if restart is required, store basic statistics, and provide some transaction enhancements that will be explained later.

There are many more possibilities, but we'll focus on the basic ones for this chapter. A complete list of all available ItemReaders can be found in Appendix A.

ItemReader is a basic interface for generic input operations:

public interface ItemReader<T> {T read() throws Exception, UnexpectedInputException, ParseException;
}

The read method defines the most essential contract of the ItemReader; calling it returns one Item or null if no more items are left. An item might represent a line in a file, a row in a database, or an element in an XML file. It is generally expected that these will be mapped to a usable domain object (i.e. Trade, Foo, etc) but there is no requirement in the contract to do so.

It is expected that implementations of the ItemReader interface will be forward only. However, if the underlying resource is transactional (such as a JMS queue) then calling read may return the same logical item on subsequent calls in a rollback scenario. It is also worth noting that a lack of items to process by an ItemReader will not cause an exception to be thrown. For example, a database ItemReader that is configured with a query that returns 0 results will simply return null on the first invocation of read.

ItemWriter

ItemWriter is similar in functionality to an ItemReader, but with inverse operations. Resources still need to be located, opened and closed but they differ in that an ItemWriter writes out, rather than reading in. In the case of databases or queues these may be inserts, updates, or sends. The format of the serialization of the output is specific to each batch job.

As with ItemReader, ItemWriter is a fairly generic interface:

public interface ItemWriter<T> {void write(List<? extends T> items) throws Exception;
}

As with read on ItemReader, write provides the basic contract of ItemWriter; it will attempt to write out the list of items passed in as long as it is open. Because it is generally expected that items will be 'batched' together into a chunk and then output, the interface accepts a list of items, rather than an item by itself. After writing out the list, any flushing that may be necessary can be performed before returning from the write method. For example, if writing to a Hibernate DAO, multiple calls to write can be made, one for each item. The writer can then call close on the hibernate Session before returning.

ItemProcessor

Spring Batch provides the ItemProcessor interface:

public interface ItemProcessor<I, O> {O process(I item) throws Exception;
}

An ItemProcessor is very simple; given one object, transform it and return another. The provided object may or may not be of the same type. The point is that business logic may be applied within process, and is completely up to the developer to create. An ItemProcessor can be wired directly into a step, For example, assuming an ItemReader provides a class of type Foo, and it needs to be converted to type Bar before being written out. An ItemProcessor can be written that performs the conversion:

public class Foo {}
public class Bar {public Bar(Foo foo) {}
}
public class FooProcessor implements ItemProcessor<Foo,Bar>{public Bar process(Foo foo) throws Exception {//Perform simple transformation, convert a Foo to a Barreturn new Bar(foo);}
}
public class BarWriter implements ItemWriter<Bar>{public void write(List<? extends Bar> bars) throws Exception {//write bars}
}

In the very simple example above, there is a class Foo, a class Bar, and a class FooProcessor that adheres to the ItemProcessor interface. The transformation is simple, but any type of transformation could be done here. The BarWriter will be used to write out Bar objects, throwing an exception if any other type is provided. Similarly, the FooProcessor will throw an exception if anything but a Foo is provided. The FooProcessor can then be injected into a Step:

<job id="ioSampleJob"><step name="step1"><tasklet><chunk reader="fooReader" processor="fooProcessor" writer="barWriter"commit-interval="2"/></tasklet></step>
</job>

Chaining ItemProcessors

Performing a single transformation is useful in many scenarios, but what if you want to 'chain' together multiple ItemProcessors? This can be accomplished using the composite pattern mentioned previously. To update the previous, single transformation, example, Foo will be transformed to Bar, which will be transformed to Foobar and written out:

public class Foo {}
public class Bar {public Bar(Foo foo) {}
}
public class Foobar{public Foobar(Bar bar) {}
}
public class FooProcessor implements ItemProcessor<Foo,Bar>{public Bar process(Foo foo) throws Exception {//Perform simple transformation, convert a Foo to a Barreturn new Bar(foo);}
}
public class BarProcessor implements ItemProcessor<Bar,FooBar>{public FooBar process(Bar bar) throws Exception {return new Foobar(bar);}
}
public class FoobarWriter implements ItemWriter<FooBar>{public void write(List<? extends FooBar> items) throws Exception {//write items}
}

A FooProcessor and BarProcessor can be 'chained' together to give the resultant Foobar:

CompositeItemProcessor<Foo,Foobar> compositeProcessor =new CompositeItemProcessor<Foo,Foobar>();
List itemProcessors = new ArrayList();
itemProcessors.add(new FooTransformer());
itemProcessors.add(new BarTransformer());
compositeProcessor.setDelegates(itemProcessors);

Just as with the previous example, the composite processor can be configured into the Step:

<job id="ioSampleJob"><step name="step1"><tasklet><chunk reader="fooReader" processor="compositeProcessor" writer="foobarWriter"commit-interval="2"/></tasklet></step>
</job>
<bean id="compositeItemProcessor"class="org.springframework.batch.item.support.CompositeItemProcessor"><property name="delegates"><list><bean class="..FooProcessor" /><bean class="..BarProcessor" /></list></property>
</bean>

========END========

转载于:https://my.oschina.net/xinxingegeya/blog/340130

Spring Batch_ItemReaders and ItemWriters相关推荐

  1. spring batch

    http://www.4ucode.com/Study/Topic/478358 DelimitedLineTokenizer 的delimiter 默认是逗号','.names is as the ...

  2. Spring Batch:多种格式输出编写器

    作为Spring Batch的坚定倡导者,我一直在谈论Spring Batch的概念,它为开发人员提供了一个框架,使他们可以专注于解决业务需求. 这样,它使开发人员不必花费过多的时间来解决所有技术问题 ...

  3. Spring Batch教程–最终指南

    这是Spring批处理教程,它是Spring框架的一部分. Spring Batch提供了可重用的功能,这些功能对于处理大量记录至关重要,包括日志记录/跟踪,事务管理,作业处理统计信息,作业重新启动, ...

  4. 具有Spring Boot和Java配置的Spring Batch教程

    我一直在努力将Podcastpedia.org的一些批处理作业迁移到Spring Batch. 以前,这些工作是以我自己的方式开发的,我认为现在是时候使用一种更"标准化"的方法了. ...

  5. spring boot项目 中止运行 最常用的几种方法

    spring boot项目 中止运行 最常用的几种方法: 1. 调用接口,停止应用上下文 @RestController public class ShutdownController impleme ...

  6. html+spring boot简单的ajax数据传输实现

    本篇讲解在前后端不分离情况下的html+spring boot的项目数据传输实现 首先,后台我写了三个接口 package com.demo.ajax.controller;import com.de ...

  7. Spring Boot整合Spring Data JPA操作数据

    一. Sping Data JPA 简介 Spring Data JPA 是 Spring 基于 ORM 框架.JPA 规范的基础上封装的一套 JPA 应用框架,底层使用了 Hibernate 的 J ...

  8. Spring AOP + Redis解决重复提交的问题

    Spring AOP + Redis解决重复提交的问题 用户在点击操作的时候,可能会连续点击多次,虽然前端可以通过设置按钮的disable的属性来控制按钮不可连续点击,但是如果别人拿到请求进行模拟,依 ...

  9. Spring cloud 微服务docker容器化最佳实践

    Spring cloud 是当下最炙手可热的微服务套件,我们将介绍如何整合Docker容器达到高效快捷的构建发布 采用了dockerfile-maven-plugin插件发布镜像到远程docker主机 ...

最新文章

  1. 计算机二级题31套资料,计算机等级考试:二级VFP机试第31套
  2. Android中常见的MVC模式
  3. 浙江等高等学校计算机,2010年浙江省高等学校计算机等级考试
  4. Docker容器网络解析
  5. Linux的启动流程简析(以Debian为例)
  6. Vmware安装Centos7上网问题的解决
  7. MySQL指定存储引擎命令_MySQL常用指令(2)——存储引擎
  8. js判断是否为ie浏览器
  9. 无法在web 服务器上启动调试。打开的url的iis辅助进程当前没有运行
  10. web测试中如何简单定位bug
  11. 手机浏览器打开微信app的方法
  12. 解决阿里云盾控制台wordpress IP验证不当漏洞
  13. leetcode 39. Combination Sume (medium)
  14. The alias ‘TaskType‘ is already mapped to the value ‘com.xxx.entity.Tasktype‘.
  15. 互联网的成功和端到端原则
  16. 二、数据集与数据类型【R与统计】
  17. 吴恩达深度学习第一课--第二周神经网络基础作业上正反向传播推导
  18. nginx的入门使用(搭建本地的网站服务)
  19. 爬取电影资源之网页爬取篇(python)
  20. 【源码】iOS指纹解锁Touch ID的开发

热门文章

  1. 使用MapReduce实现join操作
  2. POJ 1014: Dividing
  3. 怎样获得listview的第一个item?
  4. 码云创建maven工程
  5. Java Excel 插入图片
  6. 深入了解JavaScript对象(2)--函数、对象
  7. 动态规划(DP),Human Gene Functions
  8. rails设置表单默认值amp;amp;隐藏表单
  9. runtime模型与字典互转
  10. mybatis数据批量插入