【问题标题】:repeating fields in avro (python)avro(python)中的重复字段
【发布时间】:2015-06-23 07:39:28
【问题描述】:

我正在尝试在 python 中使用 avro 来序列化 XML 数据。我可以弄清楚可选字段编码,但是我该如何做重复字段?

例如,给定这个架构,我如何使 favorite_number 成为一个重复字段,以便某人可以拥有多个 favorite_number?

{"namespace": "example.avro",
 "type": "record",
 "name": "User",
 "fields": [
     {"name": "name", "type": "string"},
     {"name": "favorite_number",  "type": "int"},
     {"name": "favorite_color", "type": ["string", "null"]}
 ]
}

【问题讨论】:

    标签: python schema avro


    【解决方案1】:

    来自specification,可能是这样的......

    # For example, a linked-list of 64-bit values may be defined with:
    
    {
       "type": "record", 
       "name": "LongList",
       "aliases": ["LinkedLongs"],                      // old name for this
       "fields" : [
           {"name": "value", "type": "long"},             // each element has a long
           {"name": "next", "type": ["LongList", "null"]} // optional next element
       ]
    }
    

    或者你可以使用一个数组...

    # Arrays use the type name "array" and support a single attribute:
    #
    # items: the schema of the array's items.
    # For example, an array of strings is declared with:
    
    {"type": "array", "items": "int"}
    

    此外,您还可以嵌套记录like so

    【讨论】:

    • 看起来数组是对的,但是有好几种。我发现了这个&我现在正在处理这个线程......apache-avro.679487.n3.nabble.com/…这是我最终得到的结果:{“namespace”:“example.avro”,“type”:“record”,“name”:“User ", "fields": [ {"name": "name", "type": "string"}, {"name": "favorite_number", "type": {"type": "array", "items" : "int"}}, {"name": "favorite_color", "type": ["string", "null"]} ] }
    猜你喜欢
    • 1970-01-01
    • 2022-01-12
    • 2015-09-25
    • 2018-10-21
    • 2020-05-03
    • 2019-12-21
    • 1970-01-01
    • 2020-06-23
    • 2022-12-06
    相关资源
    最近更新 更多