Apache Kafka & Stream Processing Fundamentals · Lezione

Single Message Transforms (SMT)

Scopra come le Single Message Transforms di Kafka Connect modifichino i record inline tra connettori e Kafka senza dover scrivere un processore di flussi.

Lezione 4 di 413 passaggi

Single Message Transforms (SMT) è una lezione Apache Kafka & Stream Processing Fundamentals gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Apache Kafka & Stream Processing Fundamentals, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Apache Kafka & Stream Processing Fundamentals include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

What Are SMTs?

Single Message Transforms (SMTs) are lightweight functions applied to each record as it flows through a Kafka Connect connector.

They let you tweak data — rename fields, add metadata, route topics — without a separate processing job.

Where SMTs Run

SMTs run inside the connector worker:

  • For a source connector, after reading from the system and before writing to Kafka.
  • For a sink connector, after reading from Kafka and before writing to the target.

Configuring a Transform

Transforms are configured declaratively in the connector's JSON or properties. You name the chain, then configure each transform.

{
  "transforms": "addTs",
  "transforms.addTs.type":
    "org.apache.kafka.connect.transforms.InsertField$Value",
  "transforms.addTs.timestamp.field": "ingestedAt"
}

Common Built-in SMTs

Kafka Connect ships many useful transforms:

  • InsertField — add a static or metadata field.
  • ReplaceField — rename or drop fields.
  • MaskField — redact sensitive values.
  • ExtractField — pull one field up as the whole value.

Routing with RegexRouter

RegexRouter rewrites the destination topic name, handy for adding prefixes when ingesting many tables.

{
  "transforms": "route",
  "transforms.route.type":
    "org.apache.kafka.connect.transforms.RegexRouter",
  "transforms.route.regex": "(.*)",
  "transforms.route.replacement": "db_$1"
}

Chaining Transforms

List multiple transforms separated by commas; they run in order, each receiving the previous one's output.

{
  "transforms": "mask,route",
  "transforms.mask.type":
    "org.apache.kafka.connect.transforms.MaskField$Value",
  "transforms.mask.fields": "ssn,creditCard",
  "transforms.route.type":
    "org.apache.kafka.connect.transforms.RegexRouter",
  "transforms.route.regex": "(.*)",
  "transforms.route.replacement": "secure_$1"
}

Key vs. Value Variants

Many SMTs come in two forms, suffixed with $Key or $Value, so you can target the record's key or value independently.

For example InsertField$Key versus InsertField$Value.

Predicates

Predicates let a transform apply conditionally — for example, only to tombstone records or to a specific topic.

{
  "predicates": "isTomb",
  "predicates.isTomb.type":
    "org.apache.kafka.connect.transforms.predicates.RecordIsTombstone",
  "transforms.drop.predicate": "isTomb"
}

When NOT to Use SMTs

SMTs operate on one record at a time. They cannot:

  • Join across records or topics.
  • Aggregate or window.
  • Call external services efficiently.

For those, use Kafka Streams or ksqlDB instead.

Custom SMTs

You can implement your own by writing a class that implements the Transformation interface, packaging it as a JAR, and placing it on the worker plugin path.

public class UppercaseField
    implements Transformation<SourceRecord> {
  public SourceRecord apply(SourceRecord record) {
    // reshape and return the record
    return record;
  }
}

Best Practices

Use SMTs wisely:

  • Keep chains short and readable.
  • Mask sensitive data as early as possible.
  • Prefer predicates over per-topic connectors when filtering.
  • Move heavy logic to a stream processor.

Quick Check

Test your understanding of SMTs.

Recap

You learned about Single Message Transforms.

  • SMTs reshape each record inline within a connector.
  • Use built-ins like InsertField, ReplaceField, MaskField, RegexRouter.
  • Chain them and gate with predicates.
  • For joins, aggregations, or windows, use a stream processor instead.
Gratis per iniziare

Impara Apache Kafka & Stream Processing Fundamentals con un tutor IA — gratis

Scrivi ed esegui vero codice nel tuo browser, ricevi aiuto istantaneo da un tutor IA disponibile 24/7, e riprendi da dove hai lasciato sul web o nell'app.

Corsi
12
Lezioni
48

Domande Frequenti

La lezione «Single Message Transforms (SMT)» è gratuita?

Sì — il testo completo di «Single Message Transforms (SMT)» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Apache Kafka & Stream Processing Fundamentals, passa a CoddyKit PRO. Il corso Apache Kafka & Stream Processing Fundamentals include 4 lezioni in totale.

Cosa imparerò in «Single Message Transforms (SMT)»?

Scopra come le Single Message Transforms di Kafka Connect modifichino i record inline tra connettori e Kafka senza dover scrivere un processore di flussi. Eserciti Apache Kafka & Stream Processing Fundamentals con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Apache Kafka & Stream Processing Fundamentals?

Non è richiesta alcuna esperienza precedente. Apache Kafka & Stream Processing Fundamentals su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.

Quanto tempo richiede la lezione «Single Message Transforms (SMT)»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Apache Kafka & Stream Processing Fundamentals?

Sì. Ogni lezione Apache Kafka & Stream Processing Fundamentals include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Introduzione a Kafka Connect
  2. Connettori source per l'acquisizione
  3. Connettori sink per l'esportazione
  4. Single Message Transforms (SMT)
← Torna a Apache Kafka & Stream Processing Fundamentals