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中。这种精巧的设计给使用者带来很大的便利 。
elasticsearch java 客户端之Client简介的更多相关文章
- elasticsearch java 客户端之action简介
上一篇介绍了elasticsearch的client结构,client只是一个门面,在每个方法后面都有一个action来承接相应的功能.但是action也并非是真正的功能实现者,它只是一个代理,它的真 ...
- Jest — ElasticSearch Java 客户端
1. 介绍 任何使用过Elasticsearch的人都知道,使用基于rest的搜索API构建查询可能是单调乏味且容易出错的. 在本教程中,我们将研究Jest,一个用于Elasticsearch的HTT ...
- ElasticSearch java客户端更新时出现的错误:NoNodeAvailableException[None of the configured nodes are available
下午尝试 用ElasticSearch 的java客户端去做数据检索工作,测试了一下批量更新,代码如下: public static void bulkUpdateGoods(List<Goo ...
- springboot 配置elasticsearch Java High Rest Client
前提声明 在新版本的spring boot中逐渐放弃了对Spring Data Elasticsearch的支持,所以不推荐使用,使用ES官方推出的Java High Rest Client. 引入依 ...
- elasticsearch java客户端api使用(一)
1.客户端client构建 package com.pz998.app.service.utils; import static org.elasticsearch.common.settings ...
- Elasticsearch java客户端调用cat服务
开发环境,测试环境,预发环境和生产环境一般相互隔离的,使用开发环境或者测试环境可以使用cat来查看索引的情况 例如: 但预防环境和测试环境是不允许访问的,那怎么办呢? 可以使用后台来查看上述信息,提供 ...
- State of the official Elasticsearch Java clients
Elasticsearch Java Clients | Elastic https://www.elastic.co/blog/state-of-the-official-elasticsearch ...
- elasticsearch系列七:ES Java客户端-Elasticsearch Java client(ES Client 简介、Java REST Client、Java Client、Spring Data Elasticsearch)
一.ES Client 简介 1. ES是一个服务,采用C/S结构 2. 回顾 ES的架构 3. ES支持的客户端连接方式 3.1 REST API ,端口 9200 这种连接方式对应于架构图中的RE ...
- Elasticsearch Java client(ES Client 简介、Java REST Client、Java Client、Spring Data Elasticsearch)
elasticsearch系列七:ES Java客户端-Elasticsearch Java client(ES Client 简介.Java REST Client.Java Client.Spri ...
随机推荐
- 【SRM 717 div2 A】 NiceTable
Problem Statement You are given a vector t that describes a rectangular table of zeroes and ones. Ea ...
- 高性能网络编程 - select系统调用
IO复用使得程序可以同一时候监听多个文件描写叙述符,比方client须要同一时候处理用户输入和网络连接,server端须要同一时候处理监听套接字和连接套接字,select系统调用可以使得我们 ...
- java音乐播放之IO流处理
这个类仅仅能一直播放.知道音乐结束. 比AudioCilp要好一点. import java.io.*; import javax.sound.sampled.*; public class Test ...
- centos 7.3 配置vnc 服务 图形界面登录
1.检查系统是否有安装tigervnc-server软件包 rpm -qa |grep vnc 默认的系统未装tigervnc-server软件包 2.安装tigervnc-server软件包 yum ...
- Hadoop的多节点集群详细启动步骤(3或5节点)
版本1 利用自己写的脚本来启动,见如下博客 hadoop-2.6.0-cdh5.4.5.tar.gz(CDH)的3节点集群搭建 hadoop-2.6.0.tar.gz的集群搭建(3节点) hadoop ...
- Important Abstractions and Data Structures
For Developers > Coding Style > Important Abstractions and Data Structures 目录 1 TaskRunne ...
- UVA 12649 Folding Machine 搜索
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- Hexo Daemon
前提 今天中午的时候发现自已网站突然不能访问了,我猜肯定是后台的hexo服务异常自动kill掉了.果然登录服务器ps -ef | grep hexo查看进程,果然发现hexo的进程不在了.由于我将输出 ...
- gitlab-ce-11.0.1 安装及汉化
1.添加gitlab源(我这里使用了清华大学的源)cat <<EOF> /etc/yum.repos.d/gitlab-ce.repo[gitlab-ce]name=gitlab-c ...
- 基于Pipe的PureMVC FLEX框架的多核共享消息技术
pipes utilities,也就是所谓的通道(管道),为什么要使用通道呢?模块的结构都是一个单独的puremvc结构,模块和模块,shell和模块之间的通信 不能使用puremvc中的消息进行,因 ...