0

I have passed a Protobuf object in a Kafka producer and am receiving a byte array on the consumer side. Now I want to deserialize that response again back to a Protobuf object, but I am unable to do that. How can I do that?

Here is my consumer:

from email import message
from kafka import KafkaConsumer
import json
from simple import simple_message
import check_pb2

consumer = KafkaConsumer('latest', api_version=(0, 10, 1),
                         group_id='my-group', enable_auto_commit=False,
                         bootstrap_servers=['localhost:9092'])#,
           #value_deserializer=lambda m: json.loads(m.decode('ascii')))
for message in consumer:
    print(message)

I tried to use Confluent but getting

Kafka Exception : {} KafkaError{code=_VALUE_DESERIALIZATION,val=-159,str="Unknown magic byte. This message was not produced with a Confluent Schema Registry serializer"

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245

1 Answers1

1

If you're not using Confluent Schema Registry, use ParseFromString.

for message in consumer:
    value = ParseFromString(message.value)  # example 
    print(value)

If (and only if) you are using the Schema Registry, then Confluent has a Protobuf deserializer class. See the example, which happens to also use ParseFromString internally.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245