2015-05-06 68 views
0

在spring-batch中,是否支持Elasticsearch ItemReader,使用掃描和滾動功能?我確實看到this擴展名,但它基於普通的彈簧數據搜索查詢。因爲批量作業大多需要處理大量的數據,所以基於掃描和滾動功能會很高興。謝謝。Elasticsearch掃描和滾動的spring-batch ItemReader

+0

也許是有幫助的使用Cunk處理:HTTP://docs.spring.io/spring-batch/trunk/reference /html/configureStep.html#chunkOrientedProcessing –

+0

Spring的任何人都有一些建議嗎? – heyu

回答

0

儘管ElasticSearch沒有「原生」ItemReader實現,但Spring Batch的確提供了一個包含Spring Data PagingAndSortingRepositoryRepositoryItemReader。有了這個,您可以使用Spring Data ElasticSearch項目提供的ElasticSearch存儲庫定義。

您可以在這裏閱讀更多關於RepositoryItemReader的Spring批處理文檔:http://docs.spring.io/spring-batch/trunk/apidocs/org/springframework/batch/item/data/RepositoryItemReader.html

你可以閱讀更多有關Spring數據ElasticSearch項目位置:http://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/

0
import java.util.Iterator; 

import org.springframework.batch.item.data.AbstractPaginatedDataItemReader; 
import org.springframework.data.elasticsearch.core.ElasticsearchOperations; 
import org.springframework.data.elasticsearch.core.query.SearchQuery; 

public class ElasticsearchItemReader<T> extends AbstractPaginatedDataItemReader<T> { 
    private ElasticsearchOperations elasticsearchOperations; 
    private final SearchQuery searchQuery; 
    private String scrollId; 
    private int scrollTimeinMillis = 60000; 
    private Class<T> type; 

    public ElasticsearchItemReader(
     final ElasticsearchOperations elasticsearchOperations, 
     final SearchQuery searchQuery, 
     final Class<T> type 
    ) { 
     this.elasticsearchOperations = elasticsearchOperations; 
     this.searchQuery = searchQuery; 
     this.type = type; 
    } 

    @Override 
    protected void doOpen() throws Exception { 
     scrollId = elasticsearchOperations.scan(searchQuery, scrollTimeinMillis, false); 
    } 

    @Override 
    protected Iterator<T> doPageRead() { 
     return elasticsearchOperations.scroll(scrollId, scrollTimeinMillis, type).iterator(); 
    } 
}