-
Notifications
You must be signed in to change notification settings - Fork 1
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 all 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: Since the proto. configurations are usually shared between the producer and the | ||
* consumers,the field descriptors are well-known to both the parties. In cases when there are other | ||
* mechanisms to validate proto. compatibilities schema validation becomes redundant and this class | ||
* can be used in such cases. 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. | ||
*/ | ||
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); | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...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,45 @@ | ||
package org.hypertrace.core.kafkastreams.framework.serdes.proto; | ||
|
||
import com.google.protobuf.Message; | ||
import org.apache.kafka.common.serialization.Serializer; | ||
|
||
/** | ||
* Custom Proto Serializer for Kafka. | ||
* | ||
* <p>This class provides a serialization mechanism for Kafka messages using Protocol Buffers | ||
* without schema validation. It extends the Kafka Serializer interface and allows for direct | ||
* serialization of byte arrays into Proto message objects by utilizing the provided Parser for the | ||
* specific Proto message type. | ||
* | ||
* <p>Motivation: Since the proto. configurations are usually shared between the producer and the | ||
* consumers,the field descriptors are well-known to both the parties. In cases when there are other | ||
* mechanisms to validate proto. compatibilities schema validation becomes redundant and this class | ||
* can be used in such cases. The built-in {@code kafkaProtoSerdes} from Confluent performs schema | ||
* validation via the schema registry service, which introduces overhead. This custom serializer | ||
* 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, and configure | ||
* Kafka to use the custom serializer. | ||
* | ||
* <p>Example: | ||
* | ||
* <pre>{@code | ||
* public class MyProtoMessageSerializer extends ProtoSerializer<MyProtoMessage> { | ||
* | ||
* } | ||
* }</pre> | ||
* | ||
* Then, configure Kafka to use this serializer: | ||
* | ||
* <pre>{@code | ||
* key.serializer=com.example.MyProtoMessageSerializer | ||
* }</pre> | ||
* | ||
* @param <T> The Proto message type to be serialized. | ||
*/ | ||
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(); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...erdes/src/test/java/org/hypertrace/core/kafkastreams/framework/serdes/ProtoSerdeTest.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,38 @@ | ||
package org.hypertrace.core.kafkastreams.framework.serdes; | ||
|
||
import com.google.protobuf.Value; | ||
import org.apache.kafka.common.serialization.Deserializer; | ||
import org.apache.kafka.common.serialization.Serializer; | ||
import org.hypertrace.core.kafkastreams.framework.serdes.proto.ProtoDeserializer; | ||
import org.hypertrace.core.kafkastreams.framework.serdes.proto.ProtoSerializer; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class ProtoSerdeTest { | ||
|
||
private static final String TEST_TOPIC = "test-topic"; | ||
|
||
// Subclass for testing with proto deserialization | ||
public static class TestProtoRecordDeserializer extends ProtoDeserializer<Value> { | ||
public TestProtoRecordDeserializer() { | ||
super(Value.parser()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testSerialize() { | ||
Serializer<Value> serializer = new ProtoSerializer<>(); | ||
|
||
Deserializer<Value> deserializer = new TestProtoRecordDeserializer(); | ||
Value message = Value.newBuilder().setStringValue("id").build(); | ||
|
||
byte[] serializedData = serializer.serialize(TEST_TOPIC, message); | ||
|
||
Assertions.assertNotNull(serializedData); | ||
Assertions.assertTrue(serializedData.length > 0); | ||
|
||
Value deserializedMessage = deserializer.deserialize(TEST_TOPIC, serializedData); | ||
|
||
Assertions.assertEquals(message.getStringValue(), deserializedMessage.getStringValue()); | ||
} | ||
} |
Oops, something went wrong.
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.