1

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



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

1 回答 1

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)

于 2021-01-13T03:52:05.180 回答