在Azure Schema Registry中用Avro添加内联记录字段时遇未定义报错
解决Azure Schema Registry中Avro Schema依赖引用失败的问题
问题原因
Azure Schema Registry验证Schema时,不会自动关联已注册的依赖Schema——即便com.example.Foo已存在,提交com.example.Bar时仍需明确告知Registry该依赖的存在,否则会触发"未定义名称"的验证错误。
可行解决方案
方案1:提交Schema时显式指定依赖(推荐,适合独立维护Schema的场景)
使用Azure SDK或REST API提交com.example.Bar时,需在请求中包含依赖的com.example.Foo的信息(完全限定名或Schema ID),让Registry主动关联已存在的依赖Schema。
以Azure SDK for Python为例,代码示例如下:
from azure.schemaregistry import SchemaRegistryClient from azure.identity import DefaultAzureCredential client = SchemaRegistryClient(endpoint="你的注册表端点", credential=DefaultAzureCredential()) with client: # 获取已存在的Foo Schema的ID foo_schema_id = client.get_schema_id( group_name="你的Schema组名", schema_name="com.example.Foo", format="AVRO" ) # 提交Bar Schema并指定依赖 bar_schema_content = '''{ "type": "record", "name": "Bar", "namespace": "com.example", "fields": [ { "name": "foo", "type": "com.example.Foo" } ] }''' bar_schema_id = client.register_schema( group_name="你的Schema组名", schema_name="com.example.Bar", schema_content=bar_schema_content, format="AVRO", dependencies=[foo_schema_id] )
如果使用REST API,需在请求体中添加dependencies数组:
{ "schemaContent": "{...Bar的Schema内容...}", "schemaType": "Avro", "dependencies": [ { "schemaId": "Foo的Schema ID" } ] }
方案2:将依赖Schema内联到Bar的Schema中
如果不需要独立维护Foo和Bar的Schema,可以直接把Foo的定义内联到Bar的Schema里,让验证过程无需依赖外部Schema:
{ "type": "record", "name": "Bar", "namespace": "com.example", "fields": [ { "name": "foo", "type": { "type": "record", "name": "Foo", "fields": [ { "name": "foo_string", "type": "string" } ] } } ] }
注意事项
- 确保
com.example.Foo和com.example.Bar处于同一个Schema组内,跨组的Schema引用需要额外配置权限和显式指定。 - 提交Schema时使用的身份凭证需具备Schema Registry的
SchemaRegistry Contributor权限,确保能读取依赖Schema并提交新Schema。
内容的提问来源于stack exchange,提问作者royB
相关产品推荐
相关产品推荐

