首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >跨语言(反)序列化

跨语言(反)序列化
EN

Stack Overflow用户
提问于 2022-09-07 07:51:34
回答 1查看 154关注 0票数 0

我试图在python中序列化,在golang中解封,但我面临错误。

错误消息 --“无法解析无效的有线格式数据”。

代码配置 --

python代码 --

代码语言:javascript
复制
schema_registry_client = SchemaRegistryClient({'url': 'http://localhost:8082'})
protobuf_serializer = ProtobufSerializer(user_attributes_pb2.UserProperties,
                                           schema_registry_client,
                                           {'use.deprecated.format': True})
  producer_conf = {'bootstrap.servers':  'localhost:9092', 'key.serializer': StringSerializer('utf_8'), 'value.serializer': protobuf_serializer}
  producer = SerializingProducer(producer_conf)
  producer.poll(0.0)
  ########## Add an address #########
  PromptForAddress(user_attr)
  producer.produce(topic=topic, key=str(uuid4()), value=user_attr)
  producer.flush()

golang代码 --

代码语言:javascript
复制
        Brokers: []string{brokerAddress},
        Topic:   topic,
        GroupID: "test-consumer-group",
        Logger: l,
    })
    for {
        msg, err := r.ReadMessage(ctx)
        user := &pb.UserProperties{}
        err = proto.Unmarshal(msg.Value, user)
        // client.Query() NewKey(Namespace, Set, user.Id)
        if err != nil {
            log.Fatal(err)
        }
        fmt.Printf("\n%s\n", proto.Message(user))
        fmt.Printf("\n%v\n", user)
        if err != nil {
            panic("could not read message " + err.Error())
        }
        // after receiving the message, log its value
        fmt.Println("received: ", string(msg.Value))
    }

proto文件在两种语言中都是类似的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-07 13:58:51

由于您使用的是Confluent序列化程序及其模式注册表,因此需要在Go中进行同样的操作,并且不能只使用简单的Protobuf反序列化。

Confluent还维护一个Go客户端,您可以使用它。Protobuf - https://github.com/confluentinc/confluent-kafka-go/blob/master/examples/protobuf_consumer_example/protobuf_consumer_example.go示例

我不知道use.deprecated.format对Python做了什么,但是Go可能不会接受

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73631871

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档