Convert type processor
The convert_type processor converts a value type associated with the specified key in an event to the specified type. It is a casting processor that changes the types of some fields in events. Some data must be converted to a different type, such as an integer to a double or a string to an integer, so that it will pass the events through condition-based processors or perform conditional routing.
Starting with OpenSearch Data Prepper version 2.10.0, the convert_entry_type processor has been renamed to convert_type. Configurations using convert_entry_type generate a deprecation warning and should be updated.
Configuration
You can configure the convert_type processor with the following options.
| Option | Required | Description |
|---|---|---|
key | Conditionally | The key of the value to convert to a different type. Required unless keys or coerce_strings is specified. Cannot be used with keys. |
keys | Conditionally | The keys of the values to convert to a different type. Required unless key or coerce_strings is specified. Cannot be used with key. |
type | No | Target type for the key-value pair. Possible values are integer, long, double, big_decimal, string, and boolean. Default value is integer. |
null_conversion_values | No | A list of strings that represent a null value. If the value of a field specified in key or keys equals one of these strings, then the field is removed from the event. |
iterate_on | No | The key of a list of objects in the event. When specified, the processor converts the key or keys fields in each object in the list. |
coerce_strings | No | When specified, the processor converts the string values of all fields in the event, including fields in nested objects and lists, to the type that the string represents. The strings true and false (case insensitive) are converted to Booleans. Strings that contain a colon (:) and match one of the coerce_strings.time_formats patterns are converted to epoch milliseconds. Numeric strings that contain a decimal point or an exponent are converted to doubles, and other numeric strings are converted to integers or, if they are out of the integer range, to longs. Strings that cannot be converted are not changed. Specify coerce_strings as an empty object ({}) to use the default time formats. |
coerce_strings.time_formats | No | A list of Java DateTimeFormatter patterns used to convert time strings to epoch milliseconds. Time strings that do not include a time zone offset are interpreted in the system time zone, and patterns that do not include a year use the current year. Default is ["MMM d HH:mm:ss", "yyyy-MM-dd'T'HH:mm:ss.SSSXXX", "yyyy-MM-dd'T'HH:mm:ss.SSSSSSX", "dd/MMM/yyyy:HH:mm:ss Z"]. |
scale | No | Modifies the scale of the big_decimal when converting to a big_decimal. The default value is 0. |
tags_on_failure | No | A list of tags to be added to the event metadata when the event fails to convert. |
convert_when | No | Specifies a condition using a Data Prepper expression for performing the convert_type operation. If specified, the convert_type operation runs only when the expression evaluates to true. |
Usage
To get started, create the following pipeline.yaml file:
type-conv-pipeline:
source:
...
....
processor:
- convert_type:
key: "response_status"
type: "integer"
Next, create a log file named logs_json.log and replace the path in the file source of your pipeline.yaml file with that file path. For more information, see Running Data Prepper.
For example, before you run the convert_type processor, if the logs_json.log file contains the following event record:
{"message": "value", "response_status":"200"}
The convert_type processor converts the output received to the following output, where the type of response_status value changes from a string to an integer:
{"message":"value","response_status":200}