java Elasticsearch Scroll 游标 分页查询

package com.smk.es.servicce;

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;

public class TestEs {


    private String clusterName ="es-smk-sit";

    private String clusterNode = "192.168.23.10";

    private String clusterPort ="9301";

    private String poolSize = "10";

    private boolean snf = true;

    private String index = "smk-label";

    private String type = "label";

    public TransportClient transportClient() {
        TransportClient transportClient = null;
        try {
            Settings esSetting = Settings.builder()
                    .put("cluster.name", clusterName) //集群名字
                    .put("client.transport.sniff", snf)//增加嗅探机制,找到ES集群
                    .put("thread_pool.search.size", Integer.parseInt(poolSize))//增加线程池个数,暂时设为5
                    .build();
            //配置信息Settings自定义
            transportClient = new PreBuiltTransportClient(esSetting);
            TransportAddress transportAddress = new TransportAddress(InetAddress.getByName(clusterNode), Integer.valueOf(clusterPort));
            transportClient.addTransportAddresses(transportAddress);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("elasticsearch TransportClient create error!!");
        }
        System.out.println("es客户端创建成功");
        return transportClient;
    }

    public static  String scrollId = "";

    /**
     * 第一次查询的方式
     * @param client
     * @return
     */
    private Map<String,Object> my(TransportClient client){
        BoolQueryBuilder mustQuery = QueryBuilders.boolQuery();
        //设置查询条件
        mustQuery.must(QueryBuilders.matchQuery("sex","男"));
        mustQuery.must(QueryBuilders.matchQuery("city","杭州市"));
        SearchResponse rep =  client.prepareSearch()
                .setIndices(index) // 索引
                .setTypes(type)  //类型
                .setQuery(mustQuery)
                .setScroll(TimeValue.timeValueMinutes(2))  //设置游标有效期
                .setSize(100)  //每页的大小
                .execute()
                .actionGet();
        Map<String,Object> m = new HashMap<String,Object>();
        m.put("scrollId",rep.getScrollId());//获取返回的 游标值
        m.put("id",  (rep.getHits().getHits())[0].getId());
        return m;
    }


    private  Map<String,Object>  my2(String scrollId,TransportClient client){
            SearchResponse rep1 = client.prepareSearchScroll(scrollId)  //设置游标
                    .setScroll(TimeValue.timeValueMinutes(2))  //设置游标有效期
                    .execute()
                    .actionGet();
        Map<String,Object> m = new HashMap<String,Object>();
        m.put("scrollId",rep1.getScrollId());
        SearchHit[] s = rep1.getHits().getHits();
        if(s == null || s.length == 0){
            return null;
        }
        m.put("id",  (rep1.getHits().getHits())[0].getId());
        return m;
    }


    public static void main(String[] args) {
        TestEs t =  new TestEs();
        TransportClient client =  t.transportClient();
        Map<String,Object> m1 = t.my(client);
        System.out.println("first:"+m1.get("id"));
        String s = m1.get("scrollId").toString();
        System.out.println("first:"+s);


        int i = 0;
        while (true){
            i++;
            Map<String,Object> m2 = t.my2(s,client);
            // 查询不到数据了,就表示查询完了
            if(m2 == null){
                break;
            }
            System.out.println("insert  to mysql");
        }
        System.out.println("总次数:"+i);
        System.out.println("end");
    }

}

在这里插入图片描述

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/walle167/article/details/85165549