Elasticsearch Connector

This connector provides sinks that can request document actions to an Elasticsearch Index. To use this connector, add one of the following dependencies to your project, depending on the version of the Elasticsearch installation:

Elasticsearch versionMaven Dependency
6.x
  1. <dependency>
  2. <groupId>org.apache.flink</groupId>
  3. <artifactId>flink-connector-elasticsearch6</artifactId>
  4. <version>1.15.0</version>
  5. </dependency>
Copied to clipboard!
7.x
  1. <dependency>
  2. <groupId>org.apache.flink</groupId>
  3. <artifactId>flink-connector-elasticsearch7</artifactId>
  4. <version>1.15.0</version>
  5. </dependency>
Copied to clipboard!

Note that the streaming connectors are currently not part of the binary distribution. See here for information about how to package the program with the libraries for cluster execution.

Installing Elasticsearch

Instructions for setting up an Elasticsearch cluster can be found here.

Elasticsearch Sink

The example below shows how to configure and create a sink:

Java

Elasticsearch 6:

  1. import org.apache.flink.api.common.functions.MapFunction;
  2. import org.apache.flink.connector.elasticsearch.sink.Elasticsearch6SinkBuilder;
  3. import org.apache.flink.streaming.api.datastream.DataStream;
  4. import org.apache.http.HttpHost;
  5. import org.elasticsearch.action.index.IndexRequest;
  6. import org.elasticsearch.client.Requests;
  7. import java.util.HashMap;
  8. import java.util.Map;
  9. DataStream<String> input = ...;
  10. input.sinkTo(
  11. new Elasticsearch6SinkBuilder<String>()
  12. .setBulkFlushMaxActions(1) // Instructs the sink to emit after every element, otherwise they would be buffered
  13. .setHosts(new HttpHost("127.0.0.1", 9200, "http"))
  14. .setEmitter(
  15. (element, context, indexer) ->
  16. indexer.add(createIndexRequest(element)))
  17. .build());
  18. private static IndexRequest createIndexRequest(String element) {
  19. Map<String, Object> json = new HashMap<>();
  20. json.put("data", element);
  21. return Requests.indexRequest()
  22. .index("my-index")
  23. .type("my-type")
  24. .id(element)
  25. .source(json);
  26. }

Elasticsearch 7:

  1. import org.apache.flink.api.common.functions.MapFunction;
  2. import org.apache.flink.connector.elasticsearch.sink.Elasticsearch7SinkBuilder;
  3. import org.apache.flink.streaming.api.datastream.DataStream;
  4. import org.apache.http.HttpHost;
  5. import org.elasticsearch.action.index.IndexRequest;
  6. import org.elasticsearch.client.Requests;
  7. import java.util.HashMap;
  8. import java.util.Map;
  9. DataStream<String> input = ...;
  10. input.sinkTo(
  11. new Elasticsearch7SinkBuilder<String>()
  12. .setBulkFlushMaxActions(1) // Instructs the sink to emit after every element, otherwise they would be buffered
  13. .setHosts(new HttpHost("127.0.0.1", 9200, "http"))
  14. .setEmitter(
  15. (element, context, indexer) ->
  16. indexer.add(createIndexRequest(element)))
  17. .build());
  18. private static IndexRequest createIndexRequest(String element) {
  19. Map<String, Object> json = new HashMap<>();
  20. json.put("data", element);
  21. return Requests.indexRequest()
  22. .index("my-index")
  23. .id(element)
  24. .source(json);
  25. }

Scala

Elasticsearch 6:

  1. import org.apache.flink.api.connector.sink.SinkWriter
  2. import org.apache.flink.connector.elasticsearch.sink.{Elasticsearch6SinkBuilder, RequestIndexer}
  3. import org.apache.flink.streaming.api.datastream.DataStream
  4. import org.apache.http.HttpHost
  5. import org.elasticsearch.action.index.IndexRequest
  6. import org.elasticsearch.client.Requests
  7. val input: DataStream[String] = ...
  8. input.sinkTo(
  9. new Elasticsearch6SinkBuilder[String]
  10. .setBulkFlushMaxActions(1) // Instructs the sink to emit after every element, otherwise they would be buffered
  11. .setHosts(new HttpHost("127.0.0.1", 9200, "http"))
  12. .setEmitter((element: String, context: SinkWriter.Context, indexer: RequestIndexer) =>
  13. indexer.add(createIndexRequest(element)))
  14. .build())
  15. def createIndexRequest(element: (String)): IndexRequest = {
  16. val json = Map(
  17. "data" -> element.asInstanceOf[AnyRef]
  18. )
  19. Requests.indexRequest.index("my-index").`type`("my-type").source(mapAsJavaMap(json))
  20. }

