How to skip lines with ItemReader in Spring-Batch?

后端 未结 5 849
陌清茗
陌清茗 2021-01-16 15:39

I have a custom item reader that transforms lines from a textfile to my entity:

public class EntityItemReader extends AbstractItemStreamItemReader

        
5条回答
  •  [愿得一人]
    2021-01-16 16:28

    I think the good practice to filter some lines is to use not the reader but a processor (in which you can return null when you want to filter the line).

    Please see 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.

提交回复
热议问题