org.apache.avro.AvroTypeException:未知的联合分支

时间:2018-04-19 16:39:53

标签: avro avro-tools

我正在使用此Avro架构:

价格-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"]}
                            ]
                        }
                    }
                ]
            }
        }
    ]
}

它在this website上正确验证,所以我假设架构有效。

我在构建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
        }
    }
}

运行java -jar avro-tools-1.8.2.jar fromjson --schema-file prices-state.avsc test.json时,我得到:

  

线程“main”中的异常org.apache.avro.AvroTypeException:未知的union branch markup_strategy

我读here因为JSON编码我必须将内容包装在联合内部所以我尝试了不同的组合,但似乎没有人工作。

1 个答案:

答案 0 :(得分:6)

这是一个命名空间解决方案问题。以此简化模式为例:

<强> test.avsc

{
    "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"]
                        }
                    }]
                }
            ]
        }
    ]
}

使用以下JSON,它可以很好地验证

<强> test.json

{
    "order_by_item_price_by_item": {
        "markup_strategy": {
            "type": "margin"
        }
    }
}

现在,如果您要在模式之上添加名称空间,例如

<强> test.avsc

{
    "namespace": "test",
    "name": "Product",
    "type": "record",
    "fields": [
    ...

然后您还需要更改 test.json ,否则您将获得

  

线程“main”中的异常org.apache.avro.AvroTypeException:未知的union branch markup_strategy

<强> final_test.json

{
    "order_by_item_price_by_item": {
        "test.markup_strategy": {
            "type": "margin"
        }
    }
}

因此,当在一个union类型中并且你是JSON编码Avro的命名类型(record,fixed或enum)时,其中使用了用户指定的名称,那么该类型的名称也需要在命名空间名称前加上分辨率。

有关namespacesJSON encoding的详情。