简体   繁体   English

如何在 Flink 上将 BasicAuth 与 ElasticSearch Connector 结合使用

[英]How to use BasicAuth with ElasticSearch Connector on Flink

I want to use the elastic producer on flink but I have some trouble for authentification: I have Nginx in front of my elastic search cluster, and I use basic auth in nginx.我想在 flink 上使用弹性生产者,但我在身份验证方面遇到了一些麻烦:我的弹性搜索集群前面有 Nginx,我在 nginx 中使用了基本身份验证。

But with the elastic search connector I can't add the basic auth in my url (because of InetSocketAddress)但是使用弹性搜索连接器,我无法在我的 url 中添加基本身份验证(因为 InetSocketAddress)

did you have an Idea to use elasticsearch connector with basic auth ?您是否有将弹性搜索连接器与基本身份验证结合使用的想法?

Thanks for your time.谢谢你的时间。

there is my code :有我的代码:

 val configur = new java.util.HashMap[String, String]

    configur.put("cluster.name", "cluster")

    configur.put("bulk.flush.max.actions", "1000")

    val transportAddresses = new java.util.ArrayList[InetSocketAddress]
    transportAddresses.add(new InetSocketAddress(InetAddress.getByName("cluster.com"), 9300))


    jsonOutput.filter(_.nonEmpty).addSink(new ElasticsearchSink(configur,
                                                                transportAddresses,
                                                                new ElasticsearchSinkFunction[String] {
      def createIndexRequest(element: String): IndexRequest = {

        val jsonMap = parse(element).values.asInstanceOf[java.util.HashMap[String, String]]

        return Requests.indexRequest()
          .index("flinkTest")
          .source(jsonMap);
      }

      override def process(element: String, ctx: RuntimeContext, indexer: RequestIndexer) {
        indexer.add(createIndexRequest(element))
      }
    }))

Flink uses the Elasticsearch Transport Client which connects using a binary protocol on port 9300. Your nginx proxy is sitting in front of the HTTP interface on port 9200. Flink 使用 Elasticsearch 传输客户端,它在端口 9300 上使用二进制协议进行连接。您的 nginx 代理位于端口 9200 上的 HTTP 接口前面。

Flink isn't going to use your proxy, so there's no need to provide authentication. Flink 不会使用您的代理,因此无需提供身份验证。

If you need to use a HTTP Client to connect Flink with Elasticsearch, one solution is to use Jest Library .如果需要使用 HTTP Client 连接 Flink 和 Elasticsearch,一种解决方案是使用Jest 库

You have to create a custom SinkFunction, like this basic java class :你必须创建一个自定义的 SinkFunction,就像这个基本的 java 类:

    package fr.gfi.keenai.streaming.io.sinks.elasticsearch5;

    import org.apache.flink.configuration.Configuration;
    import org.apache.flink.streaming.api.functions.sink.RichSinkFunction;

    import io.searchbox.client.JestClient;
    import io.searchbox.client.JestClientFactory;
    import io.searchbox.client.config.HttpClientConfig;
    import io.searchbox.core.Index;

    public class ElasticsearchJestSinkFunction<T> extends RichSinkFunction<T> {

        private static final long serialVersionUID = -7831614642918134232L;

        private JestClient client;

        @Override
        public void invoke(T value) throws Exception {

            String document = convertToJsonDocument(value); 

            Index index = new Index.Builder(document).index("YOUR_INDEX_NAME").type("YOUR_DOCUMENT_TYPE").build();
            client.execute(index);

        }

        @Override
        public void open(Configuration parameters) throws Exception {

            // Construct a new Jest client according to configuration via factory
            JestClientFactory factory = new JestClientFactory();
            factory.setHttpClientConfig(new HttpClientConfig.Builder("http://localhost:9200")
                    .multiThreaded(true)
                    // Per default this implementation will create no more than 2 concurrent
                    // connections per given route
                    .defaultMaxTotalConnectionPerRoute(2)
                    // and no more 20 connections in total
                    .maxTotalConnection(20)
                    // Basic username and password authentication
                    .defaultCredentials("YOUR_USER", "YOUR_PASSWORD")
                    .build());
            client = factory.getObject();
        }

        private String convertToJsonDocument(T value) {
            //TODO
            return "{}";
        }

    }

Note that you can also use bulk operations for more speed.请注意,您还可以使用批量操作来提高速度。

An exemple of Jest implementation for Flink is described at the part "Connecting Flink to Amazon RS" of this post这篇文章的“将 Flink 连接到 Amazon RS”部分描述了 Flink 的 Jest 实现示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM