ES-Client

Language independence

  • Java REST Client
  • Java API
  • Python API
  • Go API
  • .Net API
  • PHP API
  • JavaScripts API
  • Ruby API
  • Perl API
  • Eland
  • Rust
  • Community Contributed Clients

Java API
life cycle (years of birth and death: ES 0.9 - ES 7.x)
The name of the client used by the Java API is called TransportClient. Starting from 7.0.0, it is officially not recommended to use TransportClient as the Java client of ES, and from 8.0 will be completely deleted.

Note
TransportClient uses the transport module (9300 port) to remotely connect to the Elasticsearch cluster. The client does not join the cluster, but communicates with them in a polling manner by obtaining a single or multiple transport addresses.
TransportClient uses the transport protocol to communicate with Elasticsearch nodes. If the version of the client is different from the version of the ES instance it communicates with, compatibility issues will arise. The low-level REST uses the HTTP protocol, which can communicate with any version of the ES cluster. High-level REST is based on low-level REST.
Maven dependencies

<dependency> 
	<groupId>org.elasticsearch.client</groupId> 
	<artifactId>transport</artifactId> 			     
	<version>7.12.1</version> 
</dependency>

use

// 创建客户端连接 
TransportClient client = new PreBuiltTransportClient(Settings.EMPTY) 
.addTransportAddress(new TransportAddress(InetAddress.getByName("host1"), 9300)) 
.addTransportAddress(new TransportAddress(InetAddress.getByName("host2"), 9300));
// 关闭客户端 client.close(); ```

sniffer

Settings settings = Settings.builder().put("client.transport.sniff", true).build(); 
TransportClient client = new PreBuiltTransportClient(settings);

Java REST Client
RestClient is thread-safe. RestClient uses Elasticsearch's HTTP service, which defaults to port 9200, which is different from transport client.
Life cycle (ES 5.0.0-alpha4 to now)
Java Low-level REST client
The first 5.0.0 version of the Java REST client is called a low-level client because it does little to help Java users construct requests or parse responses . It handles the request's path and query string construction, but it treats JSON request and response bodies as opaque byte arrays that must be processed by the user.

features

  • Compatible with any Elasticsearch version

ES 5.0.0 is only the ES version (2016) when the first Java Low-level REST client was released. It does not mean that it is only compatible with 5.0. The Java Low-level REST client is based on the Apache HTTP client, which allows the use of HTTP Communicate with any version of an Elasticsearch cluster.

  • minimize dependencies
  • Load balancing across all available nodes
  • Failover in case of node failure and specific response codes
  • Connection failure penalty (whether a failed node is retried depends on how many times it failed in a row; the more failed attempts, the longer the client waits before trying the same node again)
  • persistent connection
  • Tracking of requests and responses
  • Optional cluster node auto-discovery (aka sniffing)

Maven dependencies

<dependency> 
	<groupId>org.elasticsearch.client</groupId> 
	<artifactId>elasticsearch-rest-client</artifactId> 
	<version>7.12.0</version> 
</dependency>

initialization

RestClient restClient = RestClient.builder(new HttpHost("localhost1", 9200, 
"http"), new HttpHost("localhost2", 9200, "http")).build();

Resource release
restClient.close();

sniffer

A minimal library that allows automatic discovery of nodes from a running Elasticsearch cluster and provisioning them as existing RestClient instances.

Maven dependencies

<dependency> 
	<groupId>org.elasticsearch.client</groupId> 
	<artifactId>elasticsearch-rest-client-sniffer</artifactId> 
	<version>7.12.1</version> 
</dependency>

the code

//默认每五分钟发现一次 
RestClient restClient = RestClient.builder(
new HttpHost("localhost", 9200, "http")) .build(); 
Sniffer sniffer = Sniffer.builder(restClient).build();

The resource release
Sniffer object should have the same lifecycle as the RestClient and be closed before the client.

java sniffer.close(); 
restClient.close();

Set sniffing interval

