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

# Kafka Connect Cast SMT Usage Reference for Confluent Cloud

The `org.apache.kafka.connect.transforms.Cast` Apache Kafka® Single Message
Transformation (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, use `Cast` to force an integer field into an
integer of smaller width. `Cast` supports primitive data types,
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.

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

```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 `ID` field is cast
into a string and the `score` field is cast into a 64-bit floating point
value.

Before:

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

After:

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

## 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`.<br/><br/>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](filter-ak.md#ak-filter), predicates
can conditionally filter out specific records. For details and examples, see [Predicates](filter-ak.md#predicates).
