Skip to main content
External Avro Schema lets Pulsar Java clients produce and consume messages that use Kafka Avro Schema and the Kafka Schema Registry on StreamNative Cloud. Schemas are registered in and resolved from the Kafka Schema Registry, while your application uses the familiar Pulsar Producer and Consumer APIs. Use External Avro Schema when you want to:
  • Use Pulsar clients with Kafka Avro Schema and Schema Registry compatibility checks.
  • Share Avro schemas between Kafka and Pulsar clients on the same topic.
  • Work with Avro SpecificRecord classes generated from .avsc schema files.
The kafka-schemas library provides a Pulsar Schema implementation backed by the Kafka Avro serializer. The same library also supports External JSON Schema and External Protobuf Schema.

Prerequisites

  • A StreamNative Pulsar cluster for message production and consumption.
  • The Kafka Schema Registry service enabled on the cluster.
  • A service account with produce and consume permissions on the target topic.
  • RBAC permissions for the Kafka Schema Registry: assign the schema-writer role to register schemas and the schema-reader role to read schemas. See Schema Registry RBAC roles.
  • Java 17 or higher.
  • Pulsar Java client 4.1.0 or higher.

Add the dependency

Add the following Maven dependencies to your project:
The pulsar-client dependency provides the Pulsar Producer, Consumer, and PulsarClient APIs used in the examples below. The kafka-avro-serializer dependency is required at runtime because kafka-schemas declares it with provided scope. Declare avro explicitly so you control its version; kafka-schemas also pulls it in transitively.

Add the Kafka Maven repository

pulsar-client, kafka-schemas, and avro are available from Maven Central. You do not need to add a repository for those dependencies. kafka-avro-serializer is not published to Maven Central. Add the following repository to your pom.xml:
If your organization already mirrors kafka-avro-serializer artifacts in an internal repository, configure that mirror instead of adding the public repository directly.

Define an Avro schema

External Avro Schema works with Avro SpecificRecord classes. Define a schema in an .avsc file and generate the Java class with the Avro Maven plugin.

Step 1: Create a schema file

Create src/main/avro/Player.avsc:

Step 2: Generate the SpecificRecord class

Add the Avro Maven plugin to your pom.xml:
Run mvn generate-sources to generate the Player class in the com.example.avro package.

Configure Schema Registry authentication

KafkaSchemaFactory accepts the same Schema Registry configuration properties as KafkaAvroSerializerConfig. The external-schemas examples authenticate to the Schema Registry with Basic authentication. Define the helper method as private static so you can call it from main. Use your service account API key as the password. The username can be any non-empty string.
For additional serializer options, see the KafkaAvroSerializerConfig class in the kafka-avro-serializer dependency.

Produce and consume messages

Use KafkaSchemaFactory to create a Pulsar Schema backed by Kafka Avro Schema, then create a producer and consumer with the same schema instance.
When the producer sends the first message, the schema is automatically registered in the Kafka Schema Registry. The consumer resolves the schema from the registry when reading messages.

Schema compatibility

External Avro Schema registers schemas with the Pulsar schema type EXTERNAL. A topic cannot mix EXTERNAL schemas with native Pulsar schemas such as JSON, AVRO, or PROTOBUF on the same topic. For example, if a topic already uses Pulsar’s built-in Schema.AVRO(Player.class), creating a producer with External Avro Schema on the same topic fails with an incompatible schema error:
Plan your schema strategy before publishing to a topic. Once a topic uses External Avro Schema, all producers and consumers on that topic must use the same External Avro Schema type.
Schema compatibility modes for Avro in the Kafka Schema Registry are described in Configurable compatibility modes. StreamNative Cloud supports a subset of the Kafka Schema Registry REST API. See the REST API section for supported operations.

External JSON Schema

Use Kafka JSON Schema from Pulsar Java clients.

External Protobuf Schema

Use Kafka Protobuf Schema from Pulsar Java clients.

Kafka Schema Registry

Configure authentication, compatibility modes, and REST API access.

external-schemas repository

View source code, tests, and release notes for the kafka-schemas library.