无法在Avro Schema 1.7.7

时间:2016-06-27 07:35:19

标签: json avro

我想弄清楚为什么我无法在media_type avro架构中使用"message"

{
    "namespace": "com.ci.generated",
    "name": "MessageEnvelope",
    "type": "record",
    "fields": [{
        "name": "message",
        "type": {
            "name": "Message",
            "type": "record",
            "fields": [{
                    "name": "media_type",
                    "type": {
                        "type": "enum",
                        "name": "MediaTypes",
                        "symbols": ["Text", "Image"]
                    }
                },
                {
                    "name": "text_record",
                    "type": {
                        "type": "record",
                        "name": "TextRecords",
                        "fields": [{
                            "name": "name",
                            "type": "string"
                        }]
                    }
                },
                {
                    "name": "image_record",
                    "type": {
                        "name": "ImageRecords",
                        "type": "record",
                        "fields": [{
                                "name": "url",
                                "type": "string"
                            },
                            {
                                "name": "sentiment",
                                "type": {
                                    "name": "ImageSentiments",
                                    "type": "enum",
                                    "symbols": ["positive", "negetive", "neutral"]
                                }
                            }
                        ]
                    }
                },
                {
                    "name": "named_media",
                    "type": {
                        "name": "NamedMedia",
                        "type": "record",
                        "fields": [{
                                "name": "type",
                                "type": ["media_type"]
                            },
                            {
                                "name": "index",
                                "type": "int"
                            },
                            {
                                "name": "name",
                                "type": ["null", "string"]
                            },
                            {
                                "name": "data",
                                "type": [
                                    "null",
                                    "text_record",
                                    "image_record"
                                ]
                            }
                        ]
                    }
                }
            ]
        }
    }]
}

当我尝试解析架构时,我收到以下错误:

  

线程中的异常" main" org.apache.avro.SchemaParseException:未定义的名称:" media_type"           在org.apache.avro.Schema.parse(Schema.java:1162)           在org.apache.avro.Schema.parse(Schema.java:1272)           在org.apache.avro.Schema.parse(Schema.java:1203)

我正在解析为: InputStream in = ClasName.class.getResourcesAsStream(""); Schema sc = new Parser().parse(in)

当我只使用json数据类型时,它工作正常。如何为我设计的数据类型定义值?

1 个答案:

答案 0 :(得分:0)

我可以通过将数据更改为:

来实现
{"name": "data", "type": [
        "null",
        "TextRecord",
        "ImageRecords"
    ] 
}