Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand ElasticSearch Client

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces you how to understand ElasticSearch Client, the content is very detailed, interested friends can refer to, hope to be helpful to you.

The editor will focus on the relevant knowledge of ElasticSearch Client, focusing on TransportClient and Rest Client. Elasticsearch client is our gateway to Elasti-Search.

1. Overview

Focus on Elasticsearch's Java-related API implementation. All Elasticsearch operations are performed using the Client object. All API defined by Client are executed asynchronously (either using event listener callbacks or using Future patterns). In addition, operations on the client can be accumulated and executed in batches.

Elasticsearch officially plans to discard TransportClient in Elasticsearch 7.0 and delete it completely in 8.0. Instead, you should use the Java advanced REST client,rest client to execute HTTP requests to perform operations, eliminating the need for serialized Java requests. Java Advanced REST Client API currently supports the more commonly used api, but more api needs to be added.

Any missing api can be achieved by using a low-level Java REST client with JSON request and response bodies. Officially, ElasticSearch should no longer provide client-side API for a specific language, but instead use generic rest requests (http) to interact with ElasticSearch servers.

Next we will start from java api to enter the ElasticSearch API study.

ElasticSearch Client provides the following implementation according to the programming language:

Next, we will focus on JAVA Client and Java REST Client.

2. Detailed explanation of TransportClient

2.1 Overview of TransportClient

TransportClient is an ElasticSearch (java) client encapsulated object that uses the transport module to remotely connect to the Elasticsearch cluster. The transport node does not join the cluster, but simply sends requests to the nodes on the ElasticSearch cluster. Transport node uses a polling mechanism for load balancing among nodes in the cluster, although most operations (requests) may be "two-hop operations", as shown in the figure (cited in the Elasticsearch authoritative guide).

As shown in the figure above, taking a new operation as an example, the first request is first sent to NODE1, and then routing is calculated based on ID (hashcode (id)% number of primary shards). For example, using p0 (the first primary shard), NODE1 forwards the request to Node3, and the client sends a second request, which is sent to NODE2 (polling mechanism above).

The default method to build TransportClient is as follows

TransportClient client = new PreBuiltTransportClient (Settings.EMPTY) / / @ 1

.addTransportAddress (new TransportAddress (InetAddress.getByName ("192.168.1.10"), 9300)) / / @ 2

.addTransportAddress (new TransportAddress (InetAddress.getByName ("192.168.1.11"), 9300))

/ / on shutdown

Client.close ()

Code @ 1: use an empty configuration.

Code @ 2: connect ElasticSearch nodes, and you can connect multiple Node nodes through the addTransportAddress method, so that requests will be sent to these nodes in turn to achieve load balancing of cluster nodes when accepting requests.

The main parameters for the TransportClient level are as follows:

Parameter name

Parameter description

Cluster.name

Transport node and server nodes are not in the same cluster.

Client.transport.sniff

Whether to enable cluster sniffing is described in more detail below.

Client.transport.ignore_cluster_name

Whether to ignore the cluster name check of the connected node. Setting it to true means to ignore it, so that the connected node is not in the same cluster.

Client.transport.ping_timeout

The response timeout for the ping command is 5s by default.

Client.transport.nodes_sampler_interval

The frequency at which ping commands are sent to connected nodes defaults to 5s, which is often called heartbeat detection interval.

Next, we will focus on the client.transport.sniff parameter, cluster sniffing mechanism.

When creating a TransportClient, you can statically add nodes in the ElasticSearch cluster through addTransportAddress. If the cluster sniffing mechanism is enabled, that is, the node dynamic discovery mechanism is enabled, nodes are allowed to be added and deleted dynamically. When sniffing is enabled, the client first connects to the node in the addTransportAddress. After that, the client will call the internal cluster state API on these nodes to discover the available data nodes. The client's internal node list will be replaced only by the discovered data nodes. By default, this list is refreshed every 5 seconds. This means that if the node is not a data node, the list may not include the original node to which it is connected. For example, if you initially connect to a master node, and after sniffing, if you find a corresponding data node, you will no longer make a request to that master node, but to any data node. The reason why the transport client excludes non-data nodes is to avoid sending search traffic only to the primary node.

Use the configuration build Settings to build the TransportClient object code as follows:

Settings settings = Settings.builder ()

.put ("cluster.name", "myClusterName")

.put ("client.transport.sniff", "true") .build ()

TransportClient client = new PreBuiltTransportClient (settings)

.addTransportAddress (new TransportAddress (InetAddress.getByName ("192.168.1.10"), 9300))

2.2 、 TransportClient API

The core class inheritance diagram of TransportClient is as follows:

The design points of the above API are as follows:

The entire client API provides two lowest-level methods, execute, whose key features are as follows:

ActionFuture execute (Action action, Request request)

Return ActionFuture, which is a typical asynchronous call, Future mode, as you can tell by its name.

Void execute (Action action, Request request, ActionListener listener)

No return value is returned, but ActionListener listener needs to be passed. As can be known by the name, this parameter acts as an event listener (callback method), that is, after receiving the server response, call the callback function to process the result.

Note: the essence of ElasticSearch Client API is to use the asynchronous request mode.

Methods that begin with prepare, such as IndexRequestBuilder prepareIndex ()

This type of API is designed to use the Build pattern, which first builds the request parameters through build, and finally completes the interface call by calling the get () method.

This is the end of the TransportClient Api explanation, and the above API will be classified and explained in detail later.

2.3 Maven is lazy

Org.elasticsearch.client transport 6.4.1 org.apache.logging.log4j log4j-core 2.11.1

3. Detailed explanation of Java Rest Client

There are two styles of Java REST clients:

Java Low Level REST Client:elasticsearch client low-level client. It allows communication with the Elasticsearch cluster through http requests. API itself is not responsible for encoding and decoding the data, but by the user to encode and decode. It is compatible with all ElasticSearch versions.

Java High Level REST Client:Elasticsearch client official advanced client. Based on the low-level client, the API defined by it has encoded and decoded the request and response packets.

3.1 Java High Level REST Client

3.1.1 initialization

RestHighLevelClient client = new RestHighLevelClient (new HttpHost ("localhost", 9200, "http"), new HttpHost ("localhost", 9201, "http")); / / closeclient.close ()

The mechanism of new HttpHost ("localhost", 9200, "http") is consistent with that of TransportClient's addTransportAddress.

3.1.2 Core API is lazy

The class diagram of RestHighLevelClient is as follows:

Its API design has the following characteristics:

Each API provides synchronous and asynchronous calls. A method whose name ends in async is an asynchronous call, and a corresponding ActionListener implementation is required.

Each API can provide a RequestOptions object to customize the request selection.

The above API will not be covered one by one in this section, and the above API will be analyzed in detail in subsequent articles.

3.1.3 Maven is lazy

Org.elasticsearch.client elasticsearch-rest-client 6.4.0 org.elasticsearch.client elasticsearch-rest-high-level-client 6.4.0 pom on how to understand ElasticSearch Client to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report