Producer and Consumer APIs.
Use External Protobuf Schema when you want to:
- Use Pulsar clients with Kafka Protobuf Schema and Schema Registry compatibility checks.
- Share Protobuf schemas between Kafka and Pulsar clients on the same topic.
- Work with Protobuf message classes generated from
.protofiles.
kafka-schemas library provides a Pulsar Schema implementation backed by the Kafka Protobuf serializer. The same library also supports External JSON Schema and External Avro 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. Declare kafka-protobuf-serializer and protobuf-java explicitly so you control their versions. kafka-schemas also pulls them in transitively.
Add the Kafka Maven repository
pulsar-client, kafka-schemas, and protobuf-java are available from Maven Central. You do not need to add a repository for those dependencies.
kafka-protobuf-serializer is not published to Maven Central. Add the following repository to your pom.xml:
If your organization already mirrors
kafka-protobuf-serializer artifacts in an internal repository, configure that mirror instead of adding the public repository directly.Define a Protobuf schema
External Protobuf Schema works with Protobuf message classes generated from.proto files.
Step 1: Create Protobuf definition files
Createsrc/main/proto/other.proto:
src/main/proto/myRecord.proto:
Step 2: Generate the Protobuf classes
Add a Protobuf Maven plugin to yourpom.xml. The following example uses the protobuf-maven-plugin:
mvn generate-sources to generate the MyRecord and OtherRecord classes in the com.example.protobuf package.
Configure Schema Registry authentication
KafkaSchemaFactory accepts the same Schema Registry configuration properties as KafkaProtobufSerializerConfig. 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.
KafkaProtobufSerializerConfig class in the kafka-protobuf-serializer dependency.
Produce and consume messages
UseKafkaSchemaFactory to create a Pulsar Schema backed by Kafka Protobuf Schema, then create a producer and consumer with the same schema instance.
Schema compatibility
External Protobuf 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 Protobuf schema, creating a producer with External Protobuf Schema on the same topic fails with an incompatible schema error:
Related resources
External JSON Schema
Use Kafka JSON Schema from Pulsar Java clients.
External Avro Schema
Use Kafka Avro 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.