Kafka Connect Cast SMT Usage Reference for Confluent Cloud

The org.apache.kafka.connect.transforms.Cast Apache Kafka® Single Message Transform (SMT) casts fields or an entire record key or value to a specific type.

Description

The Cast SMT casts one or more fields, or an entire record key or value, to a specified type. If the record has a schema, the SMT updates the schema to match the new type. For example, Cast 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 or value, or one or more fields of a complex (Struct or Map) key or value. The following two configuration snippet examples 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 following before and after example 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": 46290, "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. For example, 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 Kafka Connect Filter (Kafka) SMT Usage Reference for Confluent Cloud, predicates can conditionally filter out specific records. For details and examples, see Predicates.