java - 如何在 Spring-Batch 中使用 ItemReader 跳过行?

标签 java spring spring-batch

我有一个自定义项目阅读器,可以将行从文本文件转换为我的实体:

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() 方法中return null 时,不仅跳过了此项,而且阅读完全终止,并且所有将跳过更多行。因为 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 跳过行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27228809/

相关文章:

java - 从用户名outlook java获取电子邮件地址

java - Spring ClassPathXmlApplicationContext 在 spring conf 文件中创建所有 bean 的空实例?

java - Spring 批处理 : MultiResourceItemReader with custom ItemReader reads the same file ad infinitum

java - 使用 Spring Batch 处理文件顺序

java - 尝试在 jpa 表上创建新条目时出错

java - 在插入多对多表之前, hibernate 选择多个列

java - 使用 Scene Builder 创建 JavaFX TreeView

java - 图片直接从客户端上传到远程服务器? Spring / Tomcat

java - 是否可以使用 Spring Session 和 Spring Boot 将 session 存储在两个不同的商店中?

java - spring batch 中来自 oracle 数据库的结果集的分区器