Apache Kafka & Stream Processing Fundamentals · บทเรียน

การแปลงข้อความเดี่ยว (SMTs)

เรียนรู้ว่า Kafka Connect ใช้การแปลงข้อความเดี่ยวเพื่อปรับรูปเรกคอร์ดระหว่างตัวเชื่อมต่อกับ Kafka ได้ทันที โดยไม่ต้องเขียนตัวประมวลผลกระแสข้อมูลอย่างไร

บทเรียน 4 จาก 413 ขั้นตอน

การแปลงข้อความเดี่ยว (SMTs) เป็นบทเรียน Apache Kafka & Stream Processing Fundamentals ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Apache Kafka & Stream Processing Fundamentals และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Apache Kafka & Stream Processing Fundamentals มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

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.
เริ่มต้นได้ฟรี

เรียนรู้ Apache Kafka & Stream Processing Fundamentals ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

คำถามที่พบบ่อย

บทเรียน “การแปลงข้อความเดี่ยว (SMTs)” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การแปลงข้อความเดี่ยว (SMTs)” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Apache Kafka & Stream Processing Fundamentals ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Apache Kafka & Stream Processing Fundamentals มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การแปลงข้อความเดี่ยว (SMTs)”

เรียนรู้ว่า Kafka Connect ใช้การแปลงข้อความเดี่ยวเพื่อปรับรูปเรกคอร์ดระหว่างตัวเชื่อมต่อกับ Kafka ได้ทันที โดยไม่ต้องเขียนตัวประมวลผลกระแสข้อมูลอย่างไร คุณปฏิบัติ Apache Kafka & Stream Processing Fundamentals ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Apache Kafka & Stream Processing Fundamentals หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Apache Kafka & Stream Processing Fundamentals บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การแปลงข้อความเดี่ยว (SMTs)” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Apache Kafka & Stream Processing Fundamentals นี้ได้ไหม

ได้ บทเรียน Apache Kafka & Stream Processing Fundamentals ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. บทนำสู่ Kafka Connect
  2. ตัวเชื่อมต่อแหล่งข้อมูลสำหรับการนำเข้า
  3. ตัวเชื่อมต่อปลายทางสำหรับการส่งออก
  4. การแปลงข้อความเดี่ยว (SMTs)
← กลับไปที่ Apache Kafka & Stream Processing Fundamentals