Confluent Avro

Updated at:

Confluent Avro is a format for reading and writing Kafka messages where schemas are managed by Confluent Schema Registry. Unlike plain Avro, each message carries a schema version ID that Flink uses to look up the correct schema at runtime.

How it works

Reading: Flink extracts the schema version ID encoded in each incoming record, retrieves the Avro writer schema from Confluent Schema Registry, and derives the reader schema from the table schema.

Writing: Flink infers the Avro structure from the table schema, then registers or retrieves the schema ID under the configured subject in Confluent Schema Registry. The schema ID is encoded into the output record.

Confluent Avro works with the Apache Kafka connector and the Upsert Kafka connector.

Internally, Flink uses io.confluent.kafka.serializers.KafkaAvroSerializer for serialization and io.confluent.kafka.serializers.KafkaAvroDeserializer for deserialization.

Examples

All three examples define a user_created table with id, name, and email as Avro-encoded Kafka values. They differ in how the Kafka key is encoded and which connector is used.

Example 1: UTF-8 string key + Avro value (Apache Kafka connector)

The Kafka key is a raw UTF-8 string. The Kafka value is an Avro record registered in Confluent Schema Registry.

CREATE TABLE user_created (
  -- Maps to the raw UTF-8 string used as the Kafka key.
  the_kafka_key STRING,

  -- Map to the Avro fields used as Kafka values.
  id    STRING,
  name  STRING,
  email STRING
) WITH (
  'connector'                       = 'kafka',
  'topic'                           = 'user_events_example1',
  'properties.bootstrap.servers'    = 'localhost:9092',

  -- Encode the Kafka key as a raw UTF-8 string using the_kafka_key column.
  'key.format'                      = 'raw',
  'key.fields'                      = 'the_kafka_key',

  'value.format'                    = 'avro-confluent',
  'value.avro-confluent.url'        = 'http://localhost:8082',
  'value.fields-include'            = 'EXCEPT_KEY'
);

To write data to the table:

INSERT INTO user_created
SELECT
  -- Copy the user ID to the Kafka key column.
  id AS the_kafka_key,
  id, name, email
FROM some_table;

Example 2: Avro key + Avro value (Apache Kafka connector)

Both the Kafka key and value are Avro records registered in Confluent Schema Registry. Use key.fields-prefix to avoid column name conflicts when the same field appears in both key and value schemas.

Schema upgrades are generally not backward- or forward-compatible in Kafka keys due to hash partitioning. Consider this before using Avro-encoded keys in production.
CREATE TABLE user_created (
  -- Maps to the Avro field id used as the Kafka key.
  kafka_key_id STRING,

  -- Map to the Avro fields used as Kafka values.
  id    STRING,
  name  STRING,
  email STRING
) WITH (
  'connector'                          = 'kafka',
  'topic'                              = 'user_events_example2',
  'properties.bootstrap.servers'       = 'localhost:9092',

  'key.format'                         = 'avro-confluent',
  'key.avro-confluent.url'             = 'http://localhost:8082',
  'key.fields'                         = 'kafka_key_id',

  -- Prefix key columns to avoid conflicts with identically named value columns.
  'key.fields-prefix'                  = 'kafka_key_',

  'value.format'                       = 'avro-confluent',
  'value.avro-confluent.url'           = 'http://localhost:8082',
  'value.fields-include'               = 'EXCEPT_KEY',

  -- Override the default subject names (available in Flink 1.13 and later).
  'key.avro-confluent.subject'         = 'user_events_example2-key2',
  'value.avro-confluent.subject'       = 'user_events_example2-value2'
);

Example 3: UTF-8 string key + Avro value (Upsert Kafka connector)

The Upsert Kafka connector derives the Kafka key from the table's primary key, so key.fields is not required.

CREATE TABLE user_created (
  -- Maps to the raw UTF-8 string used as the Kafka key.
  kafka_key_id STRING,

  -- Map to the Avro fields used as Kafka values.
  id    STRING,
  name  STRING,
  email STRING,

  -- The primary key determines the Kafka key for upsert semantics.
  PRIMARY KEY (kafka_key_id) NOT ENFORCED
) WITH (
  'connector'                       = 'upsert-kafka',
  'topic'                           = 'user_events_example3',
  'properties.bootstrap.servers'    = 'localhost:9092',

  -- Encode the Kafka key as a raw UTF-8 string.
  -- key.fields is omitted because it is inferred from the primary key.
  'key.format'                      = 'raw',

  -- Prefix key columns to avoid conflicts with identically named value columns.
  'key.fields-prefix'               = 'kafka_key_',

  'value.format'                    = 'avro-confluent',
  'value.avro-confluent.url'        = 'http://localhost:8082',
  'value.fields-include'            = 'EXCEPT_KEY'
);

Format options

Parameter Required Default Data type Description
format Yes None STRING The format identifier. Set to avro-confluent.
avro-confluent.url Yes None STRING The URL of Confluent Schema Registry. Used to retrieve or register schemas.
avro-confluent.subject No None STRING The Confluent Schema Registry subject under which schemas are registered. Defaults to <topic>-value or <topic>-key for the Apache Kafka and Upsert Kafka connectors. Required when using the filesystem connector.
avro-confluent.basic-auth.credentials-source No None STRING The source of basic authentication credentials for Confluent Schema Registry.
avro-confluent.basic-auth.user-info No None STRING Basic authentication user information for Confluent Schema Registry.
avro-confluent.bearer-auth.credentials-source No None STRING The source of bearer authentication credentials for Confluent Schema Registry.
avro-confluent.bearer-auth.token No None STRING The bearer authentication token for Confluent Schema Registry.
avro-confluent.properties No None MAP A properties map forwarded to the underlying Confluent Schema Registry client. Use this for Schema Registry client options not exposed as Flink configuration options. Flink configuration options take precedence over this map.
avro-confluent.ssl.keystore.location No None STRING The location of the SSL keystore.
avro-confluent.ssl.keystore.password No None STRING The password of the SSL keystore.
avro-confluent.ssl.truststore.location No None STRING The location of the SSL truststore.
avro-confluent.ssl.truststore.password No None STRING The password of the SSL truststore.
When configuring key and value formats separately, prefix parameters with key. or value.. For example: 'value.avro-confluent.url' and 'key.avro-confluent.url'.

Data type mappings

Confluent Avro uses the same type mappings as plain Avro. The table below lists how Flink SQL data types map to Avro types.

Flink SQL data type Avro data type Notes
CHAR, VARCHAR, STRING STRING
BOOLEAN BOOLEAN
BINARY, VARBINARY BYTES
DECIMAL FIXED Encoded as a decimal with precision.
TINYINT INT
SMALLINT INT
INT INT
BIGINT LONG
FLOAT FLOAT
DOUBLE DOUBLE
DATE INT Encoded as the number of days since the epoch.
TIME INT Encoded in milliseconds.
TIMESTAMP LONG Encoded in milliseconds.
ARRAY ARRAY
MAP MAP Map keys must be STRING, CHAR, or VARCHAR.
MULTISET MAP Map keys must be STRING, CHAR, or VARCHAR.
ROW RECORD

Nullable types map to an Avro union(T, null), where T is the Avro type corresponding to the Flink SQL type.

For the full Avro type specification, see the Apache Avro specification.