目錄
預備知識
Java客戶端分析
- 分為Java API Client和Java Rest Client
- Java API Client默認連接的是9300端口,傳輸協議,依賴netty
- Java API Client不同版本有兼容問題
- Java API Client通過方法調用完成交互,有豐富的API
- Java Rest Client默認連接的是9200端口,http協議
- Java Rest Client不同版本沒有兼容問題
- Java Rest Client分為High和Low兩種
- Java Low Level Rest Client使用Apache HttpClient進行HTTP調用,簡單封裝了一下,需要自己處理請求和響應,還是面向HTTP請求的,API簡單
- Java High Level Rest Client基於Java Low Level Rest Client封裝,提供了面向方法的API。同時請求參數和響應參數使用了elasticsearch定義的實體,方便從Java API Client遷移。
- Java High Level Rest Client完成elasticsearch請求響應實體轉換為Java Low Level Rest Client的請求響應。即解決了Java API Client兼容問題,又解決了Java Low Level Rest Client封裝使用問題
Java API Client
Demo
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>7.5.2</version>
</dependency>
package com.zby;
import java.io.IOException;
import java.net.InetAddress;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
/**
* @author zby
* @title ElasticSearchApiDemo
* @date 2020年7月1日
* @description
*/
@SuppressWarnings({"deprecation", "resource"})
public class ElasticSearchApiDemo {
public static void main(String[] args) throws IOException {
TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"), 9300));
GetResponse response = client.get(new GetRequest("customer", "1")).actionGet();
System.out.println(response);
client.close();
}
}
Java Low Level Rest Client
Demo
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.5.2</version>
</dependency>
package com.zby;
import java.io.IOException;
import org.apache.http.HttpHost;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
/**
* @author zby
* @title ElasticSearchLowLevelApiDemo
* @date 2020年7月1日
* @description
*/
public class ElasticSearchLowLevelApiDemo {
public static void main(String[] args) throws IOException {
RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200, "http")).build();
Response response = restClient.performRequest(new Request("GET", "/customer/_doc/1"));
System.out.println(EntityUtils.toString(response.getEntity()));
restClient.close();
}
}
- 請求需要自己組裝http請求
- 響應需要自己解析http響應
- 提供了HttpClient對ElasticSearch簡單封裝
Java High Level Rest Client
Demo
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.5.2</version>
</dependency>
package com.zby;
import java.io.IOException;
import org.apache.http.HttpHost;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
/**
* @author zby
* @title ElasticSearchHighLevelApiDemo
* @date 2020年7月1日
* @description
*/
public class ElasticSearchHighLevelApiDemo {
public static void main(String[] args) throws IOException {
RestHighLevelClient client =
new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));
GetResponse getResponse = client.get(new GetRequest("customer", "1"), RequestOptions.DEFAULT);
System.out.println(getResponse);
client.close();
}
}
總結
- Java High Level Rest Client兼顧另外兩種api的優點,推薦使用
- Java High Level Rest Client的請求響應跟Java API Client是一樣的,但是易用性更好