Ani
Ani

Reputation: 463

Not able to specify user defined type in Avro Schema 1.7.7

I am trying to figure out why I am not able to use media_type in "message" avro schema:

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

When I am trying to parse the schema, I am getting following error:

Exception in thread "main" org.apache.avro.SchemaParseException: Undefined name: "media_type" at org.apache.avro.Schema.parse(Schema.java:1162) at org.apache.avro.Schema.parse(Schema.java:1272) at org.apache.avro.Schema.parse(Schema.java:1203)

I am parsing as: InputStream in = ClasName.class.getResourcesAsStream(""); Schema sc = new Parser().parse(in)

It is working perfectly when I am using only json data types. How can I define a value for my designed data type?

Upvotes: 1

Views: 1786

Answers (1)

Ani
Ani

Reputation: 463

I was able to do that by changing the data to:

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

Upvotes: 1

Related Questions