Skip to main content
Create the Kafka table with kafka_handle_error_mode = 'stream'. A message that cannot be parsed then no longer stops consumption: it arrives as a row whose virtual columns _raw_message and _error hold the original message and the parsing error. One materialized view inserts the rows without an error into your table, and a second one writes the failed messages to another Kafka topic through a second Kafka table.

Setup

The examples use a broker at kafka:9092, a source topic events with JSON messages, and a dead-letter topic events_dead_letter.
  1. Create the table that consumes the source topic, with the error mode set to stream:
  1. Stop the table before you attach a view to it:
A Kafka table starts consuming as soon as its first materialized view exists. Without this step, the view of step 3 alone would consume the messages that wait in the topic or arrive during the setup: its filter drops a malformed message, the offset is committed, and the dead-letter view of step 5 never receives that message.
  1. Create the destination table and a materialized view that keeps only the rows that parsed:
  1. Create a second Kafka table on the dead-letter topic. It is used only for writing: every row inserted into it is produced as one message in the format of the table.
  1. Create the materialized view that sends the failed messages to it, together with their position in the source topic:
  1. Start consumption, now that both views exist:
SYSTEM STOP and SYSTEM START work for Kafka tables from ClickHouse 26.8. See Managing background activity. The stopped state is not kept across a server restart. On an earlier version, leave out steps 2 and 6. Consumption then starts with the view of step 3, and a malformed message that is consumed before the view of step 5 exists does not reach the dead-letter topic. By default, a consumer group without committed offsets reads the topic from the beginning, so this includes malformed messages that were produced long before the setup.

Result

Take these two messages in the events topic:
The first one is inserted into events. The event_time value of the second one cannot be parsed as DateTime, so the message is not inserted. Instead, the events_dead_letter topic receives one JSON message with the keys topic, partition, offset, raw_message (the complete original message) and error (the text of the parsing exception). A consumer of that topic can count the failures, or repair the messages and produce them again.

How it works

  • With the default kafka_handle_error_mode = 'default', a message that cannot be parsed raises an exception and the table does not get past it, unless kafka_skip_broken_messages allows it to be skipped.
  • With stream, a failed message still produces a row. All its data columns have default values, _raw_message contains the message, and _error contains the exception message. For a message that parsed, both virtual columns are empty. This is why events_mv needs the WHERE length(_error) = 0 filter: without it, a row of default values is inserted for every failed message.
  • _raw_message and _error exist only when the table is created with kafka_handle_error_mode = 'stream'. Otherwise the views fail to be created with Unknown expression identifier.
  • Only parsing errors are reported this way. A JSON message with a missing or an unknown key is not an error: the missing columns get default values.
  • The Kafka table engine has at-least-once delivery, so a failed message can occasionally be written to the dead-letter topic more than once. The topic, partition and offset fields identify the original message.
  • A view receives only the messages that are consumed after it was created. This is why the setup stops the Kafka table until both views exist.

Alternatives

  • To keep the failed messages in ClickHouse instead of Kafka, point the second materialized view at a MergeTree table with the same five columns.
  • kafka_handle_error_mode = 'dead_letter_queue' needs no materialized view for the errors: ClickHouse writes the failed messages to the system.dead_letter_queue table. The table must be enabled with the dead_letter_queue section of the server configuration, otherwise CREATE TABLE fails. In this mode a failed message produces no row, so the main view needs no filter.