Elasticsearch-RestHighLevelClient的使用

Elasticsearch-RestHighLevelClient的使用

1 概述

RestHighLevelClient设计在之前的Rest Client(现在称为Java Low Level REST Client)之上,主要为了替换之前的TransportClient。

相较于TransportClient,RestHighLevelClient最优之处在于可以兼容当前版本及以后版本的ES Cluster,而TransportClient却必须和ES Cluster版本一直否则报错。而且RestHighLevelClient和TransportClient API很类似,包括参数和返回数据,比较容易迁移。TransportClient将在7中deprecated,8中删除。

对比之前的Java Low Level REST Client,RestHighLevelClient在请求编组和响应解组上下了功夫。RestHighLevelClient将各种请求实现都进行了抽象,只需传入各类Request对象参数,返回值也是Response对象,不需要再像Low Level REST Client那样去指定URL、解析Response等。

本文只会设计一些官网上没有说清的地方或没有例子的代码,其他使用还请参考官网。

官方文档可见Java High Level REST Clientedit

2 简单例子

RestHighLevelClient client = new RestHighLevelClient(
        RestClient.builder(
                new HttpHost("localhost", 9200, "http"),
                new HttpHost("localhost", 9201, "http")));

3 带Sniffer例子

关于Sniffer可参考Sniffer

之前我们初始化TransportClient客户端的时候需要用Settings指定client.transport.sniff来发现集群中除配置外的其他节点,现在使用RestHighLevelClient也可以设定Sniffer,不过要麻烦一些,例子如下:

HttpHost[] httpHosts = new HttpHost[3];
httpHosts[0] = new HttpHost("192.168.1.1", 9200, "http");
httpHosts[1] = new HttpHost("192.168.1.2", 9200, "http");
httpHosts[2] = new HttpHost("192.168.1.3", 9200, "http");

// create RestClientBuilder with httpHosts
RestClientBuilder restClientBuilder = RestClient.builder(httpHosts);

// set a SniffOnFailureListener to restClientBuilder
SniffOnFailureListener sniffOnFailureListener = new SniffOnFailureListener();
restClientBuilder.setFailureListener(sniffOnFailureListener);

// create RestHighLevelClient
client = new RestHighLevelClient(restClientBuilder);

// configure the node sniffer(optional)
NodesSniffer nodesSniffer = new ElasticsearchNodesSniffer(
        client.getLowLevelClient(),
        // 自定义sniff节点超时时间,超过的就不等了,默认1秒,这里设为5秒
        TimeUnit.SECONDS.toMillis(5),
        // 协议,默认HTTP
        ElasticsearchNodesSniffer.Scheme.HTTP);

// build the sniffer
Sniffer sniffer = Sniffer
        .builder(client.getLowLevelClient())
        .setNodesSniffer(nodesSniffer)
        // 默认每5分钟获取一次集群中的data node,这里改为1分钟
        .setSniffIntervalMillis(60000)
        // sniff failure后额外触发sniff行为的延迟时间,默认1分钟,这里改为30秒
        .setSniffAfterFailureDelayMillis(30000)
        .build();

// connect the sniffer and the listener
sniffOnFailureListener.setSniffer(sniffer);

// finally, remember to close sniffer and client.
// must close sniffer right before the client
sniffer.close();
client.close();

4 RestHighLevelClient的确线程安全

根据ES官网文档所述,RestHighLevelClient的确线程安全,经典使用方法是在应用程序启动时或在首个请求发起时初始化一个单例对象。

具体来说,RestHighLevelClient底层维护了HTTP连接池,如果每次都创建和销毁的话代价很高。

记得用完之后必须调用RestHighLevelClient.close()方法。

官网关于线程安全出处:点这里
Elasticsearch-RestHighLevelClient的使用_第1张图片

5 Rollover Index

可自动滚动Index。

详见Elasticsearch学习-Rollover Index

参考文档

  • Changing the client’s initialization code
  • ElasticSearch Java High level REST Client - Singleton or one instance per user?
  • Should BulkProcessor And RestHighLevelClient be shared in multi-thread?
  • Elasticsearch Java High Level REST Client Establish A Bunch of TCP Connection and Don’t Close that Connections After Putting Data

你可能感兴趣的:(elasticsearch,Elasticsearch)