In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "elasticsearch how to construct Client to implement java client call interface", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "elasticsearch how to construct Client to achieve java client call interface" bar!
Elasticsearch provides a rich set of java calling interfaces by constructing a client. Generally speaking, client can be divided into two types: cluster information client and data (index) client. These two categories can be divided into two categories: general operation and admin operation.
Inheritance relationship of client
(version 1.5, other versions may be different):
Through this inheritance diagram, you can clearly understand the implementation and functions of client. There are three categories, namely client, indicesAdminClient and ClusterAdminClient. It all has its own implementation class, but in the end, it provides services through the client interface. As the general interface to the outside world, client first combines the related operations of admin through the admin () method, and it also provides all the common operations on data and cluster.
The realization of the method
All interfaces implement asynchronous calls in two ways, one is to return an ActionFuture, and the other is to accept an ActionListener.
Taking index method as an example
As shown below
ActionFuture index (IndexRequest request)
Void index (IndexRequest request, ActionListener listener)
The first method returns a future, and the second method needs to pass a Listener. These are also two basic ways to implement asynchronously. Client uses the facade pattern, and all the implementations are in the AbstractClient class. Take the index method as an example, and the code is as follows:
@ Override public ActionFuture index (final IndexRequest request) {return execute (IndexAction.INSTANCE, request);} @ Override public void index (final IndexRequest request, final ActionListener listener) {execute (IndexAction.INSTANCE, request, listener);}
As shown above, the implementation is a facade pattern because all the methods are integrated into the client, but the execution is performed in the corresponding action. In the execute method, the corresponding action instance is obtained, and the real logic is implemented in the corresponding transportaction.
Execute method code
As follows:
@ SuppressWarnings ("unchecked") @ Override public ActionFuture execute (Action action, Request request) {headers.applyTo (request); TransportAction transportAction = actions.get ((ClientAction) action); return transportAction.execute (request);} @ SuppressWarnings ("unchecked") @ Override public void execute (Action action, Request request, ActionListener listener) {headers.applyTo (request); TransportAction transportAction = actions.get ((ClientAction) action) TransportAction.execute (request, listener);}
Each operation should have a corresponding transportAction, and these transportAction are the ultimate executors. Let's take index as an example, and we'll see more of this in the later analysis of the indexing function.
Public class IndexAction extends ClientAction {public static final IndexAction INSTANCE = new IndexAction (); public static final String NAME = "indices:data/write/index"; private IndexAction () {super (NAME);} @ Override public IndexResponse newResponse () {return new IndexResponse ();} @ Override public IndexRequestBuilder newRequestBuilder (Client client) {return new IndexRequestBuilder (client);}}
In IndexAction, you simply define a NAME and a few simple methods. This name will be registered in the TransportService as the key for the transportHandler at startup. In the execute method, the transportAction is pulled out according to action, as shown in the previous code. The real execution logic is in InternalTransportClient, and its implementation is skipped here, which will be analyzed in detail later. All of these action registrations are implemented in actionModule, and the registration process is analyzed later with action.
At this point, I believe you have a deeper understanding of "how elasticsearch constructs Client to implement java client call interface". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.