嵌套Avro模式 [英] Nesting Avro schemas
问题描述
根据此问题,有关嵌套Avro模式,嵌套记录架构的正确方法如下:
According to this question on nesting Avro schemas, the right way to nest a record schema is as follows:
{
"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
).我可以给字段和架构起一个相同的名称address
吗?
I don't like giving the field the name address
and having to give a different name (AddressUSRecord
) to the field's schema. Can I give the field and schema the same name, address
?
如果我想在多个其他架构中使用AddressUSRecord
架构,而不仅仅是person
怎么办?如果我想在其他架构中使用AddressUSRecord
,比如说business
,是否还需要命名呢?
What if I want to use the AddressUSRecord
schema in multiple other schemas, not just person
? If I want to use AddressUSRecord
in another schema, let's say business
, do I have to name it something else?
理想情况下,我想在单独的架构中定义AddressUSRecord
,然后让address
的类型引用AddressUSRecord
.但是,尚不清楚Avro 1.8.1是否支持此开箱即用的功能.此 2014年文章显示,需要处理子方案与自定义代码.在Avro 1.8.1中定义可重用架构的最佳方法是什么?
Ideally, I'd like to define AddressUSRecord
in a separate schema, then let the type of address
reference AddressUSRecord
. However, it's not clear that Avro 1.8.1 supports this out-of-the-box. This 2014 article shows that sub-schemas need to be handled with custom code. What the best way to define reusable schemas in Avro 1.8.1?
注意:我想要一个与Confluent Inc.的Schema Registry一起使用的解决方案.有一个 Google网上论坛线程,似乎暗示了该架构注册表不适用于架构引用.
Note: I'd like a solution that works with Confluent Inc.'s Schema Registry. There's a Google Groups thread that seems to suggest that Schema Registry does not play nice with schema references.
推荐答案
我可以为字段和模式指定相同的名称,地址吗?
Can I give the field and schema the same name, address?
是的,您可以使用与字段名称相同的名称来命名记录.
Yes, you can name the record with the same name as the field name.
如果我想在多个其他模式(而不仅仅是个人)中使用AddressUSRecord模式,该怎么办?
What if I want to use the AddressUSRecord schema in multiple other schemas, not just person?
您可以使用以下两种技术使用多个模式:avro模式解析器客户端(JVM和其他客户端)允许您通常通过names
参数来指定多个模式(Java Schema$Parser/parse
方法允许多个模式
You can use multiple schemas using a couple of techniques: the avro schema parser clients (JVM and others) allow you to specify multiple schemas, usually through the names
parameter (the Java Schema$Parser/parse
method allows multiple schema String
arguments).
然后,您可以将依赖的模式指定为命名类型:
You can then specify dependant Schemas as a named type:
{
"type": "record",
"name": "Address",
"fields": [
{
"name": "streetaddress",
"type": "string"
},
{
"name": "city",
"type": "string"
}
]
}
并在父架构之前通过解析器运行它:
And run this through the parser before the parent schema:
{
"name": "person",
"type": "record",
"fields": [
{
"name": "firstname",
"type": "string"
},
{
"name": "lastname",
"type": "string"
},
{
"name": "address",
"type": "Address"
}
]
}
顺便说一句,这使您可以从单独的文件中进行解析.
Incidentally, this allows you to parse from separate files.
或者,您也可以解析以相同方式引用架构的单个Union架构:
Alternatively, you can also parse a single Union schema that references schemas in the same way:
[
{
"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"
}
]
}
]
我想要一个与Confluent Inc.的Schema Registry一起使用的解决方案.
I'd like a solution that works with Confluent Inc.'s Schema Registry.
模式注册表不支持单独解析模式,但是它支持后者解析为联合类型的示例.
The schema registry does not support parsing schemas separately, but it does support the latter example of parsing into a union type.
这篇关于嵌套Avro模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!