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/03/11 22:21:48 UTC

Using custom defined types in schemas

Hi,

I want to be able to import and share a schema with multiple other schemas.
The schema is defined as such:

{ 
    "namespace" : "test.avro",
    "name" : "Address",
    "type" : "record",
    "fields" : [
                {"name" : "address1" , "type" : "string" },
                {"name" : "address2" , "type" : "string" },
                {"name" : "city" , "type" : "string" },
                {"name" : "state" , "type" : "string" },
                {"name" : "zip" , "type" : "int" },
                {"name" : "zip4", "type": "int" }
               ]
}

And I¹d like to refer to an Address in other schemas, e.g.:

{
      "namespace": "test.avro",
      "name" : "User",
      "type" : "record",
      "fields" : [
                                { "name": "name", "type": "string" },
                                { "name": "address", "type": { "type" : "
test.avro.Address" } }
          ]
}

Or

{
      "namespace": "test.avro",
      "name" : "Business",
      "type" : "record",
      "fields" : [
                                { "name": "businessName", "type": "string"
},
                                { "name": "address", "type": { "type" : "
test.avro.Address" } }
          ]
}


However when I try this I get the following error:
org.apache.avro.SchemaParseException: Type not supported: Address


Basically I want to have a generic Address schema that can be imported by a
number of different other schemas.  The motivation is to avoid having to
maintain an Address record for every User, Business, etc.  If this is not
possible, can someone recommend a workaround?  Is my syntax incorrect?

Thanks in advance.

Re: Using custom defined types in schemas

Posted by Doug Cutting <cu...@apache.org>.
A protocol definition can include multiple schema definitions, where
each schema defined can refer to previously defined schema definitions.
 A protocol need not include any messages, but can be used simply as a
vehicle to define a set of schemas.

Avro IDL permits imports of external schemas and protocols:

http://avro.apache.org/docs/current/idl.html#imports

If none of these are appropriate, you can use a pre-processor like cpp
or m4 to textually include schemas within other schemas.

Doug

On 03/11/2011 01:21 PM, Vivek Hungund wrote:
> Hi,
> 
> I want to be able to import and share a schema with multiple other
> schemas.  The schema is defined as such:
> 
> {
>     "_namespace_" : "test.avro",
>     "name" : "Address",
>     "type" : "record",
>     "fields" : [
>                 {"name" : "address1" , "type" : "string" },
>                 {"name" : "address2" , "type" : "string" },
>                 {"name" : "city" , "type" : "string" },
>                 {"name" : "state" , "type" : "string" },
>                 {"name" : "_zip_" , "type" : "_int_" },   
>                 {"name" : "zip4", "type": "_int_" }    
>                ]
> }
> 
> And I’d like to refer to an Address in other schemas, e.g.:
> 
> {
>       "namespace": "test.avro",
>       "name" : "User",
>       "type" : "record",
>       "fields" : [
>                                 { "name": "name", "type": "string" },
>                                 { "name": "address", "type": { "type" :
> " test.avro.Address" } }
>           ]
> }
> 
> Or
> 
> {
>       "namespace": "test.avro",
>       "name" : "Business",
>       "type" : "record",
>       "fields" : [
>                                 { "name": "businessName", "type":
> "string" },
>                                 { "name": "address", "type": { "type" :
> " test.avro.Address" } }
>           ]
> }
> 
> 
> However when I try this I get the following error:
> _org.apache.avro.SchemaParseException_: Type not supported: Address
> 
> 
> Basically I want to have a generic Address schema that can be imported
> by a number of different other schemas.  The motivation is to avoid
> having to maintain an Address record for every User, Business, etc.  If
> this is not possible, can someone recommend a workaround?  Is my syntax
> incorrect?
> 
> Thanks in advance.