elasticsearch java 客户端之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方法为例,代码如下所示:
@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);
}
实现如上所示,之所以说它是门面模式是因为所有的方法都被集成到了client中,但是执行过程都是在对应的action中执行。在execute方法中,获取到相应的action实例,真正的逻辑是在对应的transportaction中实现。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才是最终的执行者。这里先以index为例简单说明,在后面索引功能分析中会看到更多这种的结果。
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方法中,会根据action的将transportAction取出如上一段代码所示。真正的执行逻辑在InternalTransportClient中,这里先略过它的实现,后面会有详细分析。所有这些action的注册都是在actionModule中实现,注册过程会在后面跟action一起分析。
总结:client模块通过代理模式,将所有的操作都集成到client接口中。这样外部调用只需要初始化client就能够完成所有的调用功能。这些接口的执行逻辑均在对应的transportAction中。这种精巧的设计给使用者带来很大的便利 。
最新文章
- Visual Studio 常用快捷键备忘
- 统计文件种类数+获取子shell返回值的其它方法
- GDUFE-OJ 1203x的y次方的最后三位数 快速幂
- Ubuntu 安装 mysql 并修改数据库目录
- 关于Linkedlist 和 ArrayDeque的讨论
- 用(*.frm *.MYD *.MYI)文件恢复MySql数据库
- Java IO (1) - InputStream
- PHP与最丑的后台管理系统
- 【转】android JNI编程 一些技巧(整理)
- Match类
- Xamarin.Android 入门实例(3)之呼叫电话号码
- Dynamic web module 版本之间的区别
- JEECG中出现Java.sql.SQLException: Value &#39;xxxx&#39; can not be represented as java.sql.Timestamp的解决办法
- [UOJ422][集训队作业2018]小Z的礼物——轮廓线DP+min-max容斥
- python爬虫程序打包为exe程序并在控制台下运行
- python 函数基础及装饰器
- cuda by example【读书笔记2】
- delphi-TTcpServer与TTcpClient
- golang模板语法
- linux的命令:
热门文章
- CMSIS-RTOS 时间管理之虚拟定时器Virtual Timers
- Understanding IIS Bindings, Websites, Virtual Directories, and lastly Application Pools
- 35.QQ大数据模型
- spring之DelegatingFilterProxy
- rem的解决方案
- Oracle 审计初步使用
- Flex XML/XMLList 常用操作
- swift菜鸟入门视频教程-12-21讲
- actionMode-theme中修改actionmode中more下拉框的背景颜色
- [Codeforces558E]A Simple Task 线段树