gpt4 book ai didi

nested - 嵌套 Avro 架构

转载 作者:行者123 更新时间:2023-12-02 11:35:19 33 4
gpt4 key购买 nike

根据this question在嵌套 Avro schema 上,嵌套记录 schema 的正确方法如下:

{
"name": "person",
"type": "record",
"fields": [
{"name": "firstname", "type": "string"},
{"name": "lastname", "type": "string"},
{
"name": "address",
"type": {
"type" : "record",
"name" : "AddressUSRecord",
"fields" : [
{"name": "streetaddress", "type": "string"},
{"name": "city", "type": "string"}
]
},
}
]
}

我不喜欢为字段指定名称 address,并且必须为字段的架构指定不同的名称 (AddressUSRecord)。我可以为字段和架构指定相同的名称,地址吗?

如果我想在多个其他架构中使用 AddressUSRecord 架构,而不仅仅是 person 该怎么办?如果我想在另一个架构中使用 AddressUSRecord,比如说 business,我是否必须将其命名为其他名称?

理想情况下,我想在单独的架构中定义 AddressUSRecord,然后让 address 的类型引用 AddressUSRecord。但是,尚不清楚 Avro 1.8.1 是否支持这种开箱即用的功能。这个2014 article表明子模式需要使用自定义代码进行处理。在 Avro 1.8.1 中定义可重用架构的最佳方法是什么?

注意:我想要一个与 Confluence Inc. 的架构注册表配合使用的解决方案。有一个Google Groups thread这似乎表明架构注册表不能很好地处理架构引用。

最佳答案

Can I give the field and schema the same name, address?

是的,您可以使用与字段名称相同的名称来命名记录。

What if I want to use the AddressUSRecord schema in multiple other schemas, not just person?

您可以通过多种技术来使用多个模式:avro 模式解析器客户端(JVM 和其他)允许您指定多个模式,通常通过 names 参数(Java Schema $Parser/parse 方法允许多个模式 String 参数)。

然后您可以将依赖架构指定为命名类型:

{
"type": "record",
"name": "Address",
"fields": [
{
"name": "streetaddress",
"type": "string"
},
{
"name": "city",
"type": "string"
}
]
}

并在父模式之前通过解析器运行它:

{
"name": "person",
"type": "record",
"fields": [
{
"name": "firstname",
"type": "string"
},
{
"name": "lastname",
"type": "string"
},
{
"name": "address",
"type": "Address"
}
]
}

顺便说一句,这允许您从单独的文件中进行解析。

或者,您也可以解析以相同方式引用架构的单个联合架构:

[
{
"type": "record",
"name": "Address",
"fields": [
{
"name": "streetaddress",
"type": "string"
},
{
"name": "city",
"type": "string"
}
]
},
{
"type": "record",
"name": "person",
"fields": [
{
"name": "firstname",
"type": "string"
},
{
"name": "lastname",
"type": "string"
},
{
"name": "address",
"type": "Address"
}
]
}
]

I'd like a solution that works with Confluent Inc.'s Schema Registry.

模式注册表不支持单独解析模式,但它支持后一个解析为联合类型的示例。

关于nested - 嵌套 Avro 架构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40854529/

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