前言
集群ES安装完成之后使用dbeaver 连接,发现jdbc驱动总是连接不上,后来发现免费版ElasticSearch没有JDBC许可证。
网上有资料去解决JDBC这问题,参考如下博客:
https://blog.csdn.net/wfs1994/article/details/80421922
https://www.cnblogs.com/reboot51/p/8328720.html
由于项目原因,需要使用JAVA客户端进行连接Es集群,官方提供了两种方式:
- Transport Client
- RESTful Client
第一种是直接连接Es集群服务器(未来可能去掉),后面一种是通过Es提供的Restful API进行间接调用。
according to one of their developers, Elasticsearch plans to get rid of the Transport Client in the future
Transport Client
等待更新。。。
RESTful Client
Java REST客户端分为两种类型:高级客户端以及低级客户端。高级客户端是基于底层客户端,然后公开API特定的方法,处理请求编组和响应反编组。
maven依赖
添加三个依赖
- org.elasticsearch.client.elasticsearch-rest-high-level-client
- org.elasticsearch.client:elasticsearch-rest-client
- org.elasticsearch:elasticsearch
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.3.1</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.3.1</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.3.1</version>
</dependency>
说明一下,按照官方文档说的其实只需要加入一个依赖,与之相关的其他依赖自动会被引入,但是我看引入依赖版本为6.4.3,于是我手动引入高版本的(正常来讲不应该这么做,因为我实际操作中文档有点问题)。

初始化
RestHighLevelClient实例需要低级客户端构建器来构建,如下所示:
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
//Es连接信息,我用的是伪集群哈哈哈
new HttpHost("localhost", 9200, "http"),
new HttpHost("localhost", 9201, "http"),
new HttpHost("localhost", 9202, "http")
)
);
//do something
//记得关闭,不然进程会等待
client.close();
Index API
打开官方文档,然后根据官方文档进行编码。
- 实例化请求客户端RestHighLevelClient
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost", 9202, "http")
)
);
- 构造索引请求对象IndexRequest
IndexRequest request = new IndexRequest("posts");
request.id("1");
String jsonString = "{" +
"\"user\":\"kimchy\"," +
"\"postDate\":\"2013-01-30\"," +
"\"message\":\"trying out Elasticsearch\"" +
"}";
request.source(jsonString, XContentType.JSON);
- 客户端执行索引请求
IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
客户端调用client.index()进行远程访问,这个方法是同步执行,会堵塞。如果要想异步执行的话,官方也提供了基于回调的异步方法client.indexAsync(),
client.indexAsync(request, RequestOptions.DEFAULT, listener);
异步方法不会阻塞并立即返回,一旦执行完成,会触发监听器ActionListener进行回调,onResponse和onFailure分别处理正常以及异常情况。该监听器需要自己实现,例如:
listener = new ActionListener<IndexResponse>() {
@Override
public void onResponse(IndexResponse indexResponse) {
}
@Override
public void onFailure(Exception e) {
}
};
完整代码如下:
public static void saveOrUpdate(Book book) {
IndexRequest request = new IndexRequest("terry").id("book");
String jsonStr = JSON.toJSONString(book);
request.source(jsonStr, XContentType.JSON);
IndexResponse indexResponse = null;
try(
RestHighLevelClient client = getInstance()
) {
indexResponse = client.index(request,RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
if (indexResponse!=null){
String index = indexResponse.getIndex();
String id = indexResponse.getId();
if (indexResponse.getResult() == DocWriteResponse.Result.CREATED) {
System.out.println("_index:"+index+" document:"+id +" created successfully!");
}else if (indexResponse.getResult() == DocWriteResponse.Result.UPDATED){
System.out.println("_index:"+index+" document:"+id +" updated successfully!");
}else {
System.out.println("不明操作类型:" + indexResponse.getResult());
}
}
}
private static RestHighLevelClient getInstance() {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("10.231.134.190, "9202", "http")
)
);
return client;
}
- 测试
浏览器键入:http://10.231.134.190:9202/terry/_doc/book

- 小结
- 从上面测试可以看出,Es的API其实是有规可寻的,对比构造索引请求
IndexRequest("terry").id("book"),索引和文档ID已经在url中得到体现,形如:
http://<ip>:<port>/<index>/_doc/<id> _doc没有参数吗?答案是有的,在7.0之前对应是type配置项,不过7.0之后已经不再是必须配置项,默认为doc。
type将在Elasticsearch 7.0.0中的API中弃用,并在8.0.0中完全删除。
- index-vs-type 原文: https://www.elastic.co/cn/blog/index-vs-type
后记
- 个人理解,按照官方提供的API进行封装对接,上手不难。
- 如果使用
SpringBoot来集成ElasticSearch,可以参考文档
161

被折叠的 条评论
为什么被折叠?



