单个架构中相同类型的多条AVRO记录 [英] Avro multiple record of same type in single schema

查看:136
本文介绍了单个架构中相同类型的多条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.tyes.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的硬限制吗?

推荐答案

它没有很好的文档记录,但是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"
            }
        }
    ]
}

这篇关于单个架构中相同类型的多条AVRO记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