【问题标题】:Adding a new HTTP client to Elasticsearch to support client apps to run against AWS Elasticsearch?向 Elasticsearch 添加新的 HTTP 客户端以支持客户端应用程序在 AWS Elasticsearch 上运行?
【发布时间】:2016-04-21 08:22:31
【问题描述】:

我正在尝试使用 JEST 将 Elasticsearch HTTP 访问添加到 Titan ES client。 Titan-es 仅支持 ES 的本地和传输 (TCP) 模式。但我想支持通过 ES 的 HTTP 接口进行通信。这将允许像titan-es 这样的客户端库使用AWS Elasticsearch 作为只提供HTTP(S) 接口的索引后端。请参阅this post 了解更多信息。

我正在寻找有关我目前正在考虑的方法的一些反馈:

  1. 创建一个实现org.elasticache.client.Client 接口的新类ElasticsearchHttpClient。新类将使用JestClient 作为其内部客户端。这样它将通过 HTTP 与 ES 通信。新类可能会扩展 ES 的 AbstractClient,以减少必须实现的方法:admin()settings()execute()threadPool()close()
  2. 将新枚举HTTP_CLIENT 添加到ElasticSearchSetup
  3. 确保HTTP_CLIENT 上的connect() 方法返回Connection 的实例,其中包含nodeclient 的正确值。 client 成员将是新的 ElasticsearchHttpClient 类的一个实例。
  4. 如果INTERFACE 配置为HTTP_CLIENT,请确保ElasticSearchIndex.interfaceConfiguration() 方法检索Connection 的正确实例(包含新的ElasticsearchHttpClient)。从那时起,其余代码应该继续在新协议上工作。

听起来应该可行吗?第一步是我最关心的问题 - 我不相信我可以使用 JestClient 实现所有客户端方法。

package com.thinkaurelius.titan.diskstorage.es;

import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.JestResult;
import io.searchbox.client.config.HttpClientConfig;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.*;
import org.elasticsearch.client.AdminClient;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.support.AbstractClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.threadpool.ThreadPool;

import java.io.IOException;

public class ElasticsearchHttpClient extends AbstractClient {
    private final JestClient internalClient;
    private final ThreadPool pool;

    public ElasticsearchHttpClient(String hostname, int port) {
        JestClientFactory factory = new JestClientFactory();
        factory.setHttpClientConfig(new HttpClientConfig
                .Builder(String.format("http://%s:%d", hostname, port))
                .multiThreaded(true)
                .build());
        JestClient client = factory.getObject();

        this.pool = new ThreadPool("jest");
        this.internalClient = client;
    }

    @Override
    public AdminClient admin() {
        return null;
    }

    @Override
    public Settings settings() {
        return null;
    }

    @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) {
        try {
            JestResult response = internalClient.execute(convertRequest(action, request));
            return convertResponse(response);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @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) {
        execute(action, request);
    }

    private <Response extends ActionResponse> ActionFuture<Response> convertResponse(JestResult result) {
        // TODO How to convert a JestResult a Elasticsearch ActionResponse/ActionFuture?
        return null;
    }

    private <Request extends ActionRequest, Response extends ActionResponse, RequestBuilder extends ActionRequestBuilder<Request, Response, RequestBuilder, Client>> io.searchbox.action.Action<JestResult> convertRequest(Action<Request, Response, RequestBuilder, Client> action, Request request) {
        // TODO How to convert an Elasticsearch Action<..> and Request to a Jest Action<JestResult>?
        return null;
    }

    @Override
    public ThreadPool threadPool() {
        return pool;
    }

    @Override
    public void close() throws ElasticsearchException {
        pool.shutdownNow();
    }
}

[我也在Titan mailing listElasticsearch forum上问过这个问题。]

【问题讨论】:

    标签: java elasticsearch titan amazon-elasticsearch elasticsearch-jest


    【解决方案1】:

    我已在Titan mailing list 中发布了答案。

    从 Titan 的角度来看,您需要做的是实现 索引提供者接口。我的猜测是它是不可行的 Jest 看起来像一个完整的 Elasticsearch 客户端。

    我认为你会使用 JestHttpClient -- 你不需要实现 Jest 界面。 IndexProvider 有方法 创建/删除/变异/查询一个索引,你应该能够完成 HTTP。检查 Elasticsearch HTTP 文档,看看你是否可以这样做 IndexProvider 上所有必需的方法都带有 JestHttpClient。

    已经有一个 IndexProvider 的 ElasticSearchIndex 实现, 哪个节点和传输。您正在尝试添加 HTTP 或 JEST 选项。因此,您可能会考虑将您的更改硬塞到 ElasticSearchIndex,但我不确定它的效果如何 现有的 2 个 impl 都是完整的 Elasticsearch 客户端。可能 考虑创建一个单独的 ElasticSearchHttpIndex 实现 IndexProvider 如果它更干净的话。

    【讨论】:

      猜你喜欢
      • 2016-09-28
      • 2012-05-13
      • 2017-07-05
      • 2020-02-08
      • 2017-02-12
      • 1970-01-01
      • 2017-01-08
      • 1970-01-01
      • 2015-04-08
      相关资源
      最近更新 更多