预备知识
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
org.elasticsearch.client
transport
7.5.2
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.prepareGet("customer", "_doc", "1").get();
System.out.println(response);
// on shutdown
client.close();
}
}
Java Low Level Rest Client
Demo
org.elasticsearch.client
elasticsearch-rest-client
7.5.2
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
org.elasticsearch.client
elasticsearch-rest-high-level-client
7.5.2
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的优点,推荐使用
原文:https://www.cnblogs.com/zby9527/p/13221377.html