簡體   English   中英

如何以編程方式從 Python 中的融合模式注冊表中獲取模式

[英]How to programmatically get schema from confluent schema registry in Python

截至目前,我正在做類似這樣的閱讀 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)

我喜歡這個它對我有用

     import requests
     import os

     SCHEMA_REGISTRY_URL = os.getenv('SCHEMA_REGISTRY_URL');
     print("SCHEMA_REGISTRY_URL: ", SCHEMA_REGISTRY_URL)
     URL = SCHEMA_REGISTRY_URL + '/subjects/' + topic + '/versions/latest/schema'
     r = requests.get(url=URL)
     schema = r.json()


     print("Schema From Schema Registry ==========================>>")
     print("Schema: ", schema)

在此處輸入圖片說明

您可以使用get_latest_version函數來獲取架構信息

 from confluent_kafka.schema_registry import SchemaRegistryClient sr = SchemaRegistryClient({"url": 'http://localhost:8081'}) subjects = sr.get_subjects() for subject in subjects: schema = sr.get_latest_version(subject) print(schema.version) print(schema.schema_id) print(schema.schema.schema_str)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM