Kafka Connect
Single Message Transform
Custom Coding
Data Streaming
Programming Guide

Write a custom Kafka connect single message transform

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Apache Kafka is a widely-used distributed event streaming platform capable of handling trillions of events a day. Kafka Connect, a component of Kafka, simplifies the integration of Kafka with other systems like databases, key-value stores, search indexes, and file systems. Using Kafka Connect, you can stream data into and out of Kafka without writing code. One of the powerful features of Kafka Connect is the ability to transform messages in-flight using Single Message Transforms (SMTs).

What is a Single Message Transform (SMT)?

Single Message Transforms are configurations applied to Kafka Connect to modify the data as it flows through Connect. They are particularly useful for simple transformations such as modifying a field, filtering out unwanted data, routing to specific topics, or adding new data fields. SMTs are executed in the Kafka Connect framework and can be configured per connector.

Implementing a Custom SMT

Although Kafka comes with a set of built-in transformations, specific use-cases might require custom SMT implementations. Below is an overview of how to create a custom SMT.

1. Setting up the Development Environment

First, ensure you have the Kafka environment and an IDE set up for Java development (Kafka Connect is written in Java). You will also need Maven or Gradle to manage your dependencies.

2. Creating the Custom Transform Class

The core of a custom SMT lies in implementing the Transformation interface provided by Kafka. Here’s a simple example that adds a new field to Kafka messages:

java
1import org.apache.kafka.common.config.ConfigDef;
2import org.apache.kafka.connect.connector.ConnectRecord;
3import org.apache.kafka.connect.transforms.Transformation;
4import java.util.Map;
5
6public class AddField<R extends ConnectRecord<R>> implements Transformation<R> {
7
8  public static final String NEW_FIELD_CONFIG = "new.field";
9  private String newFieldValue;
10
11  @Override
12  public void configure(Map<String, ?> configs) {
13    newFieldValue = configs.get(NEW_FIELD_CONFIG).toString();
14  }
15
16  @Override
17  public R apply(R record) {
18    // Create a new record by adding a field
19    return record.newRecord(
20      record.topic(), record.kafkaPartition(),
21      record.keySchema(), record.key(),
22      record.valueSchema(), record.value(),
23      record.timestamp(),
24      UpdatedValue.withNewField(record.value(), newFieldValue)
25    );
26  }
27
28  @Override
29  public ConfigDef config() {
30    return new ConfigDef().define(NEW_FIELD_CONFIG, ConfigDef.Type.STRING, "default", ConfigDef.Importance.HIGH, "Field to add");
31  }
32
33  @Override
34  public void close() {
35    // Nothing to close in this example
36  }
37}

This simple transform adds a new field to the message with a value that is configurable.

3. Packaging and Deploying

Package your transform into a JAR and place it in the share/java directory of your Kafka installation, or wherever your Kafka Connect is configured to pick up plugin libraries.

4. Configuring the Connector

You configure the SMT in the connector configuration. Here's an example configuration snippet for the connector:

json
"transforms": "AddField",
"transforms.AddField.type": "com.example.AddField",
"transforms.AddField.new.field": "NewValue"

Summary Table

Here is a quick look at key points related to creating a custom SMT:

AspectDetail
InterfaceTransformation<R extends ConnectRecord<R>>
Configuration OptionsSpecified in config() method
Execution Methodapply(R record) - Logic for transformation
PackagingJAR file
DeploymentKafka Connect’s share/java directory
UsageConfigured per connector

Additional Notes

  • Testing: Always thoroughly test your SMT before deploying it in a production environment. Consider using mock data and Kafka's testing tools.
  • Performance: SMTs should be efficient since they affect the throughput of your Kafka Connect tasks. Optimize your transformations for minimal overhead.
  • Complex Transformations: For more complex transformations that might involve external systems or complex logic, consider whether a separate Kafka Streams application might be more appropriate.

By creating a custom SMT, developers can extend Kafka Connect to perform specialized transformations that cater to the unique needs of their data flows.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.