【问题标题】:Not able to specify user defined type in Avro Schema 1.7.7无法在 Avro Schema 1.7.7 中指定用户定义的类型
【发布时间】:2016-10-29 03:10:31
【问题描述】:

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

{
    "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"
                                ]
                            }
                        ]
                    }
                }
            ]
        }
    }]
}

当我尝试解析架构时,出现以下错误:

线程“主”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 数据类型时,它工作得很好。如何为我设计的数据类型定义一个值?

【问题讨论】:

    标签: json avro


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2017-01-21
      • 1970-01-01
      • 2020-07-27
      • 1970-01-01
      • 2019-02-19
      • 1970-01-01
      • 1970-01-01
      • 2019-11-15
      • 2016-07-21
      相关资源
      最近更新 更多