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

Moh*_*ngh 3 python avro apache-kafka confluent-schema-registry

截至目前,我正在做类似这样的阅读 avsc 文件以获取架构

value_schema = avro.load('client.avsc')
Run Code Online (Sandbox Code Playgroud)

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

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

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

Gio*_*ous 12

使用 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]
Run Code Online (Sandbox Code Playgroud)

使用 SchemaRegistryClient

按主题名称获取模式

from schema_registry.client import SchemaRegistryClient


sr = SchemaRegistryClient('localhost:8081')
my_schema = sr.get_schema(subject='mySubject', version='latest')
Run Code Online (Sandbox Code Playgroud)

通过 ID 获取架构

from schema_registry.client import SchemaRegistryClient


sr = SchemaRegistryClient('localhost:8081')
my_schema = sr.get_by_id(schema_id=1)
Run Code Online (Sandbox Code Playgroud)

  • @wobr实际上你可以省略`version`。在这种情况下,您将获得最新的主题架构。 (2认同)