<a id="cast"></a>

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

<a id="cast-properties"></a>

## Properties

| Name                        | Description                                                                                                                                                                                                                                                                                                                                                          | Type    | Valid values                                                                                       | Importance   |
|-----------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------|----------------------------------------------------------------------------------------------------|--------------|
| `spec`                      | To cast the entire value, specify a single type (for example,<br/>`string`). To cast individual fields of a Map or Struct, specify a<br/>comma-separated list of `field:type` pairs (for example,<br/>`field1:type1,field2:type2`).<br/><br/>Valid types are:<br/><br/>- `int8`, `int16`, `int32`, `int64`<br/>- `float32`, `float64`<br/>- `boolean`<br/>- `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<br/>values. When set to `true`, the default value is applied. Otherwise,<br/>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.

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

```json
"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:

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

After:

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

## Predicates

Configure transformations with predicates to ensure they process only records
that satisfy a particular condition. You can also use predicates in a
transformation chain with the [Kafka Connect Filter (Kafka) SMT Usage Reference for Confluent Platform](filter-ak.md#ak-filter) to conditionally filter specific
records. For more information, see [Predicates](filter-ak.md#predicates).
