Elasticsearch YAML connector (Beta)

Updated at:

This topic describes how to use the Elasticsearch connector as the sink of a data ingestion YAML job.

Prerequisites

  • An Elasticsearch instance is created and can communicate with the Realtime Compute for Apache Flink cluster over the network.

  • The destination index is created in Elasticsearch with a mapping configured, or the destination cluster allows indexes and mappings to be created dynamically.

Features

  • Automatic upsert by primary key

    The connector builds the document ID (_id) from the primary key values. INSERT, UPDATE, and REPLACE events overwrite the document by ID, and DELETE events remove the document by ID, which provides primary key update semantics out of the box.

  • Partial update

    After you enable partial-update.enabled, INSERT and UPDATE events write only the non-null fields carried by the event, and the other fields of an existing document remain unchanged. If the destination document does not exist, it is inserted. This suits scenarios in which the upstream subscribes to only some columns, or fields from multiple tables must be merged into one document. DELETE events are not affected by this option and are handled according to sink.delete-strategy.

  • Multiple delete strategies

    Three delete strategies are supported: ignore the delete, delete by primary key, and set non-primary-key fields to null (soft delete). Choose the one that matches your data retention requirements.

  • Index sharding suffix

    You can specify a sharding column for each table and append its value to the index name, so that one logical table is written to different indexes based on the sharding value.

Limits

  • The connector does not create indexes or mappings. Create the destination index in Elasticsearch with a suitable mapping in advance. If the destination cluster does not allow automatic index creation, writing to an index that does not exist fails.

  • The upstream table must define a primary key. Otherwise, the document ID is empty and documents cannot be overwritten or deleted correctly.

  • inflight.requests.max, buffered.requests.max, batch.size.max.bytes, and record.size.max.bytes take effect only in Elasticsearch 8. Versions 6 and 7 support only batch.size.max and buffer.time.max.ms.

  • ROW and MAP values are written as JSON strings. Field values inside a ROW are all converted to strings, and null values are written as the string "null". If downstream requires structured queries, convert the types in the upstream or in the transform module in advance.

Usage notes

  • Index name and document ID

    By default, the index name equals the upstream table name, in the form database_name.table_name. The document ID is the primary key values joined in column order with an underscore (_). For example, if the primary key is (id, region), the document ID is 1_east. Changing the primary key definition produces a different document ID for the same row, so do not change the primary key during synchronization.

  • Concurrent requests and write order

    When inflight.requests.max is greater than 1, multiple operations on the same document are not guaranteed to take effect in event order. For tables that are sensitive to update order, set this option to 1.

  • Combining partial update and soft delete

    The two options apply to different events and can be enabled together. partial-update.enabled affects only INSERT, UPDATE, and REPLACE events: it skips null values and keeps document fields that the event does not carry. sink.delete-strategy=non-pk-field-to-null affects only DELETE events: it explicitly sets all non-primary-key fields to null, is not subject to the null-skipping rule, and creates the document by doc_as_upsert if the destination document does not exist. When both are enabled, updates do not clear existing fields, but a delete on the same primary key clears all non-primary-key fields.

  • Partial update and ingest pipelines

    After partial update is enabled, writes use doc_as_upsert semantics. Elasticsearch does not support using an ingest pipeline together with doc_as_upsert, so do not configure write logic that relies on an ingest pipeline for the index.

Data ingestion

The Elasticsearch connector can be used in data ingestion YAML jobs as a sink.

Syntax

sink:
  type: elasticsearch
  name: Elasticsearch Sink
  hosts: 127.0.0.1:9200
  version: 8

Configuration options

Parameter

Description

Required

Data type

Default value

Remarks

type

The connector type.

Yes

STRING

None

Set the value to elasticsearch.

name

The sink name.

No

STRING

None

The name of the sink.

hosts

The Elasticsearch cluster addresses.

Yes

STRING

None

Separate multiple addresses with commas (,), in the format host:port.

version

The Elasticsearch major version.

Yes

INTEGER

None

Valid values: 6, 7, and 8.

username

The username.

No

STRING

None

Configure this option when security authentication is enabled.

