gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 07:00:19 24 4
gpt4 key购买 nike

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

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com