CONFLUENT PLATFORM
The following provides usage information for the Apache Kafka® SMT org.apache.kafka.connect.transforms.Cast
org.apache.kafka.connect.transforms.Cast
Cast fields (or the entire key or value) to a specific type, updating the schema if one is present. For example, this can be used to force an integer field into an integer of smaller width. Only simple primitive types are supported, such as integer, float, boolean, and string.
Use the concrete transformation type designed for the record key (org.apache.kafka.connect.transforms.Cast$Key) or value (org.apache.kafka.connect.transforms.Cast$Value).
org.apache.kafka.connect.transforms.Cast$Key
org.apache.kafka.connect.transforms.Cast$Value
Cast can transform an entire primitive key/value or one or more fields of a complex (Struct or Map) key or value. The two configuration snippet examples below show how to use Cast in these two scenarios.
Cast
This example shows how to use Cast to transform a 64-bit floating point value into a string.
"transforms": "Cast", "transforms.Cast.type": "org.apache.kafka.connect.transforms.Cast$Value", "transforms.Cast.spec": "string"
Before: 63521704.02
63521704.02
After: "63521704.02"
"63521704.02"
This example shows how to use Cast with a complex key or value. It shows a comma-separated field:type pair, where field is the name of the field to cast and type is the type to cast the field into.
field:type
field
type
"transforms": "Cast", "transforms.Cast.type": "org.apache.kafka.connect.transforms.Cast$Value", "transforms.Cast.spec": "ID:string,score:float64"
The before and after example below shows a Map or Struct with an ID field of 46290 and a score field of 4761. The ID field is cast into a string and the score field is cast into a 64-bit floating point value.
ID
46290
score
4761
Before:
{"ID": 46920,"score": 4761}
After:
{"ID": "46290","score": 4761.0}
spec
field1:type1,field2:type2
int8
int16
int32
int64
float32
float64
boolean
string
foo:int32,bar:string
Transformations can be configured with predicates so that the transformation is applied only to records which satisfy a condition. You can use predicates in a transformation chain and, when combined with the Filter (Apache Kafka), predicates can conditionally filter out specific records. For details and examples, see Predicates.