Elasticsearch 7:

  1. import org.apache.flink.api.connector.sink.SinkWriter
  2. import org.apache.flink.connector.elasticsearch.sink.{Elasticsearch7SinkBuilder, RequestIndexer}
  3. import org.apache.flink.streaming.api.datastream.DataStream
  4. import org.apache.http.HttpHost
  5. import org.elasticsearch.action.index.IndexRequest
  6. import org.elasticsearch.client.Requests
  7. val input: DataStream[String] = ...
  8. input.sinkTo(
  9. new Elasticsearch7SinkBuilder[String]
  10. .setBulkFlushMaxActions(1) // Instructs the sink to emit after every element, otherwise they would be buffered
  11. .setHosts(new HttpHost("127.0.0.1", 9200, "http"))
  12. .setEmitter((element: String, context: SinkWriter.Context, indexer: RequestIndexer) =>
  13. indexer.add(createIndexRequest(element)))
  14. .build())
  15. def createIndexRequest(element: (String)): IndexRequest = {
  16. val json = Map(
  17. "data" -> element.asInstanceOf[AnyRef]
  18. )
  19. Requests.indexRequest.index("my-index").source(mapAsJavaMap(json))
  20. }

Note that the example only demonstrates performing a single index request for each incoming element. Generally, the ElasticsearchEmitter can be used to perform requests of different types (ex., DeleteRequest, UpdateRequest, etc.).

Internally, each parallel instance of the Flink Elasticsearch Sink uses a BulkProcessor to send action requests to the cluster. This will buffer elements before sending them in bulk to the cluster. The BulkProcessor executes bulk requests one at a time, i.e. there will be no two concurrent flushes of the buffered actions in progress.

Elasticsearch Sinks and Fault Tolerance

With Flink’s checkpointing enabled, the Flink Elasticsearch Sink guarantees at-least-once delivery of action requests to Elasticsearch clusters. It does so by waiting for all pending action requests in the BulkProcessor at the time of checkpoints. This effectively assures that all requests before the checkpoint was triggered have been successfully acknowledged by Elasticsearch, before proceeding to process more records sent to the sink.

More details on checkpoints and fault tolerance are in the fault tolerance docs.

To use fault tolerant Elasticsearch Sinks, checkpointing of the topology needs to be enabled at the execution environment:

Java

  1. final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
  2. env.enableCheckpointing(5000); // checkpoint every 5000 msecs

Scala

Elasticsearch 6:

  1. val env = StreamExecutionEnvironment.getExecutionEnvironment()
  2. env.enableCheckpointing(5000) // checkpoint every 5000 msecs

IMPORTANT: Checkpointing is not enabled by default but the default delivery guarantee is AT_LEAST_ONCE. This causes the sink to buffer requests until it either finishes or the BulkProcessor flushes automatically. By default, the BulkProcessor will flush after 1000 added Actions. To configure the processor to flush more frequently, please refer to the BulkProcessor configuration section.

Using UpdateRequests with deterministic ids and the upsert method it is possible to achieve exactly-once semantics in Elasticsearch when AT_LEAST_ONCE delivery is configured for the connector.

Handling Failing Elasticsearch Requests

Elasticsearch action requests may fail due to a variety of reasons, including temporarily saturated node queue capacity or malformed documents to be indexed. The Flink Elasticsearch Sink allows the user to retry requests by specifying a backoff-policy.

Below is an example:

Java

