elasticsearch構(gòu)造Client實現(xiàn)java客戶端調(diào)用接口示例分析
elasticsearch通過構(gòu)造一個client對外提供了一套豐富的java調(diào)用接口??傮w來說client分為兩類cluster信息方面的client及數(shù)據(jù)(index)方面的client。這兩個大類由可以分為通用操作和admin操作兩類。
client的繼承關(guān)系
(1.5版本,其它版本可能不一樣):

通過這個繼承關(guān)系圖可以很清楚的了解client的實現(xiàn),及功能??偣灿腥惣碿lient, indicesAdminClient和ClusterAdminClient。它都有自己的實現(xiàn)類,但最后都是通過client接口對外提供服務。client作為對外的總接口,首先通過admin()方法組合了admin的相關(guān)操作,它本身也提供了所有對數(shù)據(jù)和cluster的通用操作。
方法實現(xiàn)上
所有的接口都通過兩種方式實現(xiàn)了異步調(diào)用,一個是返回一個ActionFuture,另外一種方式是接受一個ActionListener。
以index方法為例
如下所示
ActionFuture<IndexResponse> index(IndexRequest request) ;
void index(IndexRequest request, ActionListener<IndexResponse> listener);
第一個方法會返回一個future,第二個方法則需要傳遞一個Listener。這也是異步實現(xiàn)的兩個基本方式。client使用了門面模式,所有的實現(xiàn)都在AbstractClient類中,還以index方法為例,代碼如下所示:
@Override
public ActionFuture<IndexResponse> index(final IndexRequest request) {
return execute(IndexAction.INSTANCE, request);
}
@Override
public void index(final IndexRequest request, final ActionListener<IndexResponse> listener) {
execute(IndexAction.INSTANCE, request, listener);
}實現(xiàn)如上所示,之所以說它是門面模式是因為所有的方法都被集成到了client中,但是執(zhí)行過程都是在對應的action中執(zhí)行。在execute方法中,獲取到相應的action實例,真正的邏輯是在對應的transportaction中實現(xiàn)。
execute方法代碼
如下所示:
@SuppressWarnings("unchecked")
@Override
public <Request extends ActionRequest, Response extends ActionResponse, RequestBuilder extends ActionRequestBuilder<Request, Response, RequestBuilder, Client>> ActionFuture<Response> execute(Action<Request, Response, RequestBuilder, Client> action, Request request) {
headers.applyTo(request);
TransportAction<Request, Response> transportAction = actions.get((ClientAction)action);
return transportAction.execute(request);
}
@SuppressWarnings("unchecked")
@Override
public <Request extends ActionRequest, Response extends ActionResponse, RequestBuilder extends ActionRequestBuilder<Request, Response, RequestBuilder, Client>> void execute(Action<Request, Response, RequestBuilder, Client> action, Request request, ActionListener<Response> listener) {
headers.applyTo(request);
TransportAction<Request, Response> transportAction = actions.get((ClientAction)action);
transportAction.execute(request, listener);
}每一種操作都對應有相應的transportAction,這些transportAction才是最終的執(zhí)行者。這里先以index為例簡單說明,在后面索引功能分析中會看到更多這種的結(jié)果。
public class IndexAction extends ClientAction<IndexRequest, IndexResponse, IndexRequestBuilder> {
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);
}
}在IndexAction中只是簡單的定義了一個NAME,及幾個簡單的方法。這個名字會在啟動時作為對于的transportHandler的key注冊到TransportService中。在execute方法中,會根據(jù)action的將transportAction取出如上一段代碼所示。真正的執(zhí)行邏輯在InternalTransportClient中,這里先略過它的實現(xiàn),后面會有詳細分析。所有這些action的注冊都是在actionModule中實現(xiàn),注冊過程會在后面跟action一起分析。
總結(jié):
client模塊通過代理模式,將所有的操作都集成到client接口中。這樣外部調(diào)用只需要初始化client就能夠完成所有的調(diào)用功能。這些接口的執(zhí)行邏輯均在對應的transportAction中。這種精巧的設(shè)計給使用者帶來很大的便利 。
以上就是elasticsearch構(gòu)造Client實現(xiàn)java客戶端調(diào)用接口示例分析的詳細內(nèi)容,更多關(guān)于elasticsearch構(gòu)造java客戶端調(diào)用接口Client的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java中HTTP GET方法調(diào)用帶有body的問題解決
這篇文章主要為大家詳細介紹了Java如何解決HTTP GET方法調(diào)用帶有body的問題,文中的示例代碼講解詳細,感興趣的小伙伴可以參考一下2024-02-02
詳解Spring Boot配置使用Logback進行日志記錄的實戰(zhàn)
本篇文章主要介紹了詳解Spring Boot配置使用Logback進行日志記錄的實戰(zhàn),具有一定的參考價值,有興趣的朋友可以了解一下2017-07-07
SpringBoot3結(jié)合gRpc實現(xiàn)遠程服務調(diào)用的流程步驟
gRPC是一個現(xiàn)代開源高性能遠程過程調(diào)用(RPC)框架,可以在任何環(huán)境中運行,它由Google開發(fā),旨在幫助開發(fā)人員更輕松地構(gòu)建分布式應用,特別是當代碼可能在不同地方運行的時候,本文介紹了SpringBoot3結(jié)合gRpc實現(xiàn)遠程服務調(diào)用的流程步驟,需要的朋友可以參考下2024-07-07
java 中JDBC連接數(shù)據(jù)庫代碼和步驟詳解及實例代碼
這篇文章主要介紹了java 中JDBC連接數(shù)據(jù)庫代碼和步驟詳解及實例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02
SpringBoot中將@Bean方法解析為BeanDefinition詳解
這篇文章主要介紹了SpringBoot中將@Bean方法解析為BeanDefinition詳解,得到的BeanDefinition是ConfigurationClassBeanDefinition類型,會為BeanDefinition設(shè)置factoryMethodName,這意味著當實例化這個bean的時候?qū)⒉捎霉S方法,需要的朋友可以參考下2023-12-12

