jest for elasticsearch

*elasticsearch(後面簡稱es)git

  • 背景: 目前項目應用中對es的操做用的是http(本身封裝)的一套方法;有些數據處理起來仍是須要定製開發處理,不是很方便。正好須要對本項目從新進行改造,因而採用第三方工具包jest 對es的操做進行從新的梳理改造。github

  • why use jest
    官方有個大體的介紹:Jest is a Java HTTP Rest client for ElasticSearch.It is actively developed and tested by Searchly.elasticsearch

    jset優點
    1)提供Restful API, 原生ES API不具有;
    2)Jest支持不一樣版本的es基本操做 主要是http rest client;maven

  • maven 管理項目:工具

    <dependency>
      <groupId>io.searchbox</groupId>
      <artifactId>jest</artifactId>
      <version>2.0.3</version>
    </dependency>
  • jest使用post

    image

    網上有不少(上述圖片中)的方法實例,下面就不具體的介紹了(連接以下)
    http://blog.csdn.net/u010466329/article/details/75020956
    https://github.com/searchbox-io/Jest測試

    結合源代碼封裝了一個能夠直接傳遞url的method,該方法的實現就是結合了jest 源碼,實現了本身想要的接口(用於用戶頁面自定義查詢 更方便)。 代碼以下:ui

    AbstractLocalHttpAction (本地HttpAction)this

    /**
        * methodName "http" "delete" "put" "get"  「post」
        * queryParam 參數
        * url 請求連接
        */
        public abstract class AbstractLocalHttpAction <T extends JestResult> extends AbstractAction<T>  {
    
        protected String methodName;
    
        private String queryParam;
    
        private String url;
    
        public AbstractLocalHttpAction(AbstractLocalHttpAction.Builder builder) {
            super(builder);
            this.queryParam = builder.queryParam;
            this.methodName = builder.methodName;
            this.url = builder.url;
        }
    
        public String getMethodName() {
            return this.methodName;
        }
    
        public String getQueryParam() {
            return this.queryParam;
        }
    
        public String getUrl() {
            return this.url;
        }
    
    
        protected String buildURI() {
            StringBuilder sb = new StringBuilder(super.buildURI());
    
            return sb.toString();
        }
    
        protected abstract static class Builder<T extends AbstractLocalHttpAction, K> extends io.searchbox.action.AbstractAction.Builder<T, K> {
            private String url;
            private String methodName;
            private String queryParam;
    
            protected Builder() {
            }
    
            public K url(String url) {
                this.url = url;
                return (K)this;
            }
    
            public K methodName(String methodName) {
                this.methodName = methodName;
                return (K)this;
            }
    
            public K queryParam(String queryParam) {
                this.queryParam = queryParam;
                return (K)this;
            }
    
        }
    }

LocalResultAbstractActionurl

public abstract class LocalResultAbstractAction extends AbstractLocalHttpAction<JestResult> {
    
        public LocalResultAbstractAction(Builder builder) {
            super(builder);
        }
    
        public JestResult createNewElasticSearchResult(String responseBody, int statusCode, String reasonPhrase, Gson gson) {
            return (JestResult)this.createNewElasticSearchResult(new JestResult(gson), responseBody, statusCode, reasonPhrase, gson);
        }
    }

JestLocalHttpClient(用法build入口)

public class JestLocalHttpClient extends LocalResultAbstractAction {

    private String query;

    protected JestLocalHttpClient(JestLocalHttpClient.Builder builder) {
        super(builder);
        this.setURI(this.buildURI()+""+getUrl());
        this.query = getQueryParam();
    }

    public String getRestMethodName() {
        return getMethodName();
    }

    public String getData(Gson gson) {
        String data  = this.query;
        return data;
    }
    public static class Builder extends AbstractLocalHttpAction.Builder<JestLocalHttpClient, JestLocalHttpClient.Builder> {

        public Builder(String url, String methodName,String queryParam) {
            this.url(url);
            this.methodName(methodName);
            this.queryParam(queryParam);
        }
        public JestLocalHttpClient build() {
            return new JestLocalHttpClient(this);
        }
    }

}

jestManager 封裝以下

public class JestManager {

    private static final Logger LOGGER = LoggerFactory.getLogger(JestManager.class);
    /**
     * 獲取JestClient對象
     * @return
     */
    public static JestClient getJestClient(String clustName) {
        JestClientFactory factory = new JestClientFactory();
        Cluster cluster = CLUSTERMAP.get(clusterName);
        try {
           notes.add("http://"+cluster.getHost()+":"+cluster.getHttprt());
                HttpClientConfig.Builder httpClientConfig = new HttpClientConfig
                        .Builder(notes)
                        .connTimeout(1500)
                        .readTimeout(3000)
                        .multiThreaded(true);
             factory.setHttpClientConfig(httpClientConfig.build());
        }catch (Exception e){
            e.printStackTrace();
            LOGGER.error("初始化jestclient實例失敗:"+e.getMessage());
        }
        return  factory.getObject();
    }
}

上面本身的封裝方法調用以下

public JestResult httpProxy(String clustName, String url, String methodName, String queryParam) {
        JestResult result = null ;
        try {
            JestLocalHttpClient jestLocalHttpClient = new JestLocalHttpClient.Builder(url,methodName,queryParam).build();
            result = JestManager.getJestClient(clustName).execute(jestLocalHttpClient);
        } catch (IOException e) {
            e.printStackTrace();
            LOGGER.error("jestLocalHttpClient失敗:"+e.getStackTrace());
        }
        return result ;
    }

測試以下

url:/demo/_search?pretty
method:get
queryParam:null

調用接口:返回信息
"{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 0,
    "max_score" : null,
    "hits" : [ ]
  }
}
"

jest 用起來很方便,方法的封裝讓咱們代碼寫起來更爲簡單。以上的內容但願能對你們有所幫助。

相關文章
相關標籤/搜索