You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@avro.apache.org by Vivek Hungund <Vi...@Incentica.net> on 2011/04/16 04:19:54 UTC

Union of a record and null in a schema

Hi,

I¹m trying to figure out how to make the location field in the following
schema a union of a record and null, similar to the way hometown is a union
of a string and null.

{
      "name" : "Profile",
      "type" : "record",
      "fields" : [
                      { "name" : "name", "type" : "string" },
                      { "name" : "location", "type" : {
                                                          "type" : "record",
                                                          "name" :
"cityState",
                                                          "fields" : [
                                                                        {
"name" : "city", "type" : [ "string" , "null" ] },
                                                                        {
"name" : "state", "type" : [ "string", "null" ] }
                                                                       ]
                                                      }
                      },
                    { "name" : "hometown", "type" : [ "string", "null" ] }
          ]
}

However, if I follow the pattern and make it:

                      { "name" : "location", "type" : [ {
                                                          "type" : "record",
                                                          "name" :
"cityState",
                                                          "fields" : [
                                                                        {
"name" : "city", "type" : [ "string" , "null" ] },
                                                                        {
"name" : "state", "type" : [ "string", "null" ] }
                                                                       ]
                                                      }, ³null² ]
                      },


I get the following error

org.apache.avro.AvroRuntimeException: Not a record schema:
[{"type":"record","name":"cityState","namespace":"com.incentica.avro.profile
s","fields":[{"name":"city","type":["string","null"]},{"name":"state","type"
:["string","null"]}]},"null"].message}

>From what I read in the specification, this should be allowed.  Is my syntax
incorrect?

Thanks.

Re: Union of a record and null in a schema

Posted by Doug Cutting <cu...@apache.org>.
It's hard to tell what's wrong without a complete program and a stack
trace, but I believe this error message is from the GenericData.Record()
constructor.  When you construct an instance of this you must pass in a
record schema, not the union schema.

Doug

On 04/15/2011 07:19 PM, Vivek Hungund wrote:
> Hi,
> 
> I’m trying to figure out how to make the location field in the following
> schema a union of a record and null, similar to the way hometown is a
> union of a string and null.
> 
> {
>       "name" : "Profile",
>       "type" : "record",
>       "fields" : [
>                       { "name" : "name", "type" : "string" },
>                       { "name" : "location", "type" : {
>                                                           "type" : "record",
>                                                           "name" :
> "cityState",
>                                                           "fields" : [
>                                                                         { "name"
> : "city", "type" : [ "string" , "null" ] },
>                                                                         { "name"
> : "state", "type" : [ "string", "null" ] }
>                                                                        ]
>                                                       }
>                       },
>                     { "name" : "hometown", "type" : [ "string", "null" ] }
>           ]
> }
> 
> However, if I follow the pattern and make it:
> 
>                       { "name" : "location", "type" : [ {
>                                                           "type" : "record",
>                                                           "name" :
> "cityState",
>                                                           "fields" : [
>                                                                         { "name"
> : "city", "type" : [ "string" , "null" ] },
>                                                                         { "name"
> : "state", "type" : [ "string", "null" ] }
>                                                                        ]
>                                                       }, “null” ]
>                       },
> 
> 
> I get the following error
> 
> _org.apache.avro.AvroRuntimeException_: Not a record schema:
> [{"type":"record","name":"cityState","namespace":"com.incentica.avro.profiles","fields":[{"name":"city","type":["string","null"]},{"name":"state","type":["string","null"]}]},"null"].message}
> 
> From what I read in the specification, this should be allowed.  Is my
> syntax incorrect?
> 
> Thanks.