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:
kafkareads and writes a stream of independent records. Use it for events, such as orders or sensor readings.upsert-kafkareads 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. Anupsert-kafkatable 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
| Property | Types | Description |
|---|---|---|
connector | Both | kafka or upsert-kafka |
properties.bootstrap.servers | Both | The Kafka brokers, separated by commas. There is no default value. |
topic | Both | The 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.format | kafka | The format of the value of a record. Specify one of the two. |
key.format and value.format | upsert-kafka | The formats of the key and the value of a record. |
Optional properties
| Property | Types | Description |
|---|---|---|
topic-pattern | Both, source | A regular expression for the topics to read. Use it instead of topic. |
properties.group.id | Both, source | The consumer group of the source. |
scan.startup.mode | kafka | Where the source starts reading. The default is group-offsets. The other values are earliest-offset, latest-offset, timestamp, and specific-offsets. |
scan.startup.timestamp-millis | kafka | The start time in epoch milliseconds, for the timestamp mode. |
scan.startup.specific-offsets | kafka | The start offsets for each partition, for the specific-offsets mode. |
scan.bounded.mode | kafka | Where the source stops reading. The default is unbounded. |
scan.topic-partition-discovery.interval | Both, source | How often the source checks for new partitions. |
key.fields | kafka | The columns that make up the key of a record. |
key.format | kafka | The format of the key of a record. Set it together with key.fields. |
value.fields-include | kafka | Whether the value of a record also contains the key columns. The default is ALL. |
sink.partitioner | Both, sink | How a sink assigns records to partitions. |
sink.delivery-guarantee | Both, sink | The default is at-least-once. The other values are none and exactly-once. |
sink.transactional-id-prefix | Both, sink | The prefix of the transaction names. It is required for exactly-once. |
sink.buffer-flush.max-rows | upsert-kafka, sink | The number of rows after which the sink writes its buffer. |
sink.buffer-flush.interval | upsert-kafka, sink | The time after which the sink writes its buffer. |
deserialization.failure.policy | Both, source | What 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.topic | Both, source | The 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.modetogroup-offsetsand the group already has offsets. Otherwise, set an explicit startup mode. - The Kafka table editor sets
scan.startup.modetoearliest-offsetunless you choose End of topic. A table that you create with DDL usesgroup-offsetsunless 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 togroup-offsets.
To manage records that cannot be read, see Handling deserialization errors and Setting the deserialization policy in DDL.