password

The password.

No

STRING

None

Used together with username. We recommend that you reference it by using a key management variable.

batch.size.max

The maximum number of operations in a single bulk request.

No

INTEGER

500

Takes effect in versions 6, 7, and 8.

buffer.time.max.ms

The maximum wait time before an incomplete batch is flushed.

No

LONG

5000

Unit: milliseconds. Takes effect in versions 6, 7, and 8.

inflight.requests.max

The maximum number of concurrent requests.

No

INTEGER

5

Supported only in Elasticsearch 8. A value greater than 1 does not guarantee the operation order for the same document. Set it to 1 if strict order is required.

buffered.requests.max

The maximum number of requests buffered in memory.

No

INTEGER

1000

Supported only in Elasticsearch 8.

batch.size.max.bytes

The maximum number of bytes in a single bulk request.

No

LONG

5242880 (5 MiB)

Supported only in Elasticsearch 8.

record.size.max.bytes

The maximum number of bytes in a single record.

No

LONG

10485760 (10 MiB)

Supported only in Elasticsearch 8.

sharding.suffix.key

The sharding columns for index suffixes.

No

STRING

None

Separate the table and the column with a colon (:), and separate multiple tables with semicolons (;), for example, test_db.table1:region;test_db.table2:region. A table that is not configured uses the first partition key of the Flink CDC schema (partitionKeys()) as the sharding column. If there is no partition key, the index name is the table name. The table name must match the destination table name after routing.

sharding.suffix.separator

The separator between the table name and the sharding value.

No

STRING

_

The default separator is an underscore _. The separator cannot contain \, /, *, ?, ", <, or >.

partial-update.enabled

Specifies whether to enable partial update.

No

BOOLEAN

false

When disabled (default), each INSERT or UPDATE event overwrites the entire document with the full row, and columns that carry no value in the event are written as null. When enabled, null values from the source do not clear existing field values in the document, and the option cannot be used together with an Elasticsearch ingest pipeline. This option applies only to INSERT, UPDATE, and REPLACE events. DELETE events are handled according to sink.delete-strategy.

sink.delete-strategy

The strategy for handling delete events.

No

STRING

delete-row-on-pk

Valid values:

  • ignore-delete: discards delete events and keeps existing documents.

  • delete-row-on-pk: deletes the entire document by primary key. This is the default value.

  • non-pk-field-to-null: soft delete. Keeps the primary key fields and sets the other fields to null.

Data type mappings

Flink CDC data type

Form written to Elasticsearch

CHAR, VARCHAR, STRING

JSON string.

BOOLEAN

JSON boolean.

TINYINT, SMALLINT, INTEGER, BIGINT

JSON number.

FLOAT, DOUBLE

JSON number.

DECIMAL

JSON string, for example, "12.34", to avoid precision loss.

BINARY, VARBINARY, BYTES

Base64-encoded JSON string.

DATE

String in the yyyy-MM-dd format.

TIMESTAMP

String in the yyyy-MM-dd HH:mm:ss.SSSSSS format. The time zone follows the pipeline time zone configuration.

TIMESTAMP_LTZ

JSON string in the yyyy-MM-dd HH:mm:ss.SSSSSS format, accurate to microseconds, converted according to the pipeline time zone.

TIMESTAMP WITH TIME ZONE

JSON string in the yyyy-MM-dd HH:mm:ss.SSSSSS format, accurate to microseconds, converted according to the pipeline time zone.

ARRAY

JSON array.

MAP

String converted from a JSON object.

ROW

String converted from a JSON object. Field values are all converted to strings, and null values become "null".

TIME

Not supported.

Schema changes

The connector maintains the schema of each table internally. Schema change behavior is controlled by the pipeline-level option schema.change.behavior:

  • Create table event: initializes the table schema.

  • Add column, drop column, and rename column: the connector updates its internal schema and continues writing.

  • Column type change: not supported by the connector. The job fails and exits.

  • For any schema change, the connector does not modify the Elasticsearch index mapping. Whether the changed data can be written depends on whether the mapping of the destination index is compatible. Maintain the mapping on the Elasticsearch side accordingly.

