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
