Kafka Connect
Data Transformation
JSON Field
Long Value Extraction
Key Insertion

Kafka Connect Transformation Extract a Long value from json field and insert as key

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Apache Kafka is a distributed event streaming platform capable of handling trillions of events a day. Kafka Connect, a component of Apache Kafka, is a tool for scalably and reliably streaming data between Apache Kafka and other data systems. A common need during data integration processes involves transforming data as it flows between systems. Kafka Connect Transformations (SMTs - Single Message Transforms) are used for this purpose. One practical transformation is extracting a numeric value from a JSON payload and using it as a Kafka message key.

Understanding the Problem

Business and system requirements often necessitate the rekeying of Kafka messages as they travel through Kafka Connect pipelines. For instance, if Kafka consumes JSON messages where a specific ID field within the JSON object is more suitable as a Kafka message key, you might need to extract this ID and set it as the Kafka message key. This is particularly useful for ensuring that all messages with the same ID land on the same Kafka partition, which is pivotal for maintaining the order of messages per ID.

Solution Implementation Using Kafka Connect

Kafka Connect supports this transformation type through its configurable framework. Below is a step-by-step guide and examples to apply this transformation.

Step 1: Configuration of Kafka Connect Source or Sink

First, ensure that you have a Kafka Connect source or sink already configured to read or write data to Kafka. The configuration might be connected to databases, file systems, or other Kafka topics.

json
1{
2    "name": "jdbc-source-connector",
3    "config": {
4        "connector.class": "io.confluent.connect.jdbc.JdbcSourceConnector",
5        "topic.prefix": "example-",
6        "connection.url": "jdbc:mysql://localhost:3306/database",
7        "mode": "incrementing",
8        "incrementing.column.name": "id",
9        "topic.prefix": "jdbc-",
10        "tasks.max": "1",
11        "transforms": "ExtractField,InsertField"
12    }
13}

Step 2: Configure the ExtractField Transformation

The ExtractField transformation is used to pull out the desired field from your structured data (which is in JSON format in our case).

json
1{
2    "transforms": "extractInt",
3    "transforms.extractInt.type": "org.apache.kafka.connect.transforms.ExtractField$Key",
4    "transforms.extractInt.field": "id"
5}

In this JSON snippet:

  • "transforms": Names the transformation chain.
  • "transforms.extractInt.type": Specifies the transformation class.
  • "transforms.extractInt.field": States which field to extract from the JSON.

Step 3: Using the Extracted Field as Key

After extracting the field, you need to ensure that the populated value is reassigned as the message key.

json
1{
2    "transforms": "extractInt,makeKey",
3    "transforms.makeKey.type": "org.apache.kafka.connect.transforms.ValueToKey",
4    "transforms.makeKey.fields": "id"
5}

Here, the ValueToKey SMT is used post extraction to assign the value as the key of Kafka message.

Summary Table

FeatureDetail
Use CaseRekeying messages based on a field from the JSON payload.
Source/SinkAny Source or Sink that can ingest/output JSON (e.g., JDBC, FileStream)
Necessary TransformationsExtractField, ValueToKey
Kafka CompatibilityWorks with Kafka Connect, a component of Apache Kafka.
Configuration ComplexityMedium (requires understanding of JSON paths and Kafka Connect SMTs)

Additional Considerations

  • Performance: Handling transformations at scale may impact Kafka Connect performance. Monitor and tune connector and task configurations to handle increased loads.
  • Schema Evolution: Changes to your source data schema (like renamed fields) will require updates to your Kafka Connect configurations.
  • Error Handling: Implement appropriate error tolerance and dead-letter queue configurations to manage transform errors.

This extraction and rekeying technique facilitates more efficient data processing patterns inside Kafka ecosystems, boosting the effectiveness of stream processing applications by aligning message keys with business-relevant identifiers.


Course illustration
Course illustration

All Rights Reserved.