如果您对使用 Python 没问题,最近添加了一个功能可以满足您的需求。你应该可以pip install fastavro 然后运行类似下面的脚本:
from fastavro import json_writer
from fastavro.utils import generate_many
from io import StringIO
schema = {
"namespace": "my.test",
"name": "example_value_schema",
"type": "record",
"fields": [
{
"name": "field",
"type": [
"null",
"int",
{
"type": "record",
"name": "my_field_type2",
"fields": [
{"name": "subfield", "type": "string"},
{"name": "bytes_field", "type": "bytes"},
{"name": "array_field", "type": {"type": "array", "items": "string"}},
]
}
]
}
]
}
sio = StringIO()
json_writer(sio, schema, generate_many(schema, 6))
print(sio.getvalue())
我运行它时的输出如下:
{"field": null}
{"field": {"int": 1903741208}}
{"field": {"my.test.my_field_type2": {"subfield": "TXJrWrluTg", "bytes_field": "\u0084\u008fEf\u0014\u00f4U\u00ba\u00f4]", "array_field": ["zdXQkoeFQv", "SCWCJmMsOd", "HeISJlaUoE", "qxptYDFfsb", "TcOiaLrXDA", "vOyWPySldE", "HOoeLYRVhS", "lUjhemxuSQ", "fiBdeeUSpZ", "AqTBRFpNoU"]}}}
{"field": null}
{"field": {"int": 1048727191}}
{"field": {"my.test.my_field_type2": {"subfield": "jzZYVZMdXq", "bytes_field": "\u0098Y\u00c5\u00f1\u0095\u009b\u00fd\u008bU]", "array_field": ["xyPsFLOhDp", "lSIWrETtvP", "NHmfWoOCGI", "iqtjfwmQNd", "hlENhjDOse", "oMQpJPkgQY", "eoIRSOydWj", "UChETKEaAk", "JlqxqDrCyH", "RyrLAxoePf"]}}}
您可以看到,对于联合案例,它将尝试为生成的每个随机记录循环遍历不同的联合案例。