elasticsearch怎么构造Client实现java客户端调用接口
导读:本文共2372字符,通常情况下阅读需要8分钟。同时您也可以点击右侧朗读,来听本文内容。按键盘←(左) →(右) 方向键可以翻页。
摘要: elasticsearch通过构造一个client对外提供了一套丰富的java调用接口。总体来说client分为两类cluster信息方面的client及数据(index)方面的client。这两个大类由可以分为通用操作和admin操作两类。client的继承关系(1.5版本,其它版本可能不一样):通过这个继承关系图可以很清楚的了解client的实现,及功能。总... ...
目录
(为您整理了一些要点),点击可以直达。elasticsearch通过构造一个client对外提供了一套丰富的java调用接口。总体来说client分为两类cluster信息方面的client及数据(index)方面的client。这两个大类由可以分为通用操作和admin操作两类。
client的继承关系
(1.5版本,其它版本可能不一样):
通过这个继承关系图可以很清楚的了解client的实现,及功能。总共有三类即client,indicesAdminClient和ClusterAdminClient。它都有自己的实现类,但最后都是通过client接口对外提供服务。client作为对外的总接口,首先通过admin()方法组合了admin的相关操作,它本身也提供了所有对数据和cluster的通用操作。
方法实现上
所有的接口都通过两种方式实现了异步调用,一个是返回一个ActionFuture,另外一种方式是接受一个ActionListener。
以index方法为例
如下所示
ActionFuture<IndexResponse> index(IndexRequest request) ;
void index(IndexRequest request, ActionListener<IndexResponse> listener);
第一个方法会返回一个future,第二个方法则需要传递一个Listener。这也是异步实现的两个基本方式。client使用了门面模式,所有的实现都在AbstractClient类中,还以index方法为例,代码如下所示:
@OverridepublicActionFuture<IndexResponse>index(finalIndexRequestrequest){returnexecute(IndexAction.INSTANCE,request);}@Overridepublicvoidindex(finalIndexRequestrequest,finalActionListener<IndexResponse>listener){execute(IndexAction.INSTANCE,request,listener);}
实现如上所示,之所以说它是门面模式是因为所有的方法都被集成到了client中,但是执行过程都是在对应的action中执行。在execute方法中,获取到相应的action实例,真正的逻辑是在对应的transportaction中实现。
execute方法代码
如下所示:
@SuppressWarnings("unchecked")@Overridepublic<RequestextendsActionRequest,ResponseextendsActionResponse,RequestBuilderextendsActionRequestBuilder<Request,Response,RequestBuilder,Client>>ActionFuture<Response>execute(Action<Request,Response,RequestBuilder,Client>action,Requestrequest){headers.applyTo(request);TransportAction<Request,Response>transportAction=actions.get((ClientAction)action);returntransportAction.execute(request);}@SuppressWarnings("unchecked")@Overridepublic<RequestextendsActionRequest,ResponseextendsActionResponse,RequestBuilderextendsActionRequestBuilder<Request,Response,RequestBuilder,Client>>voidexecute(Action<Request,Response,RequestBuilder,Client>action,Requestrequest,ActionListener<Response>listener){headers.applyTo(request);TransportAction<Request,Response>transportAction=actions.get((ClientAction)action);transportAction.execute(request,listener);}
每一种操作都对应有相应的transportAction,这些transportAction才是最终的执行者。这里先以index为例简单说明,在后面索引功能分析中会看到更多这种的结果。
publicclassIndexActionextendsClientAction<IndexRequest,IndexResponse,IndexRequestBuilder>{publicstaticfinalIndexActionINSTANCE=newIndexAction();publicstaticfinalStringNAME="indices:data/write/index";privateIndexAction(){super(NAME);}@OverridepublicIndexResponsenewResponse(){returnnewIndexResponse();}@OverridepublicIndexRequestBuildernewRequestBuilder(Clientclient){returnnewIndexRequestBuilder(client);}}
在IndexAction中只是简单的定义了一个NAME,及几个简单的方法。这个名字会在启动时作为对于的transportHandler的key注册到TransportService中。在execute方法中,会根据action的将transportAction取出如上一段代码所示。真正的执行逻辑在InternalTransportClient中,这里先略过它的实现,后面会有详细分析。所有这些action的注册都是在actionModule中实现,注册过程会在后面跟action一起分析。
</div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
elasticsearch怎么构造Client实现java客户端调用接口的详细内容,希望对您有所帮助,信息来源于网络。