【问题标题】:Correct way to create "record" field in Avro schema在 Avro 模式中创建“记录”字段的正确方法
【发布时间】:2021-02-15 06:54:18
【问题描述】:

我正在尝试理解 Avro 模式并坚持使用复杂类型(记录)。问题很简单:创建一个模式,其中包含一个记录,其中包含两个嵌套到记录的原始字段(字符串和时间戳)。我看到了架构的两个选项:

选项 1

{
    "type": "record",
    "name": "cool_subject",
    "namespace": "com.example",  
    "fields": [
        {
            "name": "field_1",
            "type": "record"
            "fields": [
                {"name": "operation", "type": "string"},
                {"name": "timestamp", "type": "long", "logical_type": "timestamp_millis"}
            ]
        }
    ]
}

选项 2

{
    "type": "record",
    "name": "cool_subject",
    "namespace": "com.example",  
    "fields": [
        {
            "name": "field_1",
            "type": {
                "type": "record",
                "name": "field_1_type",
                "fields": [
                    {"name": "operation", "type": "string"},
                    {"name": "timestamp", "type": {"type": "long", "logical_type": "timestamp_millis"}}
                ]
            }
        }
    ]
}

区别在于“类型”属性。

据我所知 opt2 是正确的方法。我对吗? opt1 有效吗?

【问题讨论】:

    标签: schema avro


    【解决方案1】:

    第二个是正确的。第一个无效。

    记录模式如下所示:

    {
        "type": "record",
        "name": <Name of the record>,
        "fields": [...],
    }
    

    对于字段,应该是这样的:

    [
        {
            "name": <name of field>,
            "type": <type of field>,
        },
        ...
    ]
    

    因此,对于包含记录的字段,它应该始终如下所示:

    [
        {
            "name": <name of field>,
            "type": {
                "type": "record",
                "name": <Name of the record>,
                "fields": [...],
            }
        },
        ...
    ]
    

    第一个示例中的格式会导致不清楚名称“field_1”是字段名称还是记录名称。

    【讨论】:

      猜你喜欢
      • 2018-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-30
      相关资源
      最近更新 更多