com.github.daniel.shuy.kafka.protobuf.serde.KafkaProtobufDeserializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kafka-protobuf-serde Show documentation
Show all versions of kafka-protobuf-serde Show documentation
Serializer/Deserializer for Kafka to serialize/deserialize Protocol Buffers messages
The newest version!
package com.github.daniel.shuy.kafka.protobuf.serde;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.MessageLite;
import com.google.protobuf.Parser;
import java.util.Map;
import org.apache.kafka.common.errors.SerializationException;
import org.apache.kafka.common.serialization.Deserializer;
/**
* Deserializer for Kafka to deserialize Protocol Buffers messages
*
* @param Protobuf message type
*/
public class KafkaProtobufDeserializer implements Deserializer {
private final Parser parser;
/**
* Returns a new instance of {@link KafkaProtobufDeserializer}.
*
* @param parser The Protobuf {@link Parser}.
*/
public KafkaProtobufDeserializer(Parser parser) {
this.parser = parser;
}
@Override
public void configure(Map configs, boolean isKey) {
}
@Override
public T deserialize(String topic, byte[] data) {
try {
return parser.parseFrom(data);
} catch (InvalidProtocolBufferException e) {
throw new SerializationException("Error deserializing from Protobuf message", e);
}
}
@Override
public void close() {
}
}