org.apache.avro.AvroTypeException: Unknown union branch

前端 未结 1 452
面向向阳花
面向向阳花 2021-01-12 16:50

I\'m using this Avro schema:

prices-state.avsc

{
    \"namespace\": \"com.company.model\",
    \"name\": \"Product\",
    \"type\":          


        
1条回答
  •  生来不讨喜
    2021-01-12 17:15

    It was a namespace resolution issue. Take this simplified schema as an example:

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

    With the following JSON it validates just fine

    test.json

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

    Now if you were to add a namespace on top of your schema like

    test.avsc

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

    Then you would need to change your test.json as well or you'll get

    Exception in thread "main" org.apache.avro.AvroTypeException: Unknown union branch markup_strategy

    final_test.json

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

    So when inside a union type and you're JSON encoding an Avro's named type (record, fixed or enum) where the user-specified name is used then the name of that type needs to be prepended with the namespace name too for resolution.

    More on namespaces and JSON encoding.

    0 讨论(0)
提交回复
热议问题