|
3 | 3 | import com.google.protobuf.Message;
|
4 | 4 | import org.apache.kafka.common.serialization.Serializer;
|
5 | 5 |
|
| 6 | +/** |
| 7 | + * Custom Proto Serializer for Kafka. |
| 8 | + * |
| 9 | + * <p>This class provides a serialization mechanism for Kafka messages using Protocol Buffers |
| 10 | + * without schema validation. It extends the Kafka Serializer interface and allows for direct |
| 11 | + * serialization of byte arrays into Proto message objects by utilizing the provided Parser for the |
| 12 | + * specific Proto message type. |
| 13 | + * |
| 14 | + * <p>Motivation: Since the proto. configurations are usually shared between the producer and the |
| 15 | + * consumers,the field descriptors are well-known to both the parties. In cases when there are other |
| 16 | + * mechanisms to validate proto. compatibilities schema validation becomes redundant and this class |
| 17 | + * can be used in such cases. The built-in {@code kafkaProtoSerdes} from Confluent performs schema |
| 18 | + * validation via the schema registry service, which introduces overhead. This custom serializer |
| 19 | + * eliminates that overhead, simplifying the processing flow by bypassing schema validation. |
| 20 | + * |
| 21 | + * <p>Usage: To use this class, create a subclass specifying the Proto message type, and configure |
| 22 | + * Kafka to use the custom serializer. |
| 23 | + * |
| 24 | + * <p>Example: |
| 25 | + * |
| 26 | + * <pre>{@code |
| 27 | + * public class MyProtoMessageSerializer extends ProtoSerializer<MyProtoMessage> { |
| 28 | + * |
| 29 | + * } |
| 30 | + * }</pre> |
| 31 | + * |
| 32 | + * Then, configure Kafka to use this serializer: |
| 33 | + * |
| 34 | + * <pre>{@code |
| 35 | + * key.serializer=com.example.MyProtoMessageSerializer |
| 36 | + * }</pre> |
| 37 | + * |
| 38 | + * @param <T> The Proto message type to be serialized. |
| 39 | + */ |
6 | 40 | public class ProtoSerializer<T extends Message> implements Serializer<T> {
|
7 | 41 | @Override
|
8 | 42 | public byte[] serialize(String topic, T data) {
|
|
0 commit comments