java-如何在Spring-Batch中使用ItemReader跳过行?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-如何在Spring-Batch中使用ItemReader跳过行?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1903字,纯文字阅读大概需要3分钟。
内容图文
![java-如何在Spring-Batch中使用ItemReader跳过行?](/upload/InfoBanner/zyjiaocheng/696/cc6197addd124d1bb3c8f9de1984b574.jpg)
我有一个自定义项目阅读器,可将行从文本文件转换为我的实体:
public class EntityItemReader extends AbstractItemStreamItemReader<MyEntity> {
@Override
public MyEntity read() {
String line = delegate.read();
//analyze line and skip by condition
//line.split
//create entity with line values
}
}
这类似于FlatFileItemReader.
然后,已读取的MyEntity将由JdbcItemReader保留到数据库中.
问题:有时我的行包含应跳过的值.
但是,当我只是在阅读器的read()方法内返回null时,不仅将跳过此项目,而且将完全终止读取,并且将跳过所有其他行.因为空元素是所有spring-reader的“信号”,所以要读取的文件已完成.
因此:如果无法返回null,我该怎么办才能按阅读器内部的条件跳过特定行?因为根据读者的本性,我被迫在此处返回一个对象.
解决方法:
我认为过滤某些行的好习惯是不使用阅读器,而是使用处理器(当您要过滤行时,可以在其中返回null).
请参阅http://docs.spring.io/spring-batch/trunk/reference/html/readersAndWriters.html:
6.3.2 Filtering Records
One typical use for an item processor is to filter out records before they are passed to the ItemWriter. Filtering is an action distinct from skipping; skipping indicates that a record is invalid whereas filtering simply indicates that a record should not be written.
For example, consider a batch job that reads a file containing three different types of records: records to insert, records to update, and records to delete. If record deletion is not supported by the system, then we would not want to send any “delete” records to the ItemWriter. But, since these records are not actually bad records, we would want to filter them out, rather than skip. As a result, the ItemWriter would receive only “insert” and “update” records.
To filter a record, one simply returns “null” from the ItemProcessor. The framework will detect that the result is “null” and avoid adding that item to the list of records delivered to the ItemWriter. As usual, an exception thrown from the ItemProcessor will result in a skip.
内容总结
以上是互联网集市为您收集整理的java-如何在Spring-Batch中使用ItemReader跳过行?全部内容,希望文章能够帮你解决java-如何在Spring-Batch中使用ItemReader跳过行?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。