Elasticsearch 6:

  1. DataStream<String> input = ...;
  2. input.sinkTo(
  3. new Elasticsearch6SinkBuilder<String>()
  4. .setHosts(new HttpHost("127.0.0.1", 9200, "http"))
  5. .setEmitter(
  6. (element, context, indexer) ->
  7. indexer.add(createIndexRequest(element)))
  8. // This enables an exponential backoff retry mechanism, with a maximum of 5 retries and an initial delay of 1000 milliseconds
  9. .setBulkFlushBackoffStrategy(FlushBackoffType.EXPONENTIAL, 5, 1000)
  10. .build());

Elasticsearch 7:

  1. DataStream<String> input = ...;
  2. input.sinkTo(
  3. new Elasticsearch7SinkBuilder<String>()
  4. .setHosts(new HttpHost("127.0.0.1", 9200, "http"))
  5. .setEmitter(
  6. (element, context, indexer) ->
  7. indexer.add(createIndexRequest(element)))
  8. // This enables an exponential backoff retry mechanism, with a maximum of 5 retries and an initial delay of 1000 milliseconds
  9. .setBulkFlushBackoffStrategy(FlushBackoffType.EXPONENTIAL, 5, 1000)
  10. .build());

Scala

Elasticsearch 6:

  1. val input: DataStream[String] = ...
  2. input.sinkTo(
  3. new Elasticsearch6SinkBuilder[String]
  4. .setHosts(new HttpHost("127.0.0.1", 9200, "http"))
  5. .setEmitter((element: String, context: SinkWriter.Context, indexer: RequestIndexer) =>
  6. indexer.add(createIndexRequest(element)))
  7. // This enables an exponential backoff retry mechanism, with a maximum of 5 retries and an initial delay of 1000 milliseconds
  8. .setBulkFlushBackoffStrategy(FlushBackoffType.EXPONENTIAL, 5, 1000)
  9. .build())

Elasticsearch 7:

  1. val input: DataStream[String] = ...
  2. input.sinkTo(
  3. new Elasticsearch7SinkBuilder[String]
  4. .setHosts(new HttpHost("127.0.0.1", 9200, "http"))
  5. .setEmitter((element: String, context: SinkWriter.Context, indexer: RequestIndexer) =>
  6. indexer.add(createIndexRequest(element)))
  7. // This enables an exponential backoff retry mechanism, with a maximum of 5 retries and an initial delay of 1000 milliseconds
  8. .setBulkFlushBackoffStrategy(FlushBackoffType.EXPONENTIAL, 5, 1000)
  9. .build())

The above example will let the sink re-add requests that failed due to resource constrains (e.g. queue capacity saturation). For all other failures, such as malformed documents, the sink will fail. If no BulkFlushBackoffStrategy (or FlushBackoffType.NONE) is configured, the sink will fail for any kind of error.

IMPORTANT: Re-adding requests back to the internal BulkProcessor on failures will lead to longer checkpoints, as the sink will also need to wait for the re-added requests to be flushed when checkpointing. For example, when using FlushBackoffType.EXPONENTIAL, checkpoints will need to wait until Elasticsearch node queues have enough capacity for all the pending requests, or until the maximum number of retries has been reached.

Configuring the Internal Bulk Processor

The internal BulkProcessor can be further configured for its behaviour on how buffered action requests are flushed, by using the following methods of the Elasticsearch6SinkBuilder:

  • setBulkFlushMaxActions(int numMaxActions): Maximum amount of actions to buffer before flushing.
  • setBulkFlushMaxSizeMb(int maxSizeMb): Maximum size of data (in megabytes) to buffer before flushing.
  • setBulkFlushInterval(long intervalMillis): Interval at which to flush regardless of the amount or size of buffered actions.

Configuring how temporary request errors are retried is also supported:

  • setBulkFlushBackoffStrategy(FlushBackoffType flushBackoffType, int maxRetries, long delayMillis): The type of backoff delay, either CONSTANT or EXPONENTIAL, the amount of backoff retries to attempt, the amount of delay for backoff. For constant backoff, this is simply the delay between each retry. For exponential backoff, this is the initial base delay.

More information about Elasticsearch can be found here.

Packaging the Elasticsearch Connector into an Uber-Jar

For the execution of your Flink program, it is recommended to build a so-called uber-jar (executable jar) containing all your dependencies (see here for further information).

Alternatively, you can put the connector’s jar file into Flink’s lib/ folder to make it available system-wide, i.e. for all job being run.

Back to top