Record Transcode Produce

The Record Transcode Produce policy converts an already schema-validated record value into a different serialization format before Event Gateway produces it to the backend cluster. It runs on a record that was already parsed by a Schema Validation Produce policy, so you must nest it under that policy.

This policy supports converting between JSON and Avro.

Use this policy to change the format of a record before Event Gateway writes it to the backend cluster. This change is irreversible, because the cluster never receives the record in its original format. To convert the format of a record only for the client that reads it, and keep the stored record unchanged, use the Record Transcode Consume policy instead.

Use cases

Common use cases for the Record Transcode Produce policy:

Use case

Description

Example: Convert JSON records to Avro before they reach the cluster Convert a client’s JSON records to Avro using an inline schema, so the backend cluster only stores Avro data.
Example: Convert records for a specific topic to Avro Use a condition to convert records produced to a single topic to Avro, and leave records on other topics unchanged.
Example: Accept JSON and Avro records and store them as Avro Use a schema registry with schema_type: any so clients can produce either JSON or Avro, and convert everything to Avro so the backend cluster stores a single format.
Tutorial: Produce Kafka records as Avro Let producers send plain JSON or Avro, and store records as Avro in the backend cluster using a Confluent Schema Registry.

How it works

This policy runs in the produce phase, after schema validation parses the record.

  1. A Kafka client produces a record and sends it to Event Gateway.
  2. The parent Schema Validation policy parses the record value.
  3. The Record Transcode Produce policy converts the record value into the configured output_format.

    If the record value already matches output_format, the policy leaves it unchanged.

  4. Event Gateway sends the record to the backend cluster, which stores the converted value.
 
sequenceDiagram
  autonumber
  participant client as Client
  participant egw as Event Gateway
  participant broker as Event broker

  client->>egw: produce record
  egw->>egw: parse record (Schema Validation)
  egw->>egw: convert record value to output_format

  egw->>broker: send record with converted value
  

Accepting records in more than one format

The parent Schema Validation policy determines which formats a client can produce. A schema registry with a fixed schema_type only accepts records in that one format.

To accept records in any supported format, set schema_type to any v1.3+ on the schema registry that the parent policy uses. This is useful while producers migrate from JSON to Avro, or when different producers send different formats to the same topics.

With any, Event Gateway resolves the format of each record individually:

  • Values that carry a schema reference, in the Confluent wire format or in a record header, are decoded using the registry.
  • Any other value is parsed as JSON without a schema, so Event Gateway doesn’t validate it.

For example, with output_format: avro, clients can produce both JSON and Avro records to the same topic. The policy transcodes the JSON records to Avro, so the backend cluster only ever stores Avro.

Configuring the target schema

Set output_format to json or avro. Converting to Avro requires schema_source, because Avro needs a schema to serialize the record value. Converting to JSON can optionally use schema_source to validate the converted value.

schema_source accepts one of the following types:

Type

Description

reference Looks up an existing schema in a schema registry using a subject and version that you compute with an expression. For example, subject: context.topic.name + '-value' and version: "'latest'".
inline Embeds the raw schema text (for example, an Avro JSON schema) directly in the policy configuration.

Leave schema_source unset if you don’t need a schema for the output data, for example when converting to JSON without validating the result.

Recording the schema reference

schema_ref_destination determines whether and how Event Gateway records a reference to the output schema on the converted record:

Type

Description

confluent_format Prefixes the structured data bytes with a reference to the schema, following the Confluent wire format.
record_header Puts the schema reference in a record header. The header name defaults to __value_schema_id, which is compatible with Confluent’s serializer, but you can change it.
none Doesn’t persist the schema reference anywhere.

Failure modes

The Record Transcode Produce policy supports the following failure modes:

Failure mode

Description

reject Rejects the record batch that holds the record.
passthrough Sends the record to the backend cluster without converting it.
mark Sends the record to the backend cluster without converting it, and adds a kong/policy-failure-<id> header. The value of the header is the reason for the failure.

Conversion fails when the record value can’t be converted to output_format, or when the converted value doesn’t satisfy the schema referenced by schema_source.

Policy order

This policy needs the parsed record value, so nest it directly under the Schema Validation Produce policy that parses it.

This policy doesn’t support nested policies of its own.

See the reference for nested policies for more detail.

Help us make these docs great!

Kong Developer docs are open source. If you find these useful and want to make them better, contribute today!