繁体   English   中英

在 Python 中反序列化为 Protobuf 对象

[英]Deserialisation to Protobuf object in Python

我已经在Kafka生产者中传递了一个Protobuf对象,并且在消费者端接收到一个字节数组。 现在我想再次将该响应反序列化回 Protobuf 对象,但我无法做到这一点。 我怎样才能做到这一点?

这是我的消费者:

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)

我尝试使用 Confluent 但得到

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

如果您不使用 Confluent Schema Registry,请使用ParseFromString

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

如果您使用的是模式注册表,那么 Confluent 有一个 Protobuf 反序列化器类示例 - https://github.com/confluentinc/confluent-kafka-python/blob/master/examples/protobuf_consumer.py

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM