Home / Data Platform / Streaming & CDC

Data PlatformCheat sheetIntermediate

Kafka Connect single message transforms (SMT) cheat sheet: routing, field changes, headers, predicates and the errors they throw

Working configs for the built-in Kafka Connect SMTs, how to apply them only to some records with predicates, and fixes for the DataExceptions they raise.

Deepak Bhardwaj7 min read

Applies toApache Kafka Connect 2.6+ (predicates and Filter), 3.x and 4.x; Confluent Platform; Debezium 2.x/3.x

How SMTs are wired

Single message transforms change each record as it passes through a connector: after a source connector produces it, or before a sink connector writes it. You declare a chain of aliases in transforms, and they run in that order.

"transforms": "route,addTs",
"transforms.route.type": "org.apache.kafka.connect.transforms.RegexRouter",
"transforms.route.regex": "dbserver1\\.inventory\\.(.*)",
"transforms.route.replacement": "cdc.$1",
"transforms.addTs.type": "org.apache.kafka.connect.transforms.InsertField$Value",
"transforms.addTs.timestamp.field": "ingested_at"

Rules that cause most mistakes:

  • Order matters. A transform sees the output of the one before it. If you route first, later predicates match the new topic name.
  • Many transforms come in $Key and $Value variants. Pick the one for the part of the record you mean.
  • Transforms run on the converted Connect data. A StringConverter value is a plain string, so field-level transforms fail on it.
  • SMTs are for light, per-record changes. Joins, lookups and aggregations belong in a stream processor or downstream.

Check which transforms your worker actually has (the connectorsOnly parameter needs Kafka 3.2 or later):

curl -s "http://connect:8083/connector-plugins?connectorsOnly=false" \
  | jq -r '.[] | select(.type=="transformation" or .type=="predicate") | .class'

Built-in transforms quick reference

All classes are in org.apache.kafka.connect.transforms unless noted.

Need Transform Key settings
Rename topics with a regex RegexRouter regex, replacement
Topic per day or hour TimestampRouter topic.format (${topic}-${timestamp}), timestamp.format
Add topic, partition, offset, timestamp or a constant InsertField$Value / $Key topic.field, partition.field, offset.field, timestamp.field, static.field, static.value
Drop or rename fields ReplaceField$Value exclude, include, renames (old:new,old2:new2)
Blank out PII MaskField$Value fields, optional replacement
Change a field type Cast$Value spec (amount:float64,id:string)
Convert timestamps TimestampConverter$Value field, target.type (string, unix, Date, Time, Timestamp), format
Build the key from value fields ValueToKey fields
Pull one field out as the whole key or value ExtractField$Key / $Value field
Wrap a primitive in a struct HoistField$Value field
Flatten nested structs Flatten$Value delimiter (default .)
Change the schema name or version SetSchemaMetadata$Value schema.name, schema.version
Drop records Filter used with a predicate
Add a static header InsertHeader header, value.literal
Remove headers DropHeaders headers
Copy or move fields into headers HeaderFrom$Value fields, headers, operation (copy or move)

The header transforms arrived in Kafka 3.x. On older workers they are missing from the plugin list. ReplaceField used blacklist and whitelist before exclude and include; old configs may still use those names.

Common recipes

Key a sink topic by a value field

Sinks that upsert, such as JDBC sinks in upsert mode, need a key. Copy the ID into the key, then unwrap it so the key is the bare value instead of a struct:

"transforms": "mkKey,unwrapKey",
"transforms.mkKey.type": "org.apache.kafka.connect.transforms.ValueToKey",
"transforms.mkKey.fields": "customer_id",
"transforms.unwrapKey.type": "org.apache.kafka.connect.transforms.ExtractField$Key",
"transforms.unwrapKey.field": "customer_id"

Remove and mask columns before they leave the source

"transforms": "drop,mask",
"transforms.drop.type": "org.apache.kafka.connect.transforms.ReplaceField$Value",
"transforms.drop.exclude": "password_hash,internal_notes",
"transforms.mask.type": "org.apache.kafka.connect.transforms.MaskField$Value",
"transforms.mask.fields": "email,phone",
"transforms.mask.replacement": "REDACTED"

Warning

Masking in the connector only protects data from this point on. Records already written to the topic still contain the original values until retention removes them. If PII leaked, plan a topic clean-up as well.

Flatten Debezium change events for a sink

Debezium wraps rows in an envelope with before, after and source. Its own SMT unwraps it:

"transforms": "unwrap",
"transforms.unwrap.type": "io.debezium.transforms.ExtractNewRecordState",
"transforms.unwrap.add.fields": "op,source.ts_ms"

Delete handling options changed between Debezium releases (delete.handling.mode and drop.tombstones were replaced by delete.tombstone.handling.mode in newer versions). Check the documentation for your Debezium version before copying settings.

Format a timestamp for a sink that wants strings

"transforms": "ts",
"transforms.ts.type": "org.apache.kafka.connect.transforms.TimestampConverter$Value",
"transforms.ts.field": "updated_at",
"transforms.ts.target.type": "string",
"transforms.ts.format": "yyyy-MM-dd HH:mm:ss"

format uses Java SimpleDateFormat patterns.

Predicates: apply a transform to only some records

Since Kafka 2.6, any transform can be made conditional with a predicate. Built-in predicates are in org.apache.kafka.connect.transforms.predicates:

Predicate Matches when Setting
TopicNameMatches Topic name matches a regex pattern
HasHeaderKey Record has a header with this name name
RecordIsTombstone Value is null none

Drop heartbeat records and only add a field to the orders topic:

"transforms": "dropHb,addSrc",
"transforms.dropHb.type": "org.apache.kafka.connect.transforms.Filter",
"transforms.dropHb.predicate": "isHeartbeat",
"transforms.addSrc.type": "org.apache.kafka.connect.transforms.InsertField$Value",
"transforms.addSrc.static.field": "source_system",
"transforms.addSrc.static.value": "erp",
"transforms.addSrc.predicate": "isOrders",
"predicates": "isHeartbeat,isOrders",
"predicates.isHeartbeat.type": "org.apache.kafka.connect.transforms.predicates.TopicNameMatches",
"predicates.isHeartbeat.pattern": "__debezium-heartbeat.*",
"predicates.isOrders.type": "org.apache.kafka.connect.transforms.predicates.TopicNameMatches",
"predicates.isOrders.pattern": ".*\\.orders"

Add "transforms.<alias>.negate": "true" to apply the transform when the predicate does not match. The usual use is skipping tombstones:

"transforms.addSrc.predicate": "isTombstone",
"transforms.addSrc.negate": "true",
"predicates.isTombstone.type": "org.apache.kafka.connect.transforms.predicates.RecordIsTombstone"

Caution

Filter drops records permanently. They are not sent to the dead letter queue. Test the predicate on a copy of the connector writing to a scratch topic before using it in production.

Errors and fixes

Error in task trace Cause Fix
Only Map objects supported in absence of schema for [field insertion], found: java.lang.String Value is a plain string (StringConverter, or JSON stored as text) Use a converter that produces structured data (JsonConverter, Avro, Protobuf)
Only Struct objects supported for [...] Schema'd record whose value is not a struct, often a primitive Wrap it with HoistField$Value first, or use the $Key variant if the field is in the key
NullPointerException or DataException on deletes Transform received a tombstone (null value) Add a RecordIsTombstone predicate with negate=true
Unknown field: <name> / field not found Wrong field name, or an earlier transform already renamed or removed it Check transform order; names are case-sensitive
ClassNotFoundException or Invalid value ... for configuration transforms.x.type Typo in the class (missing $Value), or plugin not on plugin.path Check the plugin list on every worker
Records end up on an unexpected topic Router ran before a topic-based predicate, or a regex did not match Move the router last; test the regex against real topic names

Validate a config before deploying it. The endpoint checks the connector and the transform settings and returns errors per field:

curl -s -X PUT -H "Content-Type: application/json" \
  --data @orders-sink.json \
  http://connect:8083/connector-plugins/io.confluent.connect.jdbc.JdbcSinkConnector/config/validate \
  | jq '.configs[] | select(.value.errors | length > 0) | {name: .value.name, errors: .value.errors}'

The body must be the flat config object, including connector.class, not the {"name":..., "config":...} wrapper.

Find the failing transform in a task's stack trace:

curl -s http://connect:8083/connectors/orders-sink/status | jq -r '.tasks[] | select(.state=="FAILED") | .trace' | head -30

If you want bad records sent to a dead letter queue instead of stopping the task, errors.tolerance=all also covers exceptions thrown in transforms. For sink connectors, add errors.deadletterqueue.topic.name and errors.deadletterqueue.context.headers.enable=true so each DLQ record carries the reason.

Prevent it next time

  • Keep transform chains short and documented in the connector config's repository, not edited live with curl.
  • Run config/validate in CI for every connector change.
  • Put routers last and add a tombstone predicate to any field-level transform on a CDC stream.
  • Check the worker plugin list after every Connect upgrade; renamed settings and missing plugins show up there first.