-
Notifications
You must be signed in to change notification settings - Fork 2
Adding custom proto serdes #100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
singh-viikram
wants to merge
7
commits into
main
Choose a base branch
from
addProtoSerde
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8671900
Adding custom proto serdes
singh-viikram 147cd04
resolved comments
singh-viikram c140cbc
nits
singh-viikram ccb682d
Added tests for proto serdes
singh-viikram 31576cf
nits
singh-viikram 7cf6d31
nits
singh-viikram 2a40d95
nits
singh-viikram File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
.../main/java/org/hypertrace/core/kafkastreams/framework/serdes/proto/ProtoDeserializer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package org.hypertrace.core.kafkastreams.framework.serdes.proto; | ||
|
|
||
| import com.google.protobuf.InvalidProtocolBufferException; | ||
| import com.google.protobuf.Message; | ||
| import com.google.protobuf.Parser; | ||
| import org.apache.kafka.common.serialization.Deserializer; | ||
|
|
||
| /** | ||
| * Custom Proto Deserializer for Kafka. | ||
| * | ||
| * <p>This class provides a deserialization mechanism for Kafka messages using Protocol Buffers | ||
| * without schema validation. It extends the Kafka Deserializer interface and allows for direct | ||
| * deserialization of byte arrays into Proto message objects by utilizing the provided Parser for | ||
| * the specific Proto message type. | ||
| * | ||
| * <p>Motivation: In setups where both producers and consumers use the same Proto schemas, the need | ||
| * for schema validation becomes redundant. The built-in {@code kafkaProtoSerdes} from Confluent | ||
| * performs schema validation via the schema registry service, which introduces overhead. This | ||
| * custom deserializer eliminates that overhead, simplifying the processing flow by bypassing schema | ||
| * validation. | ||
| * | ||
| * | ||
| * <p>Usage: To use this class, create a subclass specifying the Proto message type, pass the | ||
| * corresponding Parser to the superclass constructor, and configure Kafka to use the custom | ||
| * deserializer. | ||
| * | ||
| * <p>Example: | ||
| * | ||
| * <pre>{@code | ||
| * public class MyProtoMessageDeserializer extends ProtoDeserializer<MyProtoMessage> { | ||
| * public MyProtoMessageDeserializer() { | ||
| * super(MyProtoMessage.parser()); | ||
| * } | ||
| * } | ||
| * }</pre> | ||
| * | ||
| * Then, configure Kafka to use this deserializer: | ||
| * | ||
| * <pre>{@code | ||
| * key.deserializer=com.example.MyProtoMessageDeserializer | ||
| * }</pre> | ||
| * | ||
| * @param <T> The Proto message type to be deserialized. | ||
| */ | ||
singh-viikram marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public class ProtoDeserializer<T extends Message> implements Deserializer<T> { | ||
singh-viikram marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private final Parser<T> parser; | ||
|
|
||
| public ProtoDeserializer(Parser<T> parser) { | ||
| this.parser = parser; | ||
| } | ||
|
|
||
| @Override | ||
| public T deserialize(String s, byte[] bytes) { | ||
| try { | ||
| return parser.parseFrom(bytes); | ||
| } catch (InvalidProtocolBufferException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
| } | ||
11 changes: 11 additions & 0 deletions
11
...rc/main/java/org/hypertrace/core/kafkastreams/framework/serdes/proto/ProtoSerializer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package org.hypertrace.core.kafkastreams.framework.serdes.proto; | ||
|
|
||
| import com.google.protobuf.Message; | ||
| import org.apache.kafka.common.serialization.Serializer; | ||
|
|
||
| public class ProtoSerializer<T extends Message> implements Serializer<T> { | ||
singh-viikram marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @Override | ||
| public byte[] serialize(String topic, T data) { | ||
| return data.toByteArray(); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.