使用 org.apache.avro.util.RandomData() 从 avro 模式生成 JSON 示例消息不支持 UNION 类型

问题描述 投票:0回答:1

这是我的 avro 架构。

{
    "namespace": "example.avro",
    "type": "record",
    "name": "User",
    "fields": [
        {
            "name": "name",
            "type": "string"
        },
        {
            "name": "favorite_number",
            "type": ["int", "null"]
        },
        {
            "name": "favorite_color",
            "type": ["string", "null"]
        }
    ]
}

我的示例 Java 程序使用 Avro v1.11.3 生成随机 JSON 消息。

package org.example;

import org.apache.avro.Schema;
import org.apache.avro.util.RandomData;

import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;

public class MyTest {
    public static void main(String [] args) throws IOException {
        MyTest me = new MyTest();
        ClassLoader classLoader = me.getClass().getClassLoader();
        InputStream is = classLoader.getResourceAsStream("person.avsc");
        Schema schema = new Schema.Parser().parse(is);
        Iterator<Object> it = new RandomData(schema, 1).iterator();
        System.out.println(it.next());
    }
}

生成的 JSON 消息如下。根据架构进行验证时,它显然是无效的 JSON。

{
    "name": "rymhxcnbcyohbtjmouegufvchxh",
    "favorite_number": 4211,
    "favorite_color": "red"
}

正确且预期的 JSON 消息应包含像这样的 UNION 类型

{
    "name": "rymhxcnbcyohbtjmouegufvchxh",
    "favorite_number": {
        "int": 4211
    },
    "favorite_color": {
        "string": "red"
    }
}

因此,问题是如何生成包含 UNION 类型的 JSON 示例作为上面的正确输出。

json random schema avro generate
1个回答
0
投票

因为我还不能发表评论,但想提供帮助:为什么需要 JSON?您想基于该 JSON 生成新记录吗?

© www.soinside.com 2019 - 2024. All rights reserved.