用于Elasticsearch扫描和滚动的spring-batch ItemReader

时间:2015-05-06 22:53:11

标签: spring-batch spring-data-elasticsearch

在spring-batch中,是否支持Elasticsearch ItemReader,使用扫描和滚动功能?我确实看到this扩展名,但这是基于普通的spring-data搜索查询。基于扫描和滚动功能可以很好,因为批处理作业大多需要处理大批量数据。感谢。

2 个答案:

答案 0 :(得分:0)

虽然没有" native"对于ElasticSearch的{​​{1}}实现,Spring Batch确实提供了一个包含Spring Data ItemReader的{​​{1}}。有了这个,您可以使用Spring Data ElasticSearch项目提供的ElasticSearch的存储库定义。

您可以在此处阅读有关Spring Batch文档中RepositoryItemReader的更多信息:http://docs.spring.io/spring-batch/trunk/apidocs/org/springframework/batch/item/data/RepositoryItemReader.html

您可以在此处阅读有关Spring Data ElasticSearch项目的更多信息:http://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/

答案 1 :(得分: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();
   }
}