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.
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:
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:
Summary Table
Here is a quick look at key points related to creating a custom SMT:
| Aspect | Detail |
| Interface | Transformation<R extends ConnectRecord<R>> |
| Configuration Options | Specified in config() method |
| Execution Method | apply(R record) - Logic for transformation |
| Packaging | JAR file |
| Deployment | Kafka Connect’s share/java directory |
| Usage | Configured 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
- Write parquet from AWS Kinesis firehose to AWS S3
- Write to two Kafka topics in a single transaction using Spring Kafka
- Writing Custom Kafka Serializer
- Writing JUnit tests for Kafka Consumer
- Writing large DataFrame from PySpark to Kafka runs into timeout
- Writing logs to log file as well as kafka
- WSO2 SP - Kafka source with JSON attributes
- ZeroMQ - Handling slow receivers without dropping

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.