Roger Alkins
Roger Alkins

Reputation: 145

Kafka Connect SMT to add Kafka header fields

I need to find or write an SMT that will add header fields to a request. The request is missing some type fields and I want to add them.

How exactly do you add a header within an SMT all I have seen are just record transforms like below but what if its the header I want to change or add a field to?

   private R applySchemaless(R record) {

   final Map<String, Object> value = requireMap(operatingValue(record), PURPOSE);
  // record.headers.add(Header)  but how do I define the header
  // or record.headers.add(String, Schema) but I am not sure how to define Schema? 
  final Map<String, Object> updatedValue = new HashMap<>(value);

  updatedValue.put(fieldName, getRandomUuid());
  

  return newRecord(record, null, updatedValue);
  
}

Upvotes: 1

Views: 2435

Answers (1)

Nic Pegg
Nic Pegg

Reputation: 503

This should work

Headers headers = new ConnectHeaders();
headers.add(myKey, myValue, mySchema);
headers.forEach(h -> record.headers().add(h));

ConnectHeaders info can be found here - https://kafka.apache.org/25/javadoc/org/apache/kafka/connect/header/Headers.html

Upvotes: 2

Related Questions