Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java AVRO-支持联合记录类型的复杂记录_Java_Python_Avro - Fatal编程技术网

Java AVRO-支持联合记录类型的复杂记录

Java AVRO-支持联合记录类型的复杂记录,java,python,avro,Java,Python,Avro,我正在尝试用Union数据类型支持的成员记录类型构建AVRO的复杂记录 { "namespace": "proj.avro", "protocol": "app_messages", "doc" : "application messages", "types": [ { &q

我正在尝试用Union数据类型支持的成员记录类型构建AVRO的复杂记录

{
    "namespace": "proj.avro",
    "protocol": "app_messages",
    "doc" : "application messages",
    
    "types": [
        {
            "name": "record_request",
            "type" : "record", 
            "fields": 
            [
                {
                    "name" : "request_id", 
                    "type" : "int"
                },
                {
                    "name" : "message_type",
                    "type" : int,
                },
                {
                    "name" : "users",
                    "type" : "string"
                }
            ]
        },
        {
            "name" : "request_response",
            "type" : "record",
            "fields" :
            [
                {
                    "name" : "request_id",
                    "type" : "int"
                },
                {
                    "name" : "response_code",
                    "type" : "string"
                },
                {
                    "name" : "response_count",
                    "type" : "int"
                },
                {
                    "name" : "reason_code",
                    "type" : "string"
                }
            ]
        }
    ]
    
    "messages" :
    {
        "published_msgs" :
        {
            "doc" : "My Messages",
            "fields" :
            [
                {
                    "name" : "message_type",
                    "type" : "int"
                },
                {
                    "name" : "message_body",
                    "type" : 
                    [
                        "record_request", "request_response"
                    ]
                }
            ]
            
        }
    }
}
我在尝试读取这种模式时出错

我想知道-是否可以声明这样的AVRO模式-它有一个字段类型是复杂的用户定义消息结构的union

如果可能的话,你能告诉我我做错了什么,或者用union type字段的类型定义给出这样一个结构的例子吗

我想使用AVRO的动态模式用法——因此在运行时指定该模式文件,并将传入的缓冲区解析为“请求”/“响应”


谢谢,

可以定义复杂类型的并集,但模式的问题在于它没有在字段级别定义。要实现复杂类型的联合,您的模式必须如下所示

{
    "namespace": "proj.avro",
    "protocol": "app_messages",
    "doc" : "application messages",
    "name": "myRecord",
    "type" : "record",
    "fields": [
    {
    "name": "requestResponse",
    "type": [
        {
            "name": "record_request",
            "type" : "record", 
            "fields": 
            [
                {
                    "name" : "request_id", 
                    "type" : "int"
                },
                {
                    "name" : "message_type",
                    "type" : "int"
                },
                {
                    "name" : "users",
                    "type" : "string"
                }
            ]
        },
        {
            "name" : "request_response",
            "type" : "record",
            "fields" :
            [
                {
                    "name" : "request_id",
                    "type" : "int"
                },
                {
                    "name" : "response_code",
                    "type" : "string"
                },
                {
                    "name" : "response_count",
                    "type" : "int"
                },
                {
                    "name" : "reason_code",
                    "type" : "string"
                }
            ]
        }
    ]
    }
    ]
}

非常感谢。是否可以将模式定义为“类型”,而不是在消息结构中定义它们,即使用“类型”关键字。我们希望使用“类型”来定义它们,从可重用性和维护的角度来看,这很容易。特别是当我们添加更多的结构/类型时。它不是(至少我不知道)。我认为您应该根据您的应用程序类型尝试不同的解决方案,您是否尝试过schema registry?