Cast

The following provides usage information for the Apache Kafka® SMT org.apache.kafka.connect.transforms.Cast

Description

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).

Examples

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 Value

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

After: "63521704.02"

Cast Comma-Separated Values

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.

"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.

Before:

{"ID": 46920,"score": 4761}

After:

{"ID": "46290","score": 4761.0}

Tip

For additional examples, see Cast for managed connectors.

Properties

Name Description Type Default Valid Values Importance
spec A single type to cast the entire value, or for Maps and Structs the comma-separated list of field names and the type to which they should be cast, of the form field1:type1,field2:type2. Valid types are int8, int16, int32, int64, float32, float64, boolean, and string. list   comma-separated list of colon-delimited pairs of field:type, e.g. foo:int32,bar:string high

Predicates

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.