RestClient restClient = RestClient.builder( 
new HttpHost("localhost", 9200, "http")) .build(); 
// 设置嗅探间隔为60000毫秒 
Sniffer sniffer = Sniffer.builder(restClient) 
.setSniffIntervalMillis(60000).build();

Restart sniffing on failure Enable
sniffing on failure, that is, after each failure, the node list is updated immediately, rather than in the next normal sniffing round. In this case, a SniffOnFailureListener needs to be created first and provided when the RestClient is created. Also, once the sniffer is created later, it needs to be associated with the same SniffOnFailureListener instance, which will be notified on each failure and perform additional rounds of sniffing with the sniffer

SniffOnFailureListener sniffOnFailureListener = new SniffOnFailureListener(); 
RestClient restClient = RestClient.builder( 
new HttpHost("localhost", 9200)).setFailureListener(sniffOnFailureListener) 
.build(); 
Sniffer sniffer = Sniffer.builder(restClient) 
//在嗅探失败时,不仅节点在每次失败后都会更新,
而且还会比平常更早安排额外的嗅探轮次,默认情况下是在失败后一分钟,
假设事情会恢复正常并且我们想要检测尽快地。可以在 Sniffer 创建时通过
 setSniffAfterFailureDelayMillis 方法自定义所述间隔。
请注意,如果如上所述未启用故障嗅探,则最后一个配置参数无效。
.setSniffAfterFailureDelayMillis(30000) 
.build(); 
//将 Sniffer 实例设置为失败侦听器
sniffOnFailureListener.setSniffer(sniffer); 

Java High Level REST Client
life cycle (ES 5.0.0-alpha4 to now)

The Java high-level REST client runs on top of the Java low-level REST client. Its main goal is to expose API-specific methods that accept request objects as parameters and return response objects, so that request marshalling and response unmarshalling are handled by the client itself. Requires Elasticsearch version 2.0 or higher.
Client advantages and disadvantages and compatibility recommendations
Read: https://www.elastic.co/cn/blog/benchmarking-rest-client-transport-client
Java API

  • Advantages:
    slightly better performance:
    large throughput: Transport Client's batch index throughput is 4% to 7% larger than that of HTTP client (laboratory conditions)
  • Disadvantages:
    Heavy dependency: It is not a "client" in a separate sense. It depends on lucene, log4j2, etc., which may cause dependency conflicts.
    Unsafe: Java API calls services through the transport layer, which is not safe.
    Re-coupling: It has common dependencies with ES core services and requires high version compatibility.

Advantages of REST API

  • Security: REST API uses a single cluster entry point, which can ensure data security through HTTPS, and the transport layer is only used for internal node-to-node communication.

  • Ease of use: the client only calls the service through the REST layer instead of the transport layer, which can greatly simplify code writing. Disadvantages

  • The performance is slightly inferior to the Java API, but the gap is not large

shortcoming

  • Performance is slightly inferior to Java API, but not by much
    - Low level Client
    Advantages:
    Light dependencies: Apache HTTP Asynchronous Client and its transitive dependencies (Apache HTTP Client, Apache HTTP Core, Apache HTTP Core NIO, Apache Commons Codec and Apache Commons Logging)
    Strong compatibility: Compatible with all ES versions
    Disadvantages:
    Less functions: Obviously, the inevitable consequences of lightweight
    - High level Client
    Advantages:
    Powerful: Support API calls for all ES.
    Loosely coupled: the client and ES core services are completely independent and have no common dependencies.
    Stable interface: The REST API is much more stable than the Transport Client interface that exactly matches the Elasticsearch version.
    Disadvantages:
    Moderate compatibility: Based on Low Level Client, it is only backward compatible with major versions of ES. For example, the 6.0 client is compatible with 6.x (that is, the version after 6.0), but the 6.1 client may not support all 6.0ES APIs. But this is not a big problem, we can use the same version of the client and server, and it will not cause other problems.

insert image description here

Guess you like

Origin blog.csdn.net/qq_38747892/article/details/129708133