Kafka Connect Cast SMT Usage Reference for Confluent Platform

The Cast SMT (org.apache.kafka.connect.transforms.Cast) converts fields, keys, or values of a Apache Kafka® record to a specified primitive type.

Description

The Cast SMT converts record components (such as individual fields, entire keys, or values) into a specified data type, automatically updating the schema if one is present. For example, you can use it to downcast a data type, such as forcing a large integer into a smaller bit-width.

Cast supports primitive types such as integer, float, boolean, and string. Binary data is also supported, but it can only be cast to a base64-encoded string.

Use the specific transformation type designed for the record key (org.apache.kafka.connect.transforms.Cast$Key) or value (org.apache.kafka.connect.transforms.Cast$Value).

Properties

Name

Description

Type

Valid Values

Importance

spec

To cast the entire value, specify a single type (for example, string). To cast individual fields of a Map or Struct, specify a comma-separated list of field:type pairs (for example, field1:type1,field2:type2).

Valid types are:

  • int8, int16, int32, int64

  • float32, float64

  • boolean

  • string

list

comma-separated list of colon-delimited pairs of field:type. For example, foo:int32,bar:string

high

replace.null.with.default

Specifies whether to replace null fields with their defined default values. When set to true, the default value is applied. Otherwise, the field remains null.

boolean

true or false. Defaults to true.

medium

Examples

Cast can transform an entire primitive key or value, or one or more fields of a complex (Struct or Map) key or value. The following two examples show how to use Cast in these two scenarios.

Cast a 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 fields in a complex key or value

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 following before-and-after example shows a Map or Struct with an ID field of 46290 and a score field of 4761. The SMT casts the ID field into a string and the score field into a 64-bit floating point value.

Before:

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

After:

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

Predicates

Configure transformations with predicates to ensure they only process records satisfying a particular condition. You can also use predicates in a transformation chain along with the Kafka Connect Filter (Kafka) SMT Usage Reference for Confluent Platform to conditionally filter specific records. For more information, refer to Predicates.