Elasticsearch做爲一個成熟的開源框架,對主流的多種客戶端語言都支持,好比Java,JavaScript ,PHP,.Net,Python,Ruby,CURL固然還有一些小衆的語言,雖然es官網沒支持,可是我的開發者也有一些開源的,具體的可在es官網clients地址查看:html
https://www.elastic.co/guide/en/elasticsearch/client/index.htmljava
開發過程當中,基本最經常使用的就是Java和curl的方式了,由於es自己就是使用java語言開發的,因此對Java的支持應該是最到位了,此外es也支持rest ful的DSL的訪問方式,咱們能夠在linux上輕鬆的使用curl命令來對es進行增刪改查,curl的操做方式大多數都是臨時的,實際開發的咱們仍是用編程語言來訪問的:linux
es支持Java API的訪問方式,支持很是全面,惟一的缺點就是依賴有點多,代碼稍臃腫,有時候咱們想簡單的開發一個很是小的功能,又不想使用java笨重的客戶端方式,應該怎麼辦?apache
上文說到es支持rest的訪問方式,那麼咱們徹底可使用httpclient或者jsoup來直接發送http請求不就好了嗎?實際上是能夠的,使用httpclient和jsoup來發送curl的命令也能操做es,獲取結果。這裏面有一個須要注意的地方。編程
httpclient和jsoup都不直接支持發送DELETE方法帶參數的請求,這一點是個小缺陷,若是這個不支持,意味着一些刪除操做好比delete by query可能支持不太好。jsoup是徹底不支持,而在httpclient裏面咱們能夠經過繼承重寫HttpEntityEnclosingRequestBase來知足,刪除請求帶參數體,下面來看下如何使用:json
繼承重寫的代碼:api
package tools; import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; import java.net.URI; public class MyHttpDelete extends HttpEntityEnclosingRequestBase { // public static final String METHOD_NAME = "DELETE"; // public String getMethod() { return METHOD_NAME; } // public MyHttpDelete(final String uri) { super(); setURI(URI.create(uri)); } // public MyHttpDelete(final URI uri) { super(); setURI(uri); } // public MyHttpDelete() { super(); } }
而後使用httpclient發送一個刪除請求:多線程
//實例化http,刪除id=1001的一條數據 val client = HttpClients.createDefault() val httpdelete = new MyHttpDelete("http://localhost:9200/test_index/logs/_query") val s = new StringEntity("{ \"query\": { \"query_string\": { \"query\": id:1001 " } }} ",ContentType.APPLICATION_JSON) httpdelete.setEntity(s) val rs=client.execute(httpdelete)//執行刪除 //解析響應結果 val json = EntityUtils.toString(rs.getEntity(), "UTF-8") //釋放資源 client.close()
httpclient仍是比較強大的,可是上面的代碼仍是有點複雜,並且涉及鏈接的地方是有問題的,咱們都知道es通常都是多臺機器組成集羣,而使用原生的httpclient請求創建的連接只能是某一臺機器的ip這樣一來,若是這臺機器掛掉這個客戶端程序就徹底不能使用了,因此風險仍是比較大的,不過也不用擔憂,es官網也提供了ES Java RestClient的方式來訪問es,這個庫底層用的也是httpclient的組件,只不過es官網作了封裝,支持多機器ip,以及對請求方法作了簡化,因此想減小項目的依賴,又對支持功能要求比較健壯,咱們就可使用這個庫來開發咱們的業務。框架
官網文檔地址:curl
https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/index.html
maven依賴:
<dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>rest</artifactId> <version>5.5.1</version> </dependency>
下面來看下,如何使用ES的RestClient來操做ES:
//初始化RestClient實例 static RestClient restClient = RestClient.builder( new HttpHost("192.168.10.5", 9200, "http"), new HttpHost("192.168.10.6", 9200, "http"), new HttpHost("192.168.10.7", 9200, "http")).build() // (1) 執行一個基本的方法,驗證es集羣是否搭建成功 Response response = restClient.performRequest("GET", "/", Collections.singletonMap("pretty", "true")); System.out.println(EntityUtils.toString(response.getEntity())); //輸出結果: { "name" : "nd2", "cluster_name" : "search", "version" : { "number" : "2.3.4", "build_hash" : "e455fd0c13dceca8dbbdbb1665d068ae55dabe3f", "build_timestamp" : "2016-06-30T11:24:31Z", "build_snapshot" : false, "lucene_version" : "5.5.0" }, "tagline" : "You Know, for Search" } // (2)驗證es的某個索引是否存在 Response response = restClient.performRequest("HEAD","/product/pdt",Collections.<String, String>emptyMap()); System.out.println(response.getStatusLine().getReasonPhrase().equals("OK")); //輸出結果: true // (3) 刪除某個索引的指定條件的數據 Map<String, String> paramMap = new HashMap<String, String>(); paramMap.put("q", "id:"+id); paramMap.put("pretty", "true"); Response response = restClient.performRequest("DELETE", + "product/pdt/_query", paramMap); System.out.println(EntityUtils.toString(response.getEntity())); //輸出結果: { "took" : 0, "timed_out" : false, "_indices" : { "_all" : { "found" : 1, "deleted" : 0, "missing" : 0, "failed" : 0 } }, "failures" : [ ] }
總結:
ES官網提供的RestClient還支持nio實現的異步非阻塞的方式多線程多送請求,經過回調函數來處理響應的結果,固然了權限認證,超時中斷,失敗重試,線程數都有對應的設置選項,感興趣的朋友可自行查閱嘗試。
經過對比咱們發現官網封裝的RestClient使用起來更加簡單和健壯,比起來沒有包裝的httpclient原生的方式更加符合生產環境的標準,對於不想熟悉標準的java api的es操做方法,又想經過java來訪問es的朋友們,RestClient是一個不錯的選擇。