gpt4 book ai didi

apache-pulsar - 使用 Apache Pulsar 中注册的模式发布到主题

转载 作者:行者123 更新时间:2023-12-02 03:07:50 26 4
gpt4 key购买 nike

Pulsar Schema Registry Docs 中的示例所示

Producer<User> producer = client.newProducer(JSONSchema.of(User.class))
.topic(topic)
.create();
User user = new User(“Tom”, 28);
producer.send(User);

您可以使用 Java 客户端注册生产者和消费者的模式。还提到其他语言的客户端不支持架构注册。

现在是否可以从 Python API 生产者发送关于 Pulsar 主题的消息,该消息将由具有注册模式的消费者使用?例如

processor = PulsarClient.builder()
.serviceUrl("pulsar://pulsarhost:6650")
.build()
.newConsumer(JSONSchema.of(User.class))
.topic("sometopic")
.subscriptionName("somesubscription")
.subscribe();

Python: 导入 pulsar

client = pulsar.Client('pulsar://pulsarhost:6650')

producer = client.create_producer('sometopic')
client.close()

最佳答案

从 Pulsar 2.4 版本开始,您也可以在发布和使用时在 Python 中声明架构。

鉴于 Python 对象的动态性质,我们定义了一个 Record 类,您可以使用它来显式声明架构格式。例如:

import pulsar
from pulsar.schema import *

class Example(Record):
a = String()
b = Integer()
c = Boolean()


client = pulsar.Client('pulsar://localhost:6650')
producer = client.create_producer(
topic='my-topic',
schema=AvroSchema(Example) )

producer.send(Example(a='Hello', b=1))

有关 Python 客户端文档的更多示例:https://pulsar.apache.org/docs/en/client-libraries-python/#declaring-and-validating-schema

关于apache-pulsar - 使用 Apache Pulsar 中注册的模式发布到主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58523566/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com