将 Avro Schema 转换为带有值的 JSON 有效负载

时间:2021-04-28 16:16:17

标签: java json avro

我有一个 org.apache.avro.Schema 对象。我需要创建一个带有字段值的 JSON 完整有效负载。 我宁愿说我有使用这个 post

的解决方案

但是

RandomData.generate 方法中的逻辑很难理解,调试因为它使用递归:private Object generate(Schema schema, Random random, int d)< /p>

是否有人有任何其他实现以更易读的方式实现目标?除了 RandomData 实现之外,我会研究其他解决方案。

1 个答案:

答案 0 :(得分:0)

如果您对使用 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"]}}}

您可以看到,对于联合案例,它将尝试为生成的每个随机记录遍历不同的联合案例。