使用Apache Pulsar中已注册的模式发布到主题

时间:2019-10-23 13:06:31

标签: apache-pulsar python-pulsar

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 Client为生产者和使用者注册模式。还提到其他语言的客户端不支持架构注册表。

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

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

Python:     进口脉冲星

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

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

1 个答案:

答案 0 :(得分:1)

从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