All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.lx.boot.es.ElasticLowerClient Maven / Gradle / Ivy

Go to download

使用文档: https://a7fi97h1rc.feishu.cn/docx/X3LRdtLhkoXQ8hxgXDQc2CLOnEg?from=from_copylink

There is a newer version: 1.1
Show newest version
package com.lx.boot.es;

import com.lx.boot.log.entity.RunLogMessage;
import com.lx.util.LX;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.*;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ElasticLowerClient {
    private  org.slf4j.Logger logger= LoggerFactory.getLogger(ElasticLowerClient.class);
    private RestClient client;

    private final static Map MAPS = new HashMap();

    public static ElasticLowerClient getInstance(String hosts, String userName, String passWord) {
        LX.exObj(hosts,"没有传入地址");
        String key = hosts+userName+passWord;
        if (!MAPS.containsKey(key)) {
            synchronized (ElasticLowerClient.class) {
                if (!MAPS.containsKey(key)) {
                    MAPS.put(key, new ElasticLowerClient(hosts,userName,passWord));
                }
            }
        }
        return MAPS.get(key);
    }
    public ElasticLowerClient(String hosts, String userName, String passWord) {

        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, passWord));
        String[] hostsAndPorts=hosts.split(",");
        HttpHost[] httpHosts = new HttpHost[hostsAndPorts.length];
        for(int i=0;i list, String baseIndex) throws IOException {
        StringBuffer sendStr=new StringBuffer();
        for(int a=0;a reindexs= getExistIndices(indexs);
        String indexStr=String.join(",",reindexs);
        if("".equals(indexStr)){
            return "";
        }
        String reStr="";
        Request request = new Request("POST", indexStr+"/_search");
        request.setJsonEntity(queryStr);
        try {
            Response res=client.performRequest(request);

            InputStream inputStream=res.getEntity().getContent();
            byte[] bytes = new byte[0];
            bytes = new byte[inputStream.available()];
            inputStream.read(bytes);
            String str = new String(bytes);
            reStr=str;
        } catch (Exception e) {
            reStr = "";
        }
        return reStr;
    }

    //查询index列表
    public String cat(String index){
        String reStr="";
        Request request = new Request("GET", "/_cat/indices/"+index+"?v");
        try {
            Response res=client.performRequest(request);

            InputStream inputStream=res.getEntity().getContent();
            byte[] bytes = new byte[0];
            bytes = new byte[inputStream.available()];
            inputStream.read(bytes);
            String str = new String(bytes);
            reStr=str;
        } catch (Exception e) {
            reStr = "";
        }
        return reStr;
    }
    public boolean deleteIndex(String index){
        try {
            Request request = new Request("DELETE", "/" + index + "");
            Response res = client.performRequest(request);
            if (res.getStatusLine().getStatusCode() == 200) {
                return true;
            }
        }catch (Exception e){
            return false;
        }
        return false;
    }
    public void close(){
        try {
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public List getExistIndices(String [] indices){
        List existIndexList = new ArrayList();
        for (String index: indices){
            try {
                Request request = new Request("HEAD", "/"+index+"");
                Response res=client.performRequest(request);
                if(res.getStatusLine().getStatusCode()==200){
                    existIndexList.add(index);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return existIndexList;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy