apache-pulsar - 在 Apache 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 客户端为生产者和消费者注册模式。还提到其他语言的客户端不支持模式注册表。
现在是否可以从 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()
解决方案
从 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
推荐阅读
- unity3d - 如何从 json 文件中读取数据?
- python - 有没有办法在 Tkinter 中标记椭圆(canvas.create_oval)?
- python - 无法下载任何 Python 库
- wordpress - 如何调整激活 TinyMCE 自动保存功能间隔的插件
- apache-pig - 无法格式化最终的猪结果
- class - tf.scan 中的自定义可训练函数:被跟踪对象中不存在用于 Lambda 层调用的变量
- c - 让 Lua 5.1 和 IUP 3.30 在 Windows 10 x64 上使用 VSCode 和 GCC
- google-play-services - 在 Android 12 beta5 上无法使用 Nearby Messages API 检测信标
- python - NumPy:无法创建二维数组
- javascript - “下一步”按钮使用 Bootstrap 显示为 R Shiny DataTable 的文本