Producer and Consumer APIs.
Use External JSON Schema when you want to:
- Use Pulsar clients with Kafka JSON Schema and Schema Registry compatibility checks.
- Share JSON schemas between Kafka and Pulsar clients on the same topic.
- Build Key-Value messages where the key, value, or both use Kafka JSON Schema.
kafka-schemas library (previously published as kafka-json-schema) provides a Pulsar Schema implementation backed by the Kafka JSON Schema serializer. The same library also supports External Avro 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
produceandconsumepermissions on the target topic. - RBAC permissions for the Kafka Schema Registry: assign the
schema-writerrole to register schemas and theschema-readerrole 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:pulsar-client dependency provides the Pulsar Producer, Consumer, and PulsarClient APIs used in the examples below. The kafka-json-schema-serializer dependency is required at runtime because kafka-schemas declares it with provided scope.
Add the Kafka Maven repository
pulsar-client and kafka-schemas are available from Maven Central. You do not need to add a repository for those dependencies.
kafka-json-schema-serializer is not published to Maven Central. Add the following repository to your pom.xml:
If your organization already mirrors
kafka-json-schema-serializer artifacts in an internal repository, configure that mirror instead of adding the public repository directly.Configure Schema Registry authentication
KafkaSchemaFactory accepts the same Schema Registry configuration properties as KafkaJsonSchemaSerializerConfig. 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.
KafkaJsonSchemaSerializerConfig class in the kafka-json-schema-serializer dependency.
Produce and consume messages
The following example shows how to create a producer and consumer with External JSON Schema.Step 1: Define your message class
Define a POJO for your message payload. Lombok annotations are optional.Step 2: Create a schema and connect to your cluster
UseKafkaSchemaFactory to create a Pulsar Schema backed by Kafka JSON Schema, then create a producer and consumer with the same schema instance.
Use Key-Value schemas
KafkaSchemaFactory also supports Key-Value messages. You can combine a native Pulsar schema for the key with External JSON Schema for the value, or use External JSON Schema for both key and value.
Native Pulsar key with External JSON Schema value
Use a native PulsarSchema.STRING key and an External JSON Schema value:
External JSON Schema for both key and value
Use External JSON Schema for both the key and value:KeyValueEncodingType supports both INLINE and SEPARATED encoding, matching the behavior of Pulsar Key-Value schemas.
Schema compatibility
External JSON Schema registers schemas with the Pulsar schema typeEXTERNAL. 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.JSON(User.class), creating a producer with External JSON Schema on the same topic fails with an incompatible schema error:
Related resources
External Avro Schema
Use Kafka Avro 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.