溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

elasticsearch怎么構(gòu)造Client實(shí)現(xiàn)java客戶端調(diào)用接口

發(fā)布時(shí)間:2022-04-22 10:00:38 來源:億速云 閱讀:222 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容主要講解“elasticsearch怎么構(gòu)造Client實(shí)現(xiàn)java客戶端調(diào)用接口”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“elasticsearch怎么構(gòu)造Client實(shí)現(xiàn)java客戶端調(diào)用接口”吧!

    elasticsearch通過構(gòu)造一個(gè)client對外提供了一套豐富的java調(diào)用接口??傮w來說client分為兩類cluster信息方面的client及數(shù)據(jù)(index)方面的client。這兩個(gè)大類由可以分為通用操作和admin操作兩類。

    client的繼承關(guān)系

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

    elasticsearch怎么構(gòu)造Client實(shí)現(xiàn)java客戶端調(diào)用接口

    通過這個(gè)繼承關(guān)系圖可以很清楚的了解client的實(shí)現(xiàn),及功能??偣灿腥惣碿lient, indicesAdminClient和ClusterAdminClient。它都有自己的實(shí)現(xiàn)類,但最后都是通過client接口對外提供服務(wù)。client作為對外的總接口,首先通過admin()方法組合了admin的相關(guān)操作,它本身也提供了所有對數(shù)據(jù)和cluster的通用操作。

    方法實(shí)現(xiàn)上

    所有的接口都通過兩種方式實(shí)現(xiàn)了異步調(diào)用,一個(gè)是返回一個(gè)ActionFuture,另外一種方式是接受一個(gè)ActionListener。

    以index方法為例

    如下所示

     ActionFuture<IndexResponse>  index(IndexRequest request) ;

     void index(IndexRequest request, ActionListener<IndexResponse> listener);

    第一個(gè)方法會返回一個(gè)future,第二個(gè)方法則需要傳遞一個(gè)Listener。這也是異步實(shí)現(xiàn)的兩個(gè)基本方式。client使用了門面模式,所有的實(shí)現(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);
        }

    實(shí)現(xiàn)如上所示,之所以說它是門面模式是因?yàn)樗械姆椒ǘ急患傻搅薱lient中,但是執(zhí)行過程都是在對應(yīng)的action中執(zhí)行。在execute方法中,獲取到相應(yīng)的action實(shí)例,真正的邏輯是在對應(yīng)的transportaction中實(shí)現(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);
        }

    每一種操作都對應(yīng)有相應(yīng)的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中只是簡單的定義了一個(gè)NAME,及幾個(gè)簡單的方法。這個(gè)名字會在啟動時(shí)作為對于的transportHandler的key注冊到TransportService中。在execute方法中,會根據(jù)action的將transportAction取出如上一段代碼所示。真正的執(zhí)行邏輯在InternalTransportClient中,這里先略過它的實(shí)現(xiàn),后面會有詳細(xì)分析。所有這些action的注冊都是在actionModule中實(shí)現(xiàn),注冊過程會在后面跟action一起分析。

    到此,相信大家對“elasticsearch怎么構(gòu)造Client實(shí)現(xiàn)java客戶端調(diào)用接口”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

    向AI問一下細(xì)節(jié)

    免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

    AI