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)

最新更新