You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@avro.apache.org by Koert Kuipers <ko...@tresata.com> on 2013/10/14 06:20:41 UTC

default values

i am testing parsing a json string using a record class defined in an .avdl
file.

i was hoping that if a field in the record has a default value that it is
not mandatory to provide it in the json. however i get an exception:

org.apache.avro.AvroTypeException: Expected field name not found: test123

i guess it was silly of me to expect this?
are default values only used in schema resolution (so reader vs writer
schema)? and in this case there is no writer schema, just json data i
created and a reader schema, so the default values will not help me?

too bad because this seemed like a really quick and convenient way to
create configuration objects that can be instantiated from json, but
without "optional" fields with default values it loses a lot of it power.

Re: default values

Posted by Harsh J <ha...@cloudera.com>.
At write time, fields can be expected to be null (via use of unions)
and have to be still present in some form, but the defaults in the
schema are only looked at during reads.

On Mon, Oct 14, 2013 at 9:50 AM, Koert Kuipers <ko...@tresata.com> wrote:
> i am testing parsing a json string using a record class defined in an .avdl
> file.
>
> i was hoping that if a field in the record has a default value that it is
> not mandatory to provide it in the json. however i get an exception:
>
> org.apache.avro.AvroTypeException: Expected field name not found: test123
>
> i guess it was silly of me to expect this?
> are default values only used in schema resolution (so reader vs writer
> schema)? and in this case there is no writer schema, just json data i
> created and a reader schema, so the default values will not help me?
>
> too bad because this seemed like a really quick and convenient way to create
> configuration objects that can be instantiated from json, but without
> "optional" fields with default values it loses a lot of it power.
>



-- 
Harsh J

Re: default values

Posted by Doug Cutting <cu...@apache.org>.
Note that builders do supply defaults.

http://avro.apache.org/docs/current/api/java/org/apache/avro/data/RecordBuilder.html

But that might not help you much here.

You might intersect your config json with the reader schema to determine
its implied writer schema.

Doug
On Oct 13, 2013 9:21 PM, "Koert Kuipers" <ko...@tresata.com> wrote:

> i am testing parsing a json string using a record class defined in an
> .avdl file.
>
> i was hoping that if a field in the record has a default value that it is
> not mandatory to provide it in the json. however i get an exception:
>
> org.apache.avro.AvroTypeException: Expected field name not found: test123
>
> i guess it was silly of me to expect this?
> are default values only used in schema resolution (so reader vs writer
> schema)? and in this case there is no writer schema, just json data i
> created and a reader schema, so the default values will not help me?
>
> too bad because this seemed like a really quick and convenient way to
> create configuration objects that can be instantiated from json, but
> without "optional" fields with default values it loses a lot of it power.
>
>