Kafka connector

The Kafka connector reads records from Apache Kafka topics and writes records to them. This release includes Kafka connector 5.0.0, which is built for Flink 2.3.0.

Connector types

The connector has two types:

  • kafka reads and writes a stream of independent records. Use it for events, such as orders or sensor readings.
  • upsert-kafka reads and writes a changelog. Each record is an update of the last record with the same key, and a record with a null value deletes the key. Use it for tables whose rows change, such as a currency rate table. An upsert-kafka table must have a primary key.

To connect to a Kafka cluster that needs authentication, see Configuring Kafka authentication. To register the Kafka cluster once and reuse it for several tables, see Adding a Kafka data source.

The following example creates a table for a Kafka topic with JSON records:

CREATE TABLE orders (
  order_id STRING,
  price DECIMAL(10, 2),
  currency STRING,
  order_time TIMESTAMP(3),
  WATERMARK FOR order_time AS order_time - INTERVAL '5' SECOND
) WITH (
  'connector' = 'kafka',
  'properties.bootstrap.servers' = '<broker-1>:9093,<broker-2>:9093',
  'topic' = 'orders',
  'scan.startup.mode' = 'earliest-offset',
  'format' = 'json'
);

The following example creates an upsert-kafka table:

CREATE TABLE currency_rates (
  currency STRING,
  conversion_rate DECIMAL(10, 4),
  update_time TIMESTAMP(3),
  WATERMARK FOR update_time AS update_time,
  PRIMARY KEY (currency) NOT ENFORCED
) WITH (
  'connector' = 'upsert-kafka',
  'properties.bootstrap.servers' = '<broker-1>:9093,<broker-2>:9093',
  'topic' = 'currency_rates',
  'key.format' = 'json',
  'value.format' = 'json'
);

Mandatory properties

Table 1. Mandatory properties of the Kafka connector
PropertyTypesDescription
connectorBothkafka or upsert-kafka
properties.bootstrap.serversBothThe Kafka brokers, separated by commas. There is no default value.
topicBothThe topic to read from or write to. For a source, you can list several topics, separated by semicolons. For a source, specify either topic or topic-pattern.
format or value.formatkafkaThe format of the value of a record. Specify one of the two.
key.format and value.formatupsert-kafkaThe formats of the key and the value of a record.

Optional properties

Table 2. Optional properties of the Kafka connector
PropertyTypesDescription
topic-patternBoth, sourceA regular expression for the topics to read. Use it instead of topic.
properties.group.idBoth, sourceThe consumer group of the source.
scan.startup.modekafkaWhere the source starts reading. The default is group-offsets. The other values are earliest-offset, latest-offset, timestamp, and specific-offsets.
scan.startup.timestamp-milliskafkaThe start time in epoch milliseconds, for the timestamp mode.
scan.startup.specific-offsetskafkaThe start offsets for each partition, for the specific-offsets mode.
scan.bounded.modekafkaWhere the source stops reading. The default is unbounded.
scan.topic-partition-discovery.intervalBoth, sourceHow often the source checks for new partitions.
key.fieldskafkaThe columns that make up the key of a record.
key.formatkafkaThe format of the key of a record. Set it together with key.fields.
value.fields-includekafkaWhether the value of a record also contains the key columns. The default is ALL.
sink.partitionerBoth, sinkHow a sink assigns records to partitions.
sink.delivery-guaranteeBoth, sinkThe default is at-least-once. The other values are none and exactly-once.
sink.transactional-id-prefixBoth, sinkThe prefix of the transaction names. It is required for exactly-once.
sink.buffer-flush.max-rowsupsert-kafka, sinkThe number of rows after which the sink writes its buffer.
sink.buffer-flush.intervalupsert-kafka, sinkThe time after which the sink writes its buffer.
deserialization.failure.policyBoth, sourceWhat happens to a record that cannot be read. The default is fail. The other values are ignore, ignore_and_log, and dlq.
deserialization.failure.dlq.topicBoth, sourceThe topic that receives the failed records when the policy is dlq.

For the full list of options, see Apache Kafka SQL connector in the Apache Flink documentation.

Delivery guarantee and start position

  • Set the delivery guarantee of a sink with sink.delivery-guarantee.
  • A job reads from the offsets of its consumer group only when you set scan.startup.mode to group-offsets and the group already has offsets. Otherwise, set an explicit startup mode.
  • The Kafka table editor sets scan.startup.mode to earliest-offset unless you choose End of topic. A table that you create with DDL uses group-offsets unless you set the option. When you set a consumer group ID (group.id) in the editor, the editor switches the start position of the table to group-offsets.

To manage records that cannot be read, see Handling deserialization errors and Setting the deserialization policy in DDL.