

<a id="serdes-and-formatter-avro"></a>

# Avro Schema Serializer and Deserializer for Schema Registry on Confluent Platform

Use [Avro](https://avro.apache.org) schemas with the Apache Kafka® Java client and
console tools to serialize and deserialize structured Kafka data.

## What is Avro?

Avro is a data serialization framework with rich data structures, a compact
binary data format, and schema evolution capabilities. Developed within the
Apache Hadoop project, Avro uses JSON-based schemas to define data structures
and supports binary and JSON serialization formats. Avro has these key
features:

- Avro schemas can evolve over time while maintaining backward and forward
  compatibility. For more information, see
  [Schema Evolution and Compatibility for Schema Registry on Confluent Platform](../schema-evolution.md#schema-evolution-and-compatibility).
- Binary encoding provides compact, efficient storage and transmission.
- Avro supports many programming languages, including Java, Python, and C++.
- Data files include embedded schemas, which makes them self-contained.
- You can read and process schemas at runtime without code generation.

The Avro serializer for Confluent Schema Registry doesn’t include the message schema. It
includes a magic byte, the schema ID, and then the normal binary encoding of the
data itself. You can choose whether to embed a schema inline, for cases where
you want to communicate the schema offline, with headers, or some other way.
Other systems, such as Hadoop, always include the schema with the message data.
For more information, see [Wire format](index.md#messages-wire-format).

<a id="sr-serializer-avro"></a>

## Avro serializer

Plug `KafkaAvroSerializer` into `KafkaProducer` to send messages of Avro
type to Kafka.

`KafkaAvroSerializer` supports the following types:

- Primitive types: `null`, `Boolean`, `Integer`, `Long`, `Float`,
  `Double`, `String`, and `byte[]`
- Complex type: `IndexedRecord`

Sending data of any other type to `KafkaAvroSerializer` causes a
`SerializationException`. Typically, `IndexedRecord` is the type for the
value of the Kafka message.

The key of the Kafka message, if used, is often one of the primitive types that
`KafkaAvroSerializer` supports. When you send a message to a topic `t`, the
serializer registers the Avro schemas for the key and the value in Schema Registry under
the subjects `t-key` and `t-value`, respectively, if the compatibility test
passes. The only exception is that Schema Registry never registers the `null` type.

The following example sends a message to Kafka with a key of type string and a
value of type Avro record. The send call can throw a `SerializationException`
if the data isn’t well formed. The example uses the default hostname and port
for the Kafka bootstrap server (`localhost:9092`) and Schema Registry
(`localhost:8081`):

```none
import org.apache.avro.Schema;
import org.apache.avro.generic.GenericData;
import org.apache.avro.generic.GenericRecord;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;
import io.confluent.kafka.serializers.KafkaAvroSerializer;
import java.util.Properties;

Properties props = new Properties();
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,
          org.apache.kafka.common.serialization.StringSerializer.class);
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,
          io.confluent.kafka.serializers.KafkaAvroSerializer.class);
props.put("schema.registry.url", "http://localhost:8081");
KafkaProducer producer = new KafkaProducer(props);

String key = "key1";
String userSchema = "{\"type\":\"record\"," +
                    "\"name\":\"myrecord\"," +
                    "\"fields\":[{\"name\":\"f1\",\"type\":\"string\"}]}";
Schema.Parser parser = new Schema.Parser();
Schema schema = parser.parse(userSchema);
GenericRecord avroRecord = new GenericData.Record(schema);
avroRecord.put("f1", "value1");

ProducerRecord<Object, Object> record = new ProducerRecord<>("topic1", key, avroRecord);
try {
  producer.send(record);
} catch(SerializationException e) {
  // may need to do something with it
}
// When you're finished producing records, you can flush the producer to ensure it has all been written to Kafka and
// then close the producer to free its resources.
finally {
  producer.flush();
  producer.close();
}
```

Code that the [avro-maven-plugin](../../develop/maven-plugin.md#sr-maven-plugin) generates adds
Java-specific properties, such as `"avro.java.string":"String"`, that can
prevent schema evolution. To override this behavior, set
`avro.remove.java.properties=true` in the Avro serializer configuration.

<a id="sr-deserializer-avro"></a>

## Avro deserializer

Plug `KafkaAvroDeserializer` into `KafkaConsumer` to receive messages of any
Avro type from Kafka.

The following example receives messages from Kafka with a key of type `string`
and a value of type Avro record. Getting the message key or value can throw a
`SerializationException` if the data isn’t well formed. The example uses the
default hostname and port for the Kafka bootstrap server (`localhost:9092`) and
Schema Registry (`localhost:8081`):

```none
import org.apache.kafka.clients.consumer.Consumer;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.consumer.ConsumerConfig;

import org.apache.avro.generic.GenericRecord;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Properties;
import java.util.Random;

Properties props = new Properties();

props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
props.put(ConsumerConfig.GROUP_ID_CONFIG, "group1");


props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, "io.confluent.kafka.serializers.KafkaAvroDeserializer");
props.put("schema.registry.url", "http://localhost:8081");

props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");

String topic = "topic1";
final Consumer<String, GenericRecord> consumer = new KafkaConsumer<String, GenericRecord>(props);
consumer.subscribe(Arrays.asList(topic));

try {
  while (true) {
    ConsumerRecords<String, GenericRecord> records = consumer.poll(100);
    for (ConsumerRecord<String, GenericRecord> record : records) {
      System.out.printf("offset = %d, key = %s, value = %s \n", record.offset(), record.key(), record.value());
    }
  }
} finally {
  consumer.close();
}
```

Avro doesn’t need a property to specify a specific type, because the
deserializer derives the type from the Avro schema, using the namespace and name
of the Avro type. The Avro deserializer works by default with topics that
have records of heterogeneous Avro types, such as when you use
`RecordNameStrategy` or `TopicRecordNameStrategy` to store multiple types in
the same topic. For more information, see Martin Kleppmann’s blog post
[Should You Put Several Event Types in the Same Kafka Topic?](https://www.confluent.io/blog/put-several-event-types-kafka-topic/).

An alternative is to use schema references. For more information, see
[Multiple event types in the same topic](#multiple-event-types-same-topic-avro) and the blog post
[Putting Several Event Types in the Same Topic – Revisited](https://www.confluent.io/blog/multiple-event-types-in-the-same-kafka-topic/).

Unlike Avro, the [Protobuf](serdes-protobuf.md#sr-deserializer-protobuf) and
[JSON Schema](serdes-json.md#sr-deserializer-json) deserializers require a specific
property to return a specific type rather than a generic type.

To return a specific type in Avro, add the following configuration:

```java
props.put(KafkaAvroDeserializerConfig.SPECIFIC_AVRO_READER_CONFIG, true);
```




Here is a summary of specific and generic return types for each schema format.

| <br/>         | Avro                                                               | Protobuf                                                     | JSON Schema                                                    |
|---------------|--------------------------------------------------------------------|--------------------------------------------------------------|----------------------------------------------------------------|
| Specific type | Generated class that implements<br/>org.apache.avro.SpecificRecord | Generated class that extends<br/>com.google.protobuf.Message | Java class (that is compatible with<br/>Jackson serialization) |
| Generic type  | org.apache.avro.GenericRecord                                      | com.google.protobuf.DynamicMessage                           | com.fasterxml.jackson.databind.JsonNode                        |

## Configure expiry time for client-side schema caches



The following format-agnostic configuration options for cache expiry time are available on both the serializer and deserializer:

- `latest.cache.size` - The maximum size for caches holding latest schemas
- `latest.cache.ttl.sec` - The time to live (TTL) in seconds for caches holding latest schemas, or `-1` for no TTL

<a id="sr-test-drive-avro"></a>

## Test drive Avro schema

Test drive the Avro schema format by using the producer and consumer commands in
a shell to send and receive Avro data in JSON format. The producer and consumer
use `AvroMessageFormatter` and `AvroMessageReader` to convert between Avro
and JSON.

Avro defines a binary serialization format and a JSON serialization
format. Use JSON when you want human-readable data, and use the more efficient
binary format to store data in topics.

The command line producer and consumer show how the built-in Avro schema support
works on Confluent Platform and Confluent Cloud.




When you incorporate the serializer and deserializer into the code for your own
producers and consumers, messages and associated schemas are processed the same
way as they are on the console producers and consumers.

The suggested consumer commands include a flag to read `--from-beginning` to
be sure you capture the messages even if you don’t run the consumer immediately
after running the producer. If you leave off the `--from-beginning` flag, the
consumer will read only the last message produced during its current session.

The following examples include a few minimal configurations. For full property
references, see [Producer and consumer configuration reference](#avro-test-drive-configs-ref).

### Prerequisites

### Confluent Platform



- Prerequisites to run these examples are generally the same as those described for the [Schema Registry Tutorial](/platform/current/schema-registry/schema_registry_onprem_tutorial.html)
  with the exception of Maven, which is not needed here. Also, Confluent Platform version 5.5.0 or later is required here.
- The following examples use the default Schema Registry URL value (`localhost:8081`). The examples show how to configure
  this inline by supplying the URL as an argument to the `--property` flag in the command line arguments of the
  producer and consumer (`--property schema.registry.url=<address of your schema registry>`). Alternatively, you could
  set this property in `$CONFLUENT_HOME/etc/kafka/server.properties`, and not have to include it
  in the producer and consumer commands. For example: `confluent.schema.registry.url=http://localhost:8081`
- These examples make use of the `kafka-avro-console-producer` and `kafka-avro-console-consumer`, which are located in `$CONFLUENT_HOME/bin`.

### Confluent Cloud




Confluent Cloud prerequisites are:

- A Confluent Cloud account
- Permission to create a topic and schema in a cluster in Confluent Cloud
- Stream Governance Package enabled
- API key and secret for Confluent Cloud cluster (`$APIKEY`, `$APISECRET`)
- API key and secret for Schema Registry (`$SR_APIKEY`, `$SR_APISECRET`)
- Schema Registry endpoint URL (`$SCHEMA_REGISTRY_URL`)
- Cluster ID (`$CLUSTER_ID`)
- Schema registry cluster ID (`$SR_CLUSTER_ID`)

The examples assume that API keys, secrets, cluster IDs, and API endpoints are
stored in persistent environment variables wherever possible, and refer to them
as such. You can store these in shell variables if your setup is temporary. If
you want to return to this environment and cluster for future work, consider
storing them in a profile (such as `.zsh`, `.bashrc`, or `powershell.exe` profiles).

The following steps provide guidelines on these prerequisites specific to these examples. To learn more general information, see [Manage Clusters](/cloud/current/clusters/create-cluster.html#manage-clusters-in-ccloud).

1. Log in to Confluent Cloud:
   ```bash
   confluent login
   ```
2. Create a Kafka cluster in Confluent Cloud
   ```bash
   confluent kafka cluster create <name> [flags]
   ```

   For example:
   ```bash
   confluent kafka cluster create quickstart_cluster --cloud "aws" --region "us-west-2"
   ```

   Your output will include a cluster ID (in the form of `lkc-xxxxxx`), show the cluster name and
   [cluster type](/cloud/current/clusters/cluster-types.html), a Basic cluster in this case,
   and endpoints. Take note of the cluster ID, and store it in an environment variable such as `$CLUSTER_ID`.
3. Get an API key and secret for the cluster:
   ```bash
   confluent api-key create --resource $CLUSTER_ID
   ```

   Store the API key and secret for your cluster in a safe place, such as shell environment variables: `$APIKEY`, `$APISECRET`
4. View Stream Governance packages and Schema Registry endpoint URL.

   A [Stream Governance package](/cloud/current/stream-governance/packages.html#how-to-enable-sr-or-upgrade-to-a-stream-governance-package)
   was enabled as a part of creating the environment.
   - To view governance packages, use the Confluent CLI command [confluent environment list](https://docs.confluent.io/confluent-cli/current/command-reference/environment/confluent_environment_list.html):
     ```bash
     confluent environment list
     ```

     Your output will show the environment ID, name, and associated Stream Governance packages.
   - To view the Stream Governance API endpoint URL, use the command [confluent schema-registry cluster describe](https://docs.confluent.io/confluent-cli/current/command-reference/schema-registry/cluster/confluent_schema-registry_cluster_describe.html):
     ```bash
     confluent schema-registry cluster describe
     ```

     Your output will show the Schema Registry cluster ID in the form of `lsrc-xxxxxx`) and endpoint URL, which is also available to you in Cloud Console on the
     right side panel under “Stream Governance API” in the environment. Store these in environment variables: `$SR_CLUSTER_ID` and `$SCHEMA_REGISTRY_URL`.
5. Create a Schema Registry API key, using the Schema Registry cluster ID (`$SR_CLUSTER_ID`) from the previous step as the resource ID.
   ```bash
   confluent api-key create --resource $SR_CLUSTER_ID
   ```

   Store the API key and secret for your Schema Registry in a safe place, such as shell environment variables: `$SR_APIKEY` and `$SR_APISECRET`

### Create and use schemas

Create an Avro schema, add a second version with a new field, and view the
schemas and messages.

### Confluent Platform

1. Start Confluent Platform with the following command:
   ```bash
   confluent local services start
   ```

   
   
2. Verify registered schema types.
   
   

   Schema Registry supports arbitrary schema types. You should verify which schema types are currently registered with Schema Registry.

   To do so, type the following command (assuming you use the default URL and port for Schema Registry, `localhost:8081`):
   ```bash
   curl http://localhost:8081/schemas/types
   ```

   The response will be one or more of the following. If additional schema format plugins are installed, these will also be available.
   ```bash
   ["JSON", "PROTOBUF", "AVRO"]
   ```

   Alternatively, use the curl `--silent` flag, and pipe the command through [jq](https://stedolan.github.io/jq/) (`curl --silent http://localhost:8081/schemas/types | jq`) to get nicely formatted output:
   ```bash
   "JSON",
   "PROTOBUF",
   "AVRO"
   ```
3. Use the producer to send Avro records in JSON as the message value.

   The following command starts a producer, creates the
   `transactions-avro` topic if it doesn’t already exist, and creates a
   schema for the topic with two fields, `id` and `amount`:
   ```none
   kafka-avro-console-producer --bootstrap-server localhost:9092 --property schema.registry.url=http://localhost:8081 --topic transactions-avro \
   --property value.schema='{"type":"record","name":"Transaction","fields":[{"name":"id","type":"string"},{"name": "amount", "type": "double"}]}'
   ```
4. Type the following message in the shell, and press **Enter**:
   ```none
   { "id":"1000", "amount":500 }
   ```

   <a id="cp-avro-run-consumer-step"></a>
5. To read the message value in JSON from the `transactions-avro` topic,
   run the consumer in a new terminal window:
   ```none
   kafka-avro-console-consumer --bootstrap-server localhost:9092 --from-beginning --topic transactions-avro --property schema.registry.url=http://localhost:8081
   ```

   The console shows the following output:
   ```none
   {"id":"1000","amount":500.0}
   ```

   Leave this consumer running.
6. Register a new schema version under the same subject by adding a new
   field, `customer_id`.

   Because the default subject-level compatibility is `BACKWARD`, give
   the new field a default value so that the schema is compatible with
   the previous version.

   In a new terminal window, run the following command:
   ```none
   kafka-avro-console-producer --bootstrap-server localhost:9092 --property schema.registry.url=http://localhost:8081 --topic transactions-avro \
   --property value.schema='{"type": "record","name": "Transaction","fields": [{"name": "id", "type": "string"},{"name": "amount", "type": "double"},{"name": "customer_id", "type": "string", "default":"null"}]}'
   ```
7. Type the following message into your producer, and press **Enter**:
   ```none
   { "id":"1001", "amount":500, "customer_id":"1221" }
   ```
8. Return to your running consumer to read the new message from the
   `transactions-avro` topic.

   The new output appears after the original output:
   ```none
   {"id":"1000","amount":500.0}
   {"id":"1001","amount":500.0,"customer_id":"1221"}
   ```

   If you closed the original consumer, restart it with the same command
   as in [step 5](#cp-avro-run-consumer-step).
9. To examine the schemas registered with Schema Registry, use `curl` in another
   shell:
   ```none
   curl --silent http://localhost:8081/subjects/transactions-avro-value/versions/1/schema
   ```

   The output for version 1 shows the `id` and `amount` fields:
   ```none
   {"type":"record","name":"Transaction","fields":[{"name":"id","type":"string"},{"name":"amount","type":"double"}]}
   ```

   To view version 2, run the following command:
   ```none
   curl --silent http://localhost:8081/subjects/transactions-avro-value/versions/2/schema
   ```

   The output for version 2 includes the `customer_id` field:
   ```none
   {"type":"record","name":"Transaction","fields":[{"name":"id","type":"string"},{"name":"amount","type":"double"},{"name":"customer_id","type":"string","default":"null"}]}
   ```
10. View the schema in more detail with the following command, which pipes
    the output through `jq` and suppresses the `curl` download messages
    for readable output:
    ```none
    curl --silent -X GET http://localhost:8081/subjects/transactions-avro-value/versions/latest | jq .
    ```

    Your output should resemble:
    ```none
    "subject": "transactions-avro-value",
    "version": 2,
    "id": 2,
    "schema": "{\"type\":\"record\",\"name\":\"Transaction\",\"fields\":[{\"name\":\"id\",\"type\":\"string\"},{\"name\":\"amount\",\"type\":\"double\"},{\"name\":\"customer_id\",\"type\":\"string\",\"default\":\"null\"}]}"
    ```
11. Use Confluent Control Center to examine schemas and messages.
    
    

    Messages that were successfully produced also show on Control Center ([http://localhost:9021/](http://localhost:9021/))
    in **Topics > <topicName> > Messages**. You may have to select a partition or jump to a timestamp to see messages sent earlier.
    (For timestamp, type in a number, which will default to partition `1/Partition: 0`, and press return. To get the message view shown here,
    select the **cards** icon on the upper right.)
    ![Messages tab for the transactions-avro topic in Control Center](images/serdes-avro-c3-messages.png)

    Schemas you create are available on the **Schemas** tab for the selected topic.
    ![Schema tab for the transactions-avro topic in Control Center](images/serdes-avro-c3-schema.png)
12. 
    

    Run shutdown and cleanup tasks.
    - You can stop the consumer and producer with Ctl-C in their respective command windows.
    - To stop Confluent Platform, type `confluent local services stop`.
    - If you would like to clear out existing data (topics, schemas, and messages) before starting again with another test, type `confluent local destroy`.

### Confluent Cloud

1. Create a Kafka topic:
   ```bash
   confluent kafka topic create transactions-avro --cluster $CLUSTER_ID
   ```
2. Copy the following schema into a file named `schema.txt`:
   ```none
   {
      "type": "record",
      "name": "Transaction",
      "fields": [
          {"name": "id", "type": "string"},
          {"name": "amount", "type": "double"}
      ]
    }
   ```
3. Run the following command to create a producer that uses the schema
   from the previous step:
   ```bash
   confluent kafka topic produce transactions-avro \
   --cluster $CLUSTER_ID \
   --schema "<full path to file>/schema.txt" \
   --schema-registry-endpoint $SCHEMA_REGISTRY_URL \
   --schema-registry-api-key $SR_APIKEY \
   --schema-registry-api-secret $SR_APISECRET \
   --api-key $APIKEY --api-secret $APISECRET  \
   --value-format "avro"
   ```

   Your output should resemble:
   ```bash
   Successfully registered schema with ID 100001
   Starting Kafka Producer. Use Ctrl-C or Ctrl-D to exit.
   ```

   Provide the full path to the schema file, even if the file is in the
   current directory.

   #### NOTE
   The examples assume that you use the latest
   [version](https://docs.confluent.io/confluent-cli/current/command-reference/confluent_version.html) of
   the [Confluent CLI](https://docs.confluent.io/confluent-cli/current/overview.html). The new
   `--schema-registry-endpoint`, `--schema-registry-api-key`, and
   `--schema-registry-api-secret` flags replace the deprecated
   `--sr-endpoint`, `--sr-api-key`, and `--sr-api-secret` flags.
   If the CLI doesn’t recognize the new flags, either
   [update](https://docs.confluent.io/confluent-cli/current/command-reference/confluent_update.html) the
   CLI or use the deprecated flags.
4. Type the following message in the shell, and press **Enter**:
   ```none
   { "id":"1000", "amount":500 }
   ```

   <a id="cc-avro-run-consumer-step"></a>
5. To read the message value in JSON from the `transactions-avro` topic,
   run a consumer in another terminal:
   ```bash
   confluent kafka topic consume transactions-avro \
   --cluster $CLUSTER_ID \
   --from-beginning \
   --value-format "avro" \
   --schema-registry-endpoint $SCHEMA_REGISTRY_URL \
   --schema-registry-api-key $SR_APIKEY \
   --schema-registry-api-secret $SR_APISECRET \
   --api-key $APIKEY --api-secret $APISECRET
   ```

   Your output should be:
   ```bash
   {"id":"1000","amount":500}
   ```
6. Register a new schema version under the same subject by adding a new
   field, `customer_id`.

   Because the default subject-level compatibility is `BACKWARD`, give
   the new field a default value so that the schema is compatible with
   the previous version.

   Copy the following schema into a new file named `schema2.txt`:
   ```json
   {
    "type": "record",
    "name": "Transaction",
    "fields": [
        {"name": "id", "type": "string"},
        {"name": "amount", "type": "double"},
        {"name": "customer_id", "type": "string", "default":"null"}
    ]
   }
   ```

   In another terminal, run the following command:
   ```bash
   confluent kafka topic produce transactions-avro \
   --cluster $CLUSTER_ID \
   --schema "<full path to file>/schema2.txt" \
   --schema-registry-endpoint $SCHEMA_REGISTRY_URL \
   --schema-registry-api-key $SR_APIKEY \
   --schema-registry-api-secret $SR_APISECRET \
   --api-key $APIKEY \
   --api-secret "$APISECRET" \
   --value-format "avro"
   ```

   Your output should resemble:
   ```bash
   Successfully registered schema with ID 100002
   Starting Kafka Producer. Use Ctrl-C or Ctrl-D to exit.
   ```
7. Type the following message into your producer, and press **Enter**:
   ```none
   { "id":"1001", "amount":500, "customer_id":"1221" }
   ```
8. Switch to the terminal with your running consumer to read the new
   message from the `transactions-avro` topic.

   The new output appears after the original output:
   ```none
   {"id":"1000","amount":500.0}
   {"id":"1001","amount":500.0,"customer_id":"1221"}
   ```

   If you closed the original consumer, restart it with the same command
   as in [step 5](#cc-avro-run-consumer-step).
9. View versions 1 and 2 of the schema registered with Schema Registry. To view
   version 1, run the following command:
   ```none
   confluent schema-registry schema describe --subject transactions-avro-value --version 1
   ```

   The output for version 1 shows the `id` and `amount` fields:
   ```none
   Schema ID: 100001
   Schema: {"type":"record","name":"Transaction","fields":[{"name":"id","type":"string"},{"name":"amount","type":"double"}]}
   ```

   To view version 2, run the following command:
   ```none
   confluent schema-registry schema describe --subject transactions-avro-value --version 2
   ```

   The output for version 2 includes the `customer_id` field:
   ```none
   Schema ID: 100002
   Schema: {"type":"record","name":"Transaction","fields":[{"name":"id","type":"string"},{"name":"amount","type":"double"},{"name":"customer_id","type":"string","default":"null"}]}
   ```
10. Use the Confluent Cloud Console to examine schemas and messages.
    
    

    Messages that were successfully produced also show on the Confluent Cloud Console ([https://confluent.cloud/](https://confluent.cloud/)).
    in **Topics > <topicName> > Messages**. You may have to select a partition or jump to a timestamp to see messages sent earlier.
    (For timestamp, type in a number, which will default to partition `1/Partition: 0`, and press return. To get the message view shown here,
    select the **cards** icon on the upper right.)
    ![Messages tab for the transactions-avro topic in Confluent Cloud](images/serdes-avro-cloud-ui-messages.png)

    Schemas you create are available on the **Schemas** tab for the selected topic.
    ![Schema tab for the transactions-avro topic in Confluent Cloud](images/serdes-avro-cloud-ui-schema.png)
11. 
    

    Run shutdown and cleanup tasks.
    - You can stop the consumer and producer with Ctl-C in their respective command windows.
    - If you were using shell environment variables and want to keep them for later, remember to store them in a safe, persistent location.
    - You can remove topics, clusters, and environments from the [command line](https://docs.confluent.io/confluent-cli/current/command-reference/overview.html) or from the [Confluent Cloud Console](https://confluent.cloud/).

<a id="avro-test-drive-configs-ref"></a>

## Producer and consumer configuration reference

The following configuration properties apply to any Kafka producers and
consumers, regardless of schema format.

### Add Schema Registry security credentials

Pass truststore and keystore credentials for Schema Registry as properties, in addition to
the Schema Registry properties that the [test drive](#sr-test-drive-avro) examples
pass on the command line and in configuration files. For more information, see
[Additional configurations for HTTPS](/platform/current/schema-registry/security/index.html#additional-configurations-for-https).
The following example shows a producer on Confluent Platform:

```bash
kafka-avro-console-producer --bootstrap-server localhost:9092 \
--property schema.registry.url=http://localhost:8081 --topic transactions-avro \
--property value.schema='{"type":"record","name":"Transaction","fields":[{"name":"id","type":"string"},{"name": "amount", "type": "double"}]}' \
--property schema.registry.ssl.truststore.location=/etc/kafka/security/schema.registry.client.truststore.jks \
--property schema.registry.ssl.truststore.password=myTrustStorePassword
```

### Kafka producer configurations

For a complete reference of producer configuration properties, see
[Kafka Producer Configurations](/platform/current/installation/configuration/producer-configs.html).

### Kafka consumer configurations

For a complete reference of consumer configuration properties, see
[Kafka Consumer Configurations](/platform/current/installation/configuration/consumer-configs.html).

### Schema Registry configuration options

For a complete reference of Schema Registry configuration options, see
[Schema Registry Configuration Options](/platform/current/schema-registry/installation/config.html)
in the Confluent Platform documentation.

### Use Schema Registry with Connect

If you use serializers and deserializers with Kafka Connect, you need to
configure key and value converters. For details, see
[Configuring key and value converters](/kafka-connectors/self-managed/userguide.html#configuring-key-and-value-converters)
in the Connect documentation.

<a id="referenced-schemas-avro"></a>

## Schema references in Avro



Confluent Platform provides full support for the notion of [schema references](index.md#referenced-schemas), the ability of a schema to refer to other schemas.

For more information, see the following:

- [Multiple event types in the same topic](#multiple-event-types-same-topic-avro) for an example of schema
  references.
- The blog post
  [Putting Several Event Types in the Same Topic – Revisited](https://www.confluent.io/blog/multiple-event-types-in-the-same-kafka-topic/)
  for further detail.
- [POST /subjects/(string: subject)/versions](/platform/current/schema-registry/develop/api.html#api-example-avro-schema-references)
  for an API example that registers a new schema with a referenced schema.

<a id="multiple-event-types-same-topic-avro"></a>

## Multiple event types in the same topic

Use [schema references](#referenced-schemas-avro) to
[combine multiple event types in the same topic](https://www.confluent.io/blog/multiple-event-types-in-the-same-kafka-topic/)
and still maintain subject-topic constraints. Schema references also let one
schema call other schemas.

To do this in Avro:

- Use the default [subject name strategy](index.md#sr-schemas-subject-name-strategy),
  `TopicNameStrategy`. It uses the topic name to determine the subject for
  schema lookups and helps enforce subject-topic constraints.
- Use an Avro union to define the schema references as a list of schema names,
  such as:
  ```none
  [
    "io.confluent.examples.avro.Customer",
    "io.confluent.examples.avro.Product",
    "io.confluent.examples.avro.Order"
  ]
  ```

  When you register the schema, send an array of reference versions:
  ```none
  [
    {
      "name": "io.confluent.examples.avro.Customer",
      "subject": "customer",
      "version": 1
    },
    {
      "name": "io.confluent.examples.avro.Product",
      "subject": "product",
      "version": 1
    },
    {
      "name": "io.confluent.examples.avro.Order",
      "subject": "order",
      "version": 1
    }
  ]
  ```
- Configure the Avro serializer to use your Avro union for serialization by
  setting the following properties in your producer application:
  ```none
  auto.register.schemas=false
  use.latest.version=true
  ```

  To set the same properties in Java code:
  ```none
  props.put(AbstractKafkaSchemaSerDeConfig.AUTO_REGISTER_SCHEMAS, false);
  props.put(AbstractKafkaSchemaSerDeConfig.USE_LATEST_VERSION, true);
  ```

  For more information about configuring Schema Registry with Connect, see
  [Configuration Options](/platform/current/schema-registry/connect.html#configuration-options)
  for Kafka Connect.

### Limitations for librdkafka clients

librdkafka clients don’t support Avro unions in serialization or
deserialization. As a workaround, reference the schema as a field for each type
in the union. Fields can contain referenced types. For example, convert a union
such as `["typeA", "typeB"]` to a schema that has a field for each element of
the union, represented as:

```json
{ "type": "A", "typeA": <typeA instance> }, { "type": "B", "typeB": <typeB instance> }
```

Describe type A as follows:

```json
{
   "type": "A",
   "typeA": <typeA instance>,
   "typeB": null
}
```

Describe type B as follows:

```json
{
   "type": "B",
   "typeA": null,
   "typeB": <typeB instance>
}
```

For more information about clients, see:

- [Build Kafka Client Applications on Confluent Cloud](https://docs.confluent.io/cloud/current/client-apps/overview.html)
- [Build Client Applications for Confluent Platform](https://docs.confluent.io/platform/current/clients/index.html)

<a id="messages-avro-reflection"></a>

## Reflection-based Avro serializer and deserializer

Confluent Platform includes `ReflectionAvroSerializer` and `ReflectionAvroDeserializer` to
write and read data in reflection Avro format.

The serializer writes data in the [wire format](index.md#messages-wire-format), and
the deserializer reads data in the same format.

The serde for the reflection-based Avro serializer and deserializer is
`ReflectionAvroSerde`.

For more information, see
[Kafka Streams Data Types and Serialization](/platform/current/streams/developer-guide/datatypes.html#kstreams-data-types-and-serialization).

## Allow null values with Avro reflection

Set `avro.reflection.allow.null` to `true` to allow null values in fields
when you use the [Reflection-based Avro serializer and deserializer](#messages-avro-reflection). The default is `false`.

<a id="sr-avro-schema-compatibility"></a>

## Avro schema compatibility rules

The compatibility rules for Avro are in the Avro specification under
[Schema Resolution](https://avro.apache.org/docs/++version++/specification/#schema-resolution).
For more information, see [Schema Evolution and Compatibility for Schema Registry on Confluent Platform](../schema-evolution.md#schema-evolution-and-compatibility) and
[Compatibility checks](index.md#sr-serdes-schemas-compatibility-checks) in the overview.

Per the specification, extending an enum value follows the same rules as adding
a field, and default values are forward-compatible.

- If you add a new value to the `enum` array, any existing JSON instance that
  was valid against the old schema, using one of the old enum values, is still
  valid against the new schema. Adding a value is a compatible change for
  validation purposes. New instances can use the new value and are still valid.
- If you remove a value from the `enum` array, any existing JSON instance that
  used that removed value is no longer valid against the new schema. Removing a
  value is a breaking change for validation.

## Differences between Avro specific and Avro generic records

In Kafka applications, you can handle Avro records as generic records or as
specific records.

### Generic and specific records overview

- Avro generic records take a dynamic, runtime-based approach to working with
  data. You don’t need to generate Java classes from your schema ahead of time.
  You work with the `GenericRecord` interface and access fields using
  string-based keys at runtime.
- Avro specific records use code generation to create strongly typed Java
  classes from your Avro schemas. The generated classes add compile-time
  type safety and IDE autocompletion.

### Key differences between generic and specific records

The following table summarizes the main differences between Avro generic and
Avro specific records:

| Feature           | Generic records                                 | Specific records                                   |
|-------------------|-------------------------------------------------|----------------------------------------------------|
| Code generation   | Not required                                    | Required, using `avro-maven-plugin` or similar     |
| Type safety       | Runtime checking only                           | Compile-time type checking                         |
| Schema handling   | Schema parsed at runtime                        | Schema embedded in generated class                 |
| Field access      | String-based: `record.get("fieldName")`         | Method-based: `record.getFieldName()`              |
| Development speed | Faster for prototyping and dynamic schemas      | Slower initial setup, faster long-term development |
| Refactoring       | Error-prone because field names are strings     | IDE-supported, safer refactoring                   |
| Schema evolution  | Requires manual field checking                  | Generated classes handle defaults automatically    |
| Performance       | Slightly slower because of field lookup by name | Slightly faster because of direct method calls     |
| Flexibility       | Works with any schema at runtime                | Tied to specific schema versions                   |

### Use Avro generic records

Generic records are ideal when:

- You need to process data with schemas that aren’t known at compile time
- You build schema-agnostic tools or frameworks, such as data transformation
  pipelines
- You want rapid prototyping without code generation steps
- You need to handle different schema versions dynamically

#### Avro generic record producer

To use generic records, define or load the schema manually and use the
`GenericRecord` interface. The following producer defines the schema inline
and sends a generic record:

```java
Properties props = new Properties();
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, KafkaAvroSerializer.class);
props.put("schema.registry.url", "http://localhost:8081");

Schema schema = new Schema.Parser().parse(
      "{\"type\":\"record\",\"name\":\"User\",\"namespace\":\"com.example\","
      + "\"fields\":[{\"name\":\"name\",\"type\":\"string\"},{\"name\":\"age\",\"type\":\"int\"}]}");

try (KafkaProducer<String, GenericRecord> producer = new KafkaProducer<>(props)) {
      GenericRecord user = new GenericData.Record(schema);
      user.put("name", "Alice");
      user.put("age", 30);
      producer.send(new ProducerRecord<>("users", "key1", user));
}
```

#### Avro generic record consumer

When you consume generic records, access fields with string lookups, which
requires manual casting. The following consumer reads generic records and prints
two fields:

```java
Properties props = new Properties();
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
props.put(ConsumerConfig.GROUP_ID_CONFIG, "my-group");
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, KafkaAvroDeserializer.class);
props.put("schema.registry.url", "http://localhost:8081");
// GenericRecord is the default because specific.avro.reader defaults to false

try (KafkaConsumer<String, GenericRecord> consumer = new KafkaConsumer<>(props)) {
      consumer.subscribe(List.of("users"));
      while (true) {
         for (ConsumerRecord<String, GenericRecord> record : consumer.poll(Duration.ofMillis(100))) {
            GenericRecord user = record.value();
            System.out.println(user.get("name") + " " + user.get("age"));
         }
      }
}
```

### Use Avro specific records

Specific records are ideal when:

- You need compile-time type safety and want to catch schema-related errors
  early
- Your schemas are stable and well defined
- You want IDE support for autocompletion and refactoring
- You build production applications with strict type requirements
- You need better documentation through generated code

#### Avro specific record class

The following schema generates a `User` class:

```json
{"type": "record", "name": "User", "namespace": "com.example",
"fields": [{"name": "name", "type": "string"}, {"name": "age", "type": "int"}]}
```

#### Avro specific record producer

The following producer sends a specific record:

```java
Properties props = new Properties();
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, KafkaAvroSerializer.class);
props.put("schema.registry.url", "http://localhost:8081");

try (KafkaProducer<String, User> producer = new KafkaProducer<>(props)) {
      User user = User.newBuilder().setName("Alice").setAge(30).build();
      producer.send(new ProducerRecord<>("users", "key1", user));
}
```

#### Avro specific record consumer

The following consumer reads specific records. Specific records require
`specific.avro.reader` set to `true`. For more information, see
[Avro deserializer](#sr-deserializer-avro).

```java
Properties props = new Properties();
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
props.put(ConsumerConfig.GROUP_ID_CONFIG, "my-group");
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, KafkaAvroDeserializer.class);
props.put("schema.registry.url", "http://localhost:8081");
props.put("specific.avro.reader", true);  // required for SpecificRecord

try (KafkaConsumer<String, User> consumer = new KafkaConsumer<>(props)) {
      consumer.subscribe(List.of("users"));
      while (true) {
         for (ConsumerRecord<String, User> record : consumer.poll(Duration.ofMillis(100))) {
            User user = record.value();
            System.out.println(user.getName() + " " + user.getAge());
         }
      }
}
```

### Schema evolution considerations

Generic and specific records support schema evolution, with these
differences:

- With generic records, you must manually check for field existence and handle
  missing fields when consuming data with evolved schemas.
- With specific records, generated classes include default values from the
  schema, which makes it easier to handle schema evolution. You must regenerate
  the classes when schemas change.

### Best practices for generic and specific records

Follow these best practices when you work with Avro records:

- Use specific records for production applications because compile-time type
  safety and better maintainability outweigh the initial code generation setup
  cost.
- Use generic records for tooling and frameworks when you build tools that
  process arbitrary schemas, such as data migration tools, monitoring
  applications, or generic extract, transform, load (ETL) pipelines. Generic
  records offer the necessary flexibility.
- Configure `avro.remove.java.properties` when you use specific records.
  Setting it to `true` in your serializer configuration removes Java-specific
  properties that can affect schema evolution. For more information, see
  [Avro serializer](#sr-serializer-avro).
- Be consistent within an application. Mixing generic and specific records in
  the same application can lead to confusion. Choose one approach and use it
  consistently.
- Plan for schema evolution. Regardless of which approach you choose, design
  your schemas with evolution in mind from the start. Add default values to
  fields and follow the [compatibility rules](#sr-avro-schema-compatibility).

## Related content

- [Additional configurations for HTTPS on Confluent Platform](/platform/current/schema-registry/security/index.html#additional-configurations-for-https)
- Confluent Developer: [Code Examples for Apache Kafka](https://developer.confluent.io/get-started/) for examples of Kafka client producers and consumers, with and without Avro
- [Kafka Streams Data Types and Serialization](/platform/current/streams/developer-guide/datatypes.html#kstreams-data-types-and-serialization) for more on using Avro with Kafka Streams
- [Apache Avro Documentation](https://avro.apache.org/docs/++version++/) for detailed information on Avro types and serialization
- [Schema Registry Maven Plugin](/platform/current/schema-registry/develop/maven-plugin.html) for generating specific record classes
- [Schema Registry API Reference](/platform/current/schema-registry/develop/api.html) for programmatic schema management
- [Schema Evolution and Compatibility for Schema Registry on Confluent Platform](../schema-evolution.md#schema-evolution-and-compatibility) for how schemas evolve over time
