gpt4 book ai didi

python - 如何以编程方式更新 Confluent Schema Registry 中的主题架构和兼容性

转载 作者:行者123 更新时间:2023-12-04 09:55:24 30 4
gpt4 key购买 nike

我已经在架构注册表中注册了一个架构,我可以使用 register() 来完成。像这样,

from schema_registry.client import SchemaRegistryClient, schema

subject_name = "new-schema"
schema_url = "https://{{ schemaRegistry }}:8081"
sr = SchemaRegistryClient(schema_url)

schema = schema.AvroSchema({
"namespace": "example.avro",
"type": "record",
"name": "user",
"fields": [
{"name": "fname", "type": "string"},
{"name": "favorite_number", "type": "int"}
]
})

my_schema = sr.register(subject_name, schema)

现在我需要用一个新字段更新这个相同的主题,所以我会得到新的模式 ID,和 version = 2 .
updated_schema = schema.AvroSchema({
"namespace": "example.avro",
"type": "record",
"name": "user",
"fields": [
{"name": "fname", "type": "string"},
{"name": "favorite_number", "type": "int"},
{"name": "favorite_food", "type": "string"}
]
})

我尝试使用 sr.register(subject_name, updated_schema) ,它会为同一主题引发错误:
AttributeError: 'ClientError' object has no attribute '_get_object_id'
ClientError: Incompatible Avro schema

是的,此功能是注册新架构而不是更新。我没有得到任何更新功能,我不知道我该怎么做。那么如何更新架构?任何帮助,将不胜感激。

最佳答案

在主题中注册新模式时,模式注册表会强制执行某些兼容性规则。因此,您需要确保主题的兼容模式与您正在寻找的模式演变相匹配。

使用 confluent-kafka-python

from confluent_kafka.schema_registry import SchemaRegistryClient


sr = SchemaRegistryClient("https://schema-registry-host:8081")

# Options are:
# - NONE, FULL, BACKWARD, FORWARD,
# - BACKWARD_TRANSITIVE, FORWARD_TRANSITIVE, FULL_TRANSITIVE
sr.set_compatibility("yourSubjectName", "NONE")

使用 python-schema-registry-client
from schema_registry.client import SchemaRegistryClient


sr = SchemaRegistryClient("https://schema-registry-host:8081")
sr.update_compatibility(level="NONE", subject="yourSubjectName")

有关兼容性类型的完整列表,请参阅 Confluent Documentation .

关于python - 如何以编程方式更新 Confluent Schema Registry 中的主题架构和兼容性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61931099/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com