繁体   English   中英

JSON 的 AVRO 模式

[英]AVRO schema for JSON

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



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

架构应该是这样的:

{
    "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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM