python - 如何以编程方式从 Python 中的融合模式注册表中获取模式

标签 python apache-kafka avro confluent-schema-registry

到目前为止,我正在做类似这样的事情来读取 avsc 文件来获取架构

value_schema = avro.load('client.avsc')

我可以做些什么来使用主题名称从融合模式注册表中获取模式吗?

我找到了一种方法,但不知道如何使用它。

https://github.com/marcosschroh/python-schema-registry-client

最佳答案

使用 confluent-kafka-python

from confluent_kafka.avro.cached_schema_registry_client import CachedSchemaRegistryClient

sr = CachedSchemaRegistryClient({
    'url': 'http://localhost:8081',
    'ssl.certificate.location': '/path/to/cert',  # optional
    'ssl.key.location': '/path/to/key'  # optional
})

value_schema = sr.get_latest_schema("orders-value")[1]
key_schema= sr.get_latest_schema("orders-key")[1]

使用 SchemaRegistryClient

按主题名称获取模式
from schema_registry.client import SchemaRegistryClient


sr = SchemaRegistryClient('localhost:8081')
my_schema = sr.get_schema(subject='mySubject', version='latest')

通过 ID 获取架构
from schema_registry.client import SchemaRegistryClient


sr = SchemaRegistryClient('localhost:8081')
my_schema = sr.get_by_id(schema_id=1)

关于python - 如何以编程方式从 Python 中的融合模式注册表中获取模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60467878/

相关文章:

Python AlsaLib 错误

python - 'if not in' 时间复杂度

scala - Kafka API : java. io.IOException:无法解析地址:357d78957cf5:9092

java - 如何将 DataSet<Row> 转换为 JSON 消息的 DataSet 以写入 Kafka?

java - 为什么 avro 无法从 .avro 文件中获取架构?

python - 如何理解gunicorn中的workers是如何被消耗的

python - 为什么我在使用 Google Protocol Buffers 时会看到 "cannot import name descriptor_pb2"错误?

apache-spark - 如何在Spark结构化流中手动设置group.id并提交kafka偏移量?

java - 如何从 Avro Schema 获取所有字段名称?

向服务器发送 avro/bytes POST 请求的 java 示例