Examples

Synchronize a single table

source:
  type: mysql
  name: MySQL Source
  hostname: ${secret_values.mysql.hostname}
  port: 3306
  username: ${secret_values.mysql.username}
  password: ${secret_values.mysql.password}
  tables: test_db.order
  server-id: 5401-5499
  # (Optional) Synchronize data from tables created during the incremental phase
  scan.binlog.newly-added-table.enabled: true

sink:
  type: elasticsearch
  name: Elasticsearch Sink
  hosts: ${secret_values.es.hosts}
  version: 8
  username: ${secret_values.es.username}
  password: ${secret_values.es.password}
  # (Optional) Delete documents by primary key
  sink.delete-strategy: delete-row-on-pk

pipeline:
  name: MySQL to Elasticsearch Pipeline

Synchronize an entire database

source:
  type: mysql
  name: MySQL Source
  hostname: ${secret_values.mysql.hostname}
  port: 3306
  username: ${secret_values.mysql.username}
  password: ${secret_values.mysql.password}
  tables: test_db.\.*
  server-id: 5401-5499
  # (Optional) Synchronize data from tables created during the incremental phase
  scan.binlog.newly-added-table.enabled: true

sink:
  type: elasticsearch
  name: Elasticsearch Sink
  hosts: ${secret_values.es.hosts}
  version: 8
  username: ${secret_values.es.username}
  password: ${secret_values.es.password}

pipeline:
  name: MySQL to Elasticsearch Pipeline
  parallelism: 4

Merge sharded tables

source:
  type: mysql
  name: MySQL Source
  hostname: ${secret_values.mysql.hostname}
  port: 3306
  username: ${secret_values.mysql.username}
  password: ${secret_values.mysql.password}
  tables: test_db.order_[0-9]+
  server-id: 5401-5499

sink:
  type: elasticsearch
  name: Elasticsearch Sink
  hosts: ${secret_values.es.hosts}
  version: 8

route:
  # Merge all sharded tables under test_db into the index for test_db.order
  - source-table: test_db.order_[0-9]+
    sink-table: test_db.order

pipeline:
  name: Merge Sharded Tables to Elasticsearch

Generate index suffixes by sharding column

sink:
  type: elasticsearch
  name: Elasticsearch Sink
  hosts: ${secret_values.es.hosts}
  version: 8
  # Specify a sharding column for each table
  sharding.suffix.key: test_db.order:region;test_db.payment:region
  # Index names: test_db.order_east, test_db.payment_west, and so on
  sharding.suffix.separator: _

Partial update and soft delete

sink:
  type: elasticsearch
  name: Elasticsearch Sink
  hosts: ${secret_values.es.hosts}
  version: 8
  username: ${secret_values.es.username}
  password: ${secret_values.es.password}
  # Update only the fields carried by the event and keep the other fields in the document
  partial-update.enabled: true
  # On delete, keep the primary key fields and set the other fields to null
  sink.delete-strategy: non-pk-field-to-null
  # Send requests serially when update order matters
  inflight.requests.max: 1

FAQ

Why do documents overwrite each other, or why do deletes not take effect?

This usually happens because the upstream table has no primary key, which leaves the document ID empty. Add a primary key to the upstream table and then start the job.

Why are old field values not cleared after partial update is enabled?

Partial update writes only the non-null fields carried by the event and skips null fields. To clear fields, disable partial-update.enabled. Each event then overwrites the entire document.

Why does the index name not change after a sharding suffix is configured?

Check whether the table and the column in sharding.suffix.key are separated by a colon (:), and whether the table name matches the destination table name after routing. A table that is not configured falls back to the first partition key of the schema (partitionKeys()) as the sharding column, and if there is no partition key, the index name is the table name. When partial update is enabled, also make sure that the event carries the sharding column.

Why do concurrency options have no effect on an Elasticsearch 7 cluster?

inflight.requests.max, buffered.requests.max, batch.size.max.bytes, and record.size.max.bytes take effect only in version 8. For versions 6 and 7, tune the write batch by using batch.size.max and buffer.time.max.ms.