6.1 ItemReader

Spring Batch 官方文檔翻譯, 6.1 ItemReader 

 

 

 

Although a simple concept, 

an ItemReader is the means for providing data from many different types of input. 

儘管這是一個簡單的概念

一個 ItemReader 是一種讀取從不同形式輸入源數據的方法

 

The most general examples include:

* 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).

最常見的例子包括 Flat File ItemReader

從純文本中讀取一行一行的數據

純文本文件有固定格式的欄位去潤飾數據

或是使用一些特殊字符 ( 比如逗號 ) 分隔紀錄中各個字段

 

* 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.

XML ItemReaders 獨立執行 XML 包括用於解析, 映射, 驗證物件的技術

可針對用來輸入資料的 XML file 用 XSD schema 驗證 

 

* 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.

一個 database 資源被存取, 

返回 resultsets, 而 resultsets 可以被映射成要處理的物件

預設的 SQL ItemREaders 調用一個 RowMapper 傳回物件

並保持跟蹤紀錄當前行 ( row ) , 以防需要重啟

儲存基本訊息, 並提供一些事物增強特性 ( 事務增強特性之後會提到

 

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 列表參照 附錄A ( Appendix A 點下去

 

ItemReader is a basic interface for generic input operations:

ItemReader 是一個通用的輸入操作的基本介面

 

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. 

read 是 ItemReader 最不可或缺的方法

呼叫它時會回傳一個 Item 或是 null ( 如果沒有更多 item  )

一個 item 可能代表一行或是一個檔案, 在資料庫中的一排或是在 XML 檔裡的一個元素

 

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.

一般來說,

這些 item 都可被映射為一個 domain 物件 ( 比如 Trade, Foo... )

但不是強制要求

 

It is expected that implementations of the ItemReader interface will be forward only. 

一般來說 實作 ItemReader 介面都要是只向前行的 ( forward only, 比如 resultset , cursor 就一直指下去指到完 )

 

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. 

然而

如果底層資源是事物性的 ( 比如 JMS 佇列 )

那可能發生 rollback

呼叫 read 可能返回其他次呼叫中同樣邏輯的 item 

 

It is also worth noting that a lack of items to process 

by an ItemReader will not cause an exception to be thrown. 

如果處理過程沒有 items

ItemReader 不會拋出任何異常

 

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.

比如

一個資料庫 ItemReader 被配置了一個查詢語句返回結果數為 0 

那麼第一次呼叫 read 會回傳 0 

 

參考資料:

https://docs.spring.io/spring-batch/trunk/reference/html/readersAndWriters.html

https://kimmking.gitbooks.io/springbatchreference/06_ItemReaders_ItemWriters/63.html