我正在使用以下Avro模式:
Price-state.avsc
{
"namespace": "com.company.model",
"name": "Product",
"type": "record",
"fields": [
{
"name": "product_id",
"type": "string"
},
{
"name": "sale_prices",
"type": {
"name": "sale_prices",
"type": "record",
"fields": [
{
"name": "default",
"type": {
"name": "default",
"type": "record",
"fields": [
{
"name": "order_by_item_price_by_item",
"type": [
"null",
{
"name": "markup_strategy",
"type": "record",
"fields": [
{
"name": "type",
"type": {
"name": "type",
"type": "enum",
"symbols": ["margin", "sale_price"]
}
}
]
}
]
},
{"name": "order_by_item_price_by_weight", "type": ["null", "string"]},
{"name": "order_by_weight_price_by_weight", "type": ["null", "string"]}
]
}
}
]
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
它可以在此网站上正确验证,因此我假设该架构有效。
我在构建JSON文件时遇到问题,然后应使用上述架构对其进行编码。
我正在使用此JSON进行一些测试:
test.json
{
"product_id": "123",
"sale_prices": {
"default": {
"order_by_item_price_by_item": {
"markup_strategy": {
"type": {"enum": "margin"}
}
},
"order_by_item_price_by_weight": null,
"order_by_weight_price_by_weight": null
}
}
}
Run Code Online (Sandbox Code Playgroud)
运行时,java -jar avro-tools-1.8.2.jar fromjson --schema-file prices-state.avsc test.json我得到:
线程“主要” org.apache.avro.AvroTypeException中的异常:未知的联合分支markup_strategy
我在这里读到,由于JSON编码,我必须将东西包装在并集内,所以我尝试了不同的组合,但似乎没人能用。
这是一个名称空间解析问题。以这个简化的模式为例:
测试文件
{
"name": "Product",
"type": "record",
"fields": [
{
"name": "order_by_item_price_by_item",
"type": [
"null",
{
"type": "record",
"name": "markup_strategy",
"fields": [{
"name": "type",
"type": {
"name": "type",
"type": "enum",
"symbols": ["margin", "sale_price"]
}
}]
}
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
使用以下JSON可以很好地验证
test.json
{
"order_by_item_price_by_item": {
"markup_strategy": {
"type": "margin"
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在,如果要在架构顶部添加名称空间,例如
测试文件
{
"namespace": "test",
"name": "Product",
"type": "record",
"fields": [
...
Run Code Online (Sandbox Code Playgroud)
然后,您还需要更改test.json,否则您将获得
线程“主要” org.apache.avro.AvroTypeException中的异常:未知的联合分支markup_strategy
final_test.json
{
"order_by_item_price_by_item": {
"test.markup_strategy": {
"type": "margin"
}
}
}
Run Code Online (Sandbox Code Playgroud)
因此,当在联合类型中并且您使用用户指定名称对JSON编码的Avro命名类型(记录,固定或枚举)进行编码时,该类型的名称也必须在名称空间名称前加上解析度。
| 归档时间: |
|
| 查看次数: |
3103 次 |
| 最近记录: |