liveBook · Manning

Apply method

As explained in Kafka Streams in Action, Second Edition

The apply method is the Kafka Connect transformation operation that receives a record and returns its transformed form.

The apply method is the central operation in a Kafka Connect transformation: it receives a Connect record, chooses the correct value representation, and returns the transformed record. section 5.6.3

A custom transformation uses apply to handle these cases:

  • Return the original record when the operating value is null. section 5.6.3
  • Process schemaless values as maps, retaining only configured field names. section 5.6.3
  • Process schema-based values as Struct objects, rebuilding and caching an updated schema. section 5.6.3
  • Support transformation of either the record key or value through operating methods and inner classes. section 5.6.3

Filtering a stock-feed record

A stock API record may contain more than 70 metrics. The apply method can retain only the five or six configured fields, handling both schemaless maps and schema-based Struct values. section 5.6.3

Listing 5.12 Implementation of the custom transformation
public abstract class MultiFieldExtract<R extends ConnectRecord<R>>   #1
                                       implements Transformation<R> {

 @Override
public R apply(R connectRecord) {      #2
  if (operatingValue(connectRecord) == null) {   #3
    return connectRecord;
  } else if (operatingSchema(connectRecord) == null) {  #4
    return applySchemaless(connectRecord);
  } else {
    return applyWithSchema(connectRecord);  #5
  }
}

Trace the decision path from a null value to schemaless or schema-aware transformation. section 5.6.3

How Kafka Streams in Action, Second Edition covers it

How deep it goes
Explains branching, map filtering, Struct rebuilding, schema caching, and key/value handling without covering omitted implementation details.
Examples
Uses a stock API record with more than 70 metrics and a configurable field-extraction transformation.
What it assumes
Assumes familiarity with Kafka Connect transformations, records, schemas, and configured key/value operation.
What you can do after
Build a custom transformation that filters schemaless or schema-based record values.
How it connects
Connects Kafka Connect records with transformations, schemas, Struct values, and key/value processing.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kafka Streams in Action, Second Edition. The section links point to the parts of the book it draws on.