Avro在单一架构

时间:2018-01-04 17:31:15

标签: avro spark-avro

我喜欢多次在Avro架构中使用相同的记录类型。考虑这个模式定义

{
    "type": "record",
    "name": "OrderBook",
    "namespace": "my.types",
    "doc": "Test order update",
    "fields": [
        {
            "name": "bids",
            "type": {
                "type": "array",
                "items": {
                    "type": "record",
                    "name": "OrderBookVolume",
                    "namespace": "my.types",
                    "fields": [
                        {
                            "name": "price",
                            "type": "double"
                        },
                        {
                            "name": "volume",
                            "type": "double"
                        }
                    ]
                }
            }
        },
        {
            "name": "asks",
            "type": {
                "type": "array",
                "items": {
                    "type": "record",
                    "name": "OrderBookVolume",
                    "namespace": "my.types",
                    "fields": [
                        {
                            "name": "price",
                            "type": "double"
                        },
                        {
                            "name": "volume",
                            "type": "double"
                        }
                    ]
                }
            }
        }
    ]
}

这不是有效的Avro架构,Avro架构解析器失败并带有

  

org.apache.avro.SchemaParseException:无法重新定义:my.types.OrderBookVolume

我可以通过将OrderBookVolume移动到两个不同的名称空间来使类型唯一来解决这个问题:

{
    "type": "record",
    "name": "OrderBook",
    "namespace": "my.types",
    "doc": "Test order update",
    "fields": [
        {
            "name": "bids",
            "type": {
                "type": "array",
                "items": {
                    "type": "record",
                    "name": "OrderBookVolume",
                    "namespace": "my.types.bid",
                    "fields": [
                        {
                            "name": "price",
                            "type": "double"
                        },
                        {
                            "name": "volume",
                            "type": "double"
                        }
                    ]
                }
            }
        },
        {
            "name": "asks",
            "type": {
                "type": "array",
                "items": {
                    "type": "record",
                    "name": "OrderBookVolume",
                    "namespace": "my.types.ask",
                    "fields": [
                        {
                            "name": "price",
                            "type": "double"
                        },
                        {
                            "name": "volume",
                            "type": "double"
                        }
                    ]
                }
            }
        }
    ]
}

这不是一个有效的解决方案,因为Avro代码生成会生成两个不同的类,如果我也喜欢将该类型用于其他事情而不仅仅是用于deser和ser,这非常烦人。

此问题与此问题有关: Avro Spark issue #73

通过在命名空间前加上外部记录名称,增加了具有相同名称的嵌套记录的区别。他们的用例可能纯粹与存储相关,因此它可能适用于他们但不适用于我们。

有人知道更好的解决方案吗?这是Avro的一个严格限制吗?

2 个答案:

答案 0 :(得分:5)

没有详细记录,但Avro允许您通过使用正在引用的名称的完整命名空间来引用先前定义的名称。在您的情况下,以下代码将导致仅生成一个类,由每个数组引用。它也很好地干掉了架构。

{
    "type": "record",
    "name": "OrderBook",
    "namespace": "my.types",
    "doc": "Test order update",
    "fields": [
        {
            "name": "bids",
            "type": {
                "type": "array",
                "items": {
                    "type": "record",
                    "name": "OrderBookVolume",
                    "namespace": "my.types.bid",
                    "fields": [
                        {
                            "name": "price",
                            "type": "double"
                        },
                        {
                            "name": "volume",
                            "type": "double"
                        }
                    ]
                }
            }
        },
        {
            "name": "asks",
            "type": {
                "type": "array",
                "items": "my.types.bid.OrderBookVolume"
            }
        }
    ]
}

答案 1 :(得分:0)

the spec中所述:

A schema or protocol may not contain multiple definitions of a fullname.
Further, a name must be defined before it is used ("before" in the
depth-first, left-to-right traversal of the JSON parse tree, where the
types attribute of a protocol is always deemed to come "before" the
messages attribute.)

例如:

{
    "type": "record",
    "namespace": "my.types",
    "name": "OrderBook",
    "fields": [
        {
            "name": "bids",
            "type": {
                "type": "array",
                "items": {
                    "type": "record",
                    "name": "OrderBookVolume",
                    "fields": [
                        {"name": "price", "type": "double"},
                        {"name": "volume", "type": "double"}
                    ]
                }
            }
        },
        {
            "name": "asks",
            "type": {
                "type": "array",
                "items": {
                    "type": "record",
                    "name": "my.types.OrderBookVolume"
                }
            }
        }
    ]
}

第一次出现的是OrderBookVolume的完整架构。之后,您只需参考fullnamemy.types.OrderBookVolume

还值得注意的是,您不需要为每个记录都有一个名称空间。它从其父级继承它。包括它将会覆盖命名空间。

相关问题