All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.daniel.shuy.kafka.protobuf.serde.KafkaProtobufDeserializer Maven / Gradle / Ivy

Go to download

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() {
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy