首页 > Java > java教程 > 如何使用Java构建Elasticsearch客户端并调用API?

如何使用Java构建Elasticsearch客户端并调用API?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
发布: 2023-04-24 09:46:06
转载
885 人浏览过

    elasticsearch通过构造一个client对外提供了一套丰富的java调用接口。总体来说client分为两类cluster信息方面的client及数据(index)方面的client。这两个大类由可以分为通用操作和admin操作两类。

    client的继承关系

    (1.5版本,其它版本可能不一样):

    elasticsearch怎么构造Client实现java客户端调用接口

    通过这个继承关系图可以很清楚的了解client的实现,及功能。总共有三类即client, indicesAdminClient和ClusterAdminClient。它都有自己的实现类,但最后都是通过client接口对外提供服务。client作为对外的总接口,首先通过admin()方法组合了admin的相关操作,它本身也提供了所有对数据和cluster的通用操作。

    方法实现上

    所有的接口都通过两种方式实现了异步调用,一个是返回一个ActionFuture,另外一种方式是接受一个ActionListener。

    以index方法为例

    如下所示

     ActionFuture  index(IndexRequest request) ;

     void index(IndexRequest request, ActionListener listener);

    第一个方法会返回一个future,第二个方法则需要传递一个Listener。这也是异步实现的两个基本方式。client使用了门面模式,所有的实现都在AbstractClient类中,还以index方法为例,代码如下所示:

    1

    2

    3

    4

    5

    6

    7

    8

    @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方法代码

    如下所示:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    @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为例简单说明,在后面索引功能分析中会看到更多这种的结果。

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    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一起分析。

    以上是如何使用Java构建Elasticsearch客户端并调用API?的详细内容。更多信息请关注PHP中文网其他相关文章!

    相关标签:
    本站声明
    本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
    最新问题
    java可以做为web的后端吗?
    来自于 1970-01-01 08:00:00
    0
    0
    0
    安装JAVA
    来自于 1970-01-01 08:00:00
    0
    0
    0
    无法安装java
    来自于 1970-01-01 08:00:00
    0
    0
    0
    java - php调取webservice的map类型,如果封装?
    来自于 1970-01-01 08:00:00
    0
    0
    0
    这个是Java语言的吗
    来自于 1970-01-01 08:00:00
    0
    0
    0
    热门教程
    更多>
    最新下载
    更多>
    网站特效
    网站源码
    网站素材
    前端模板