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 atkafka:9092, a source topic events with JSON messages, and a dead-letter topic events_dead_letter.
- Create the table that consumes the source topic, with the error mode set to
stream:
- Stop the table before you attach a view to it:
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.
- Create the destination table and a materialized view that keeps only the rows that parsed:
- Create a second
Kafkatable 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.
- Create the materialized view that sends the failed messages to it, together with their position in the source topic:
- 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 theevents topic:
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, unlesskafka_skip_broken_messagesallows it to be skipped. - With
stream, a failed message still produces a row. All its data columns have default values,_raw_messagecontains the message, and_errorcontains the exception message. For a message that parsed, both virtual columns are empty. This is whyevents_mvneeds theWHERE length(_error) = 0filter: without it, a row of default values is inserted for every failed message. _raw_messageand_errorexist only when the table is created withkafka_handle_error_mode = 'stream'. Otherwise the views fail to be created withUnknown 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
Kafkatable engine has at-least-once delivery, so a failed message can occasionally be written to the dead-letter topic more than once. Thetopic,partitionandoffsetfields identify the original message. - A view receives only the messages that are consumed after it was created. This is why the setup stops the
Kafkatable until both views exist.
Alternatives
- To keep the failed messages in ClickHouse instead of Kafka, point the second materialized view at a
MergeTreetable 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 thesystem.dead_letter_queuetable. The table must be enabled with thedead_letter_queuesection of the server configuration, otherwiseCREATE TABLEfails. In this mode a failed message produces no row, so the main view needs no filter.
Related pages
Kafkatable engine, including the list of virtual columns- Using the Kafka table engine
system.dead_letter_queue