【问题标题】:AVRO schema for JSONJSON 的 AVRO 模式
【发布时间】:2021-01-12 18:05:35
【问题描述】:

我有一个这样生成的 JSON。我想知道为此的 avro 模式是什么。数组列表中键值的数量不固定。有相关的帖子,但它们引用了键并且不会更改。在我的情况下,键会改变。变量键的名称不断变化。



"fixedKey": [
                {
                    "variableKey1": 2
                },
                {
                    "variableKey2": 1
                },
                {
                    "variableKey3": 3
                },
                .....
                {
                    "variableKeyN" : 10
                }
    
    
            ]

【问题讨论】:

    标签: json avro fastavro


    【解决方案1】:

    架构应该是这样的:

    {
        "type": "record",
        "name": "test",
        "fields": [
            {
                "name": "fixedKey",
                "type": {
                    "type": "array",
                    "items": [
                        {"type": "map", "values": "int"},
                    ],
                },
            }
        ],
    }
    

    以下是对示例数据进行序列化和反序列化的示例:

    from io import BytesIO
    from fastavro import writer, reader
    
    
    schema = {
        "type": "record",
        "name": "test",
        "fields": [
            {
                "name": "fixedKey",
                "type": {
                    "type": "array",
                    "items": [
                        {"type": "map", "values": "int"},
                    ],
                },
            }
        ],
    }
    
    records = [
        {
            "fixedKey": [
                {
                    "variableKey1": 1,
                },
                {
                    "variableKey2": 2,
                },
                {
                    "variableKey3": 3,
                },
            ]
        }
    ]
    
    bio = BytesIO()
    
    writer(bio, schema, records)
    bio.seek(0)
    for record in reader(bio):
        print(record)
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-09
      • 2020-01-17
      • 1970-01-01
      • 2014-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多