6.2 ItemWriter

Spring Batch 官方文檔 6.2 ItemWriter

ItemWriter is similar in functionality to an ItemReader, 

but with inverse operations. 

ItemWriter 跟 ItemReader 有相似的功能

但是相反

 

Resources still need to be located, 

opened and closed but they differ in that anItemWriter writes out, rather than reading in. 

資源仍然需要被定位, 開啟, 關閉

但差在 ItemWriter 是寫入操作, 不是讀取

 

In the case of databases or queues these may be inserts, 

updates, or sends. 

在使用資料庫或是佇列的情況

ItemWriter 對應的是 插入, 上傳, 發送

 

The format of the serialization of the output is specific to each batch job.

序列化輸出的格式依每個批次處理作業而定

 

As with ItemReader, 

ItemWriter is a fairly generic interface:

跟 Item Reader 一樣

ItemWriter 也是一個通用的介面

 

public interface ItemWriter<T> {

 

    void write(List<? extends T> items) throws Exception;

}

 

As with read on ItemReader, 

write provides the basic contract of ItemWriter; 

it will attempt to write out the list of items passed in as long as it is open. 

跟 ItemReader 的 read 一樣

write 提供 ItemWriter 的根本方法

只要傳入的 item 列表是打開的, 它就會嘗試將其寫入

 

Because it is generally expected 

that items will be 'batched' together into a chunk and then output, 

the interface accepts a list of items, 

rather than an item by itself. 

因為一般來說

items 會被批量寫入到一起然後輸出

介面接受一個 list 的 items

而不是單一 item

 

After writing out the list, 

any flushing that may be necessary can be performed before returning from the write method. 

list 被輸出後

在 write 返回之前, 對緩衝執行 flush 是必要的

 

For example, 

if writing to a Hibernate DAO, 

multiple calls to write can be made, one for each item. 

The writer can then call close on the hibernate Session before returning.

比如說

如果寫入到 Hibernate DAO 

對每個 item 都要進行一次 write

writer 會在返回前關閉跟 hibernate 的會話 ( session )

 

參考文件:

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

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