Class ProtobufSerde<T extends com.google.protobuf.Message>
- Type Parameters:
T- the generated Protobuf message type
- All Implemented Interfaces:
Closeable, AutoCloseable, org.apache.kafka.common.serialization.Serde<T>
The serde is created from a generated message's default instance. The
.proto schema text registered with the registry is reconstructed from the
message's file descriptor, and the Confluent Protobuf frame — including the
message-index list that identifies nested messages — is written and read
automatically.
On deserialization the writer's registered messageType is compared with
the local message's full name; a mismatch throws SerializationException
instead of silently parsing bytes into the wrong message.
Example
var serde = ProtobufSerde.forValue(Order.getDefaultInstance(), cache);
serde.registerSubject("orders");
cache.prewarm().join();
byte[] bytes = serde.serializer().serialize("orders", order);
Order roundTripped = serde.deserializer().deserialize("orders", bytes);
-
Method Summary
Modifier and TypeMethodDescriptionprotected final SchemaCachecache()Returns the schema cache this serde resolves subjects and writer schemas with.protected TdeserializeBody(int schemaId, byte[] body) Decodes one body into a value using the writer schema registered under the ID.final org.apache.kafka.common.serialization.Deserializer<T> Returns the deserializer half of this serde.static <T extends com.google.protobuf.Message>
ProtobufSerde<T> forKey(T defaultInstance, SchemaCache cache) Creates a key serde for a generated Protobuf message.static <T extends com.google.protobuf.Message>
ProtobufSerde<T> forKey(T defaultInstance, SchemaCache cache, SubjectNameStrategy strategy) Creates a key serde with a custom subject strategy.static <T extends com.google.protobuf.Message>
ProtobufSerde<T> forValue(T defaultInstance, SchemaCache cache) Creates a value serde for a generated Protobuf message.static <T extends com.google.protobuf.Message>
ProtobufSerde<T> forValue(T defaultInstance, SchemaCache cache, SubjectNameStrategy strategy) Creates a value serde with a custom subject strategy.protected byte[]frame(int schemaId, byte[] body) Wraps body bytes in the Confluent wire format.final voidregisterSubject(String topic) Adds this serde's subject to the cache prewarm set.protected byte[]serializeBody(T value) Encodes one value into the schema-format-specific body bytes, without framing.final org.apache.kafka.common.serialization.Serializer<T> Returns the serializer half of this serde.protected ConfluentWireFormat.Frameunframe(byte[] bytes) Splits framed record bytes into the schema ID and body.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.apache.kafka.common.serialization.Serde
close, configure
-
Method Details
-
forValue
public static <T extends com.google.protobuf.Message> ProtobufSerde<T> forValue(T defaultInstance, SchemaCache cache) Creates a value serde for a generated Protobuf message.- Type Parameters:
T- the generated Protobuf message type- Parameters:
defaultInstance- the message's default instance, for exampleOrder.getDefaultInstance()cache- the cache that resolves subjects and writer schemas- Returns:
- a value serde for the message type
-
forValue
public static <T extends com.google.protobuf.Message> ProtobufSerde<T> forValue(T defaultInstance, SchemaCache cache, SubjectNameStrategy strategy) Creates a value serde with a custom subject strategy.- Type Parameters:
T- the generated Protobuf message type- Parameters:
defaultInstance- the message's default instance, for exampleOrder.getDefaultInstance()cache- the cache that resolves subjects and writer schemasstrategy- the subject naming strategy that overrides the cache default- Returns:
- a value serde for the message type
-
forKey
public static <T extends com.google.protobuf.Message> ProtobufSerde<T> forKey(T defaultInstance, SchemaCache cache) Creates a key serde for a generated Protobuf message.- Type Parameters:
T- the generated Protobuf message type- Parameters:
defaultInstance- the message's default instance, for exampleOrderKey.getDefaultInstance()cache- the cache that resolves subjects and writer schemas- Returns:
- a key serde for the message type
-
forKey
public static <T extends com.google.protobuf.Message> ProtobufSerde<T> forKey(T defaultInstance, SchemaCache cache, SubjectNameStrategy strategy) Creates a key serde with a custom subject strategy.- Type Parameters:
T- the generated Protobuf message type- Parameters:
defaultInstance- the message's default instance, for exampleOrderKey.getDefaultInstance()cache- the cache that resolves subjects and writer schemasstrategy- the subject naming strategy that overrides the cache default- Returns:
- a key serde for the message type
-
serializeBody
Encodes one value into the schema-format-specific body bytes, without framing.- Parameters:
value- the non-null value to encode- Returns:
- the encoded body
-
deserializeBody
Decodes one body into a value using the writer schema registered under the ID.- Parameters:
schemaId- the schema ID taken from the frame headerbody- the body bytes that followed the frame header- Returns:
- the decoded value
- Throws:
Exception- if the body cannot be decoded
-
frame
protected byte[] frame(int schemaId, byte[] body) Wraps body bytes in the Confluent wire format.- Parameters:
schemaId- the resolved schema ID for the subjectbody- the encoded body- Returns:
- the framed record bytes
-
unframe
Splits framed record bytes into the schema ID and body.- Parameters:
bytes- the framed record bytes- Returns:
- the decoded frame
-
registerSubject
Adds this serde's subject to the cache prewarm set.Call this once per topic the serde will read or write, then resolve every registered subject with
SchemaCache.prewarm()before the first record is serialized. Registration is idempotent by subject.- Parameters:
topic- the Kafka topic whose subject should be resolved during prewarm
-
cache
Returns the schema cache this serde resolves subjects and writer schemas with.- Returns:
- the schema cache supplied at construction
-
serializer
Returns the serializer half of this serde.The serializer frames each non-null value with the Confluent wire format using the schema ID resolved during prewarm. It throws
SerializationExceptionwhen the subject has not been resolved yet or when the body cannot be encoded. A null value serializes to null.- Specified by:
serializerin interfaceorg.apache.kafka.common.serialization.Serde<T>- Returns:
- the reusable, thread-safe serializer
-
deserializer
Returns the deserializer half of this serde.The deserializer unframes the Confluent header, resolves the writer schema for the frame's schema ID through the cache, and decodes the body. It throws
SchemaFetchPendingExceptionwhile an unknown writer schema is being fetched andSerializationExceptionfor malformed input. Null input deserializes to null.- Specified by:
deserializerin interfaceorg.apache.kafka.common.serialization.Serde<T>- Returns:
- the reusable, thread-safe deserializer
-