KAFKA-20709: Add HeaderRouter SMT#22870
Open
1230fahid wants to merge 1 commit into
Open
Conversation
This commit adds a built-in SMT that routes records to a destination topic based on the value of a Kafka record header. Header names are specified in priority order. The first header found on the record determines the destination topic. An optional fallback.topic routes unmatched records. If unset, records pass through with their original topic unchanged.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds a new built-in Single Message Transformation,
HeaderRouter, that routes records to a destination topic based on the value of a Kafka record header. This addresses a gap in Kafka Connect's built-in transforms where no native mechanism existed to route records using header metadata.The transform accepts an ordered list of header names (
header.names). On each record, headers are checked in priority order and the value of the first matching header is used as the destination topic name.An optional
fallback.topiccan be configured for records where none of the specified headers are present; if omitted, such records pass through with their original topic unchanged.Changes
connect/transforms/.../HeaderRouter.java: New SMT implementation. Iteratesheader.namesin priority order, resolves header values asString,byte[](decoded as UTF-8), or any other type viatoString(). Skips headers withnullor empty string values. Normalizes a blankfallback.topicto absent atconfigure()time to prevent routing records to an invalid empty topic name.connect/transforms/.../META-INF/services/org.apache.kafka.connect.transforms.Transformation: RegistersHeaderRouterfor automatic plugin discovery by the Connect runtime.connect/runtime/.../tools/TransformationDoc.java: AddsHeaderRouterto the doc generation tool so it appears in the generated HTML reference documentation (docs/generated/connect_transforms.html).Testing
Unit tests added in
HeaderRouterTest.javacovering:fallback.topicused when no configured header is present on the recordnullheader value is skipped; next header in list is triedfallback.topicconfig is treated as absent (pass-through)byte[]header values are decoded as UTF-8 for the topic nameConfigExceptionthrown whenheader.namesis missing or empty