You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@avro.apache.org by Wai Yip Tung <wy...@tungwaiyip.info> on 2014/02/19 17:41:56 UTC

default write value in schema?

In the schema we can define a "default" value. It is used as default 
when reading. If the field is missing from the data when we write, it 
results in an error. Is there a counterpart of "default" in writing so 
that if the field is missing, the writer just fill in the default value?

Wai Yip

Re: default write value in schema?

Posted by Gary Steelman <ga...@gmail.com>.
I've had some issues using builders in the past: building a new object
which has required fields in the schema (the type is not a union with null)
but that field doesn't specify a default value.

I realize the above is a corner case, but the schemas I've been given are
poorly designed, so I had to work around it.

When a default value is not specified and a field is optional, will a
builder for a brand new object set the value to null in the Java object?
On Feb 19, 2014 5:44 PM, "Lewis John Mcgibbney" <le...@gmail.com>
wrote:

> +1 we do this in Gora.
> Please note that there are some overheads with using the builders...
> "namely Avro objects can be created either by invoking a constructor
> directly or by using a builder. Unlike constructors, builders will
> automatically set any default values specified in the schema. Additionally,
> builders validate the data as it set, whereas objects constructed directly
> will not cause an error until the object is serialized. However, using
> constructors directly generally offers better performance, as builders
> create a copy of the datastructure before it is written."
>
> Please see
> http://avro.apache.org/docs/current/gettingstartedjava.html#Creating+Users
>
> hth
> Lewis
>
>
> On Wed, Feb 19, 2014 at 5:39 PM, Christophe Taton <ta...@wibidata.com>wrote:
>
>> Hi,
>> I'd suggest using the record builders to construct your records: record
>> builders will initialize fields with their default values if you don't set
>> them.
>>  C.
>>
>>
>> On Wed, Feb 19, 2014 at 9:07 AM, Gary Steelman <gary.steelman42@gmail.com
>> > wrote:
>>
>>> Hey Wai,
>>>
>>> Good question. I've had that problem recently too. My solution has been
>>> to set the missing field to a known default value in my code before I try
>>> to serialize an object. That is, if some integer is required by the schema,
>>> I do a check on my object.
>>>
>>> if (myObject.value == null) {myObject.value = -1;}
>>>
>>> But then you have to do this for all fields in the schema which aren't
>>> optional. And that's kind of a pain. It'd be kind of nice if the writer
>>> also used the default value from the schema for writing.
>>>
>>> Thanks,
>>> Gary
>>> On Feb 19, 2014 10:42 AM, "Wai Yip Tung" <wy...@tungwaiyip.info> wrote:
>>>
>>>> In the schema we can define a "default" value. It is used as default
>>>> when reading. If the field is missing from the data when we write, it
>>>> results in an error. Is there a counterpart of "default" in writing so that
>>>> if the field is missing, the writer just fill in the default value?
>>>>
>>>> Wai Yip
>>>>
>>>
>>
>
>
> --
> *Lewis*
>

Re: default write value in schema?

Posted by Lewis John Mcgibbney <le...@gmail.com>.
+1 we do this in Gora.
Please note that there are some overheads with using the builders...
"namely Avro objects can be created either by invoking a constructor
directly or by using a builder. Unlike constructors, builders will
automatically set any default values specified in the schema. Additionally,
builders validate the data as it set, whereas objects constructed directly
will not cause an error until the object is serialized. However, using
constructors directly generally offers better performance, as builders
create a copy of the datastructure before it is written."

Please see
http://avro.apache.org/docs/current/gettingstartedjava.html#Creating+Users

hth
Lewis


On Wed, Feb 19, 2014 at 5:39 PM, Christophe Taton <ta...@wibidata.com>wrote:

> Hi,
> I'd suggest using the record builders to construct your records: record
> builders will initialize fields with their default values if you don't set
> them.
> C.
>
>
> On Wed, Feb 19, 2014 at 9:07 AM, Gary Steelman <ga...@gmail.com>wrote:
>
>> Hey Wai,
>>
>> Good question. I've had that problem recently too. My solution has been
>> to set the missing field to a known default value in my code before I try
>> to serialize an object. That is, if some integer is required by the schema,
>> I do a check on my object.
>>
>> if (myObject.value == null) {myObject.value = -1;}
>>
>> But then you have to do this for all fields in the schema which aren't
>> optional. And that's kind of a pain. It'd be kind of nice if the writer
>> also used the default value from the schema for writing.
>>
>> Thanks,
>> Gary
>> On Feb 19, 2014 10:42 AM, "Wai Yip Tung" <wy...@tungwaiyip.info> wrote:
>>
>>> In the schema we can define a "default" value. It is used as default
>>> when reading. If the field is missing from the data when we write, it
>>> results in an error. Is there a counterpart of "default" in writing so that
>>> if the field is missing, the writer just fill in the default value?
>>>
>>> Wai Yip
>>>
>>
>


-- 
*Lewis*

Re: default write value in schema?

Posted by Wai Yip Tung <wy...@tungwaiyip.info>.
Thank you. I'm using the Python API. It seems record builder is not 
available. But it is good to know the general approach for initializing 
with default value.

Wai Yip

Christophe Taton wrote:
>
> Hi,
> I'd suggest using the record builders to construct your records:
> record builders will initialize fields with their default values if
> you don't set them.
> C.
>
> On Wed, Feb 19, 2014 at 9:07 AM, Gary Steelman
> <gary.steelman42@gmail.com <ma...@gmail.com>> wrote:
>
> Hey Wai,
>
> Good question. I've had that problem recently too. My solution has
> been to set the missing field to a known default value in my code
> before I try to serialize an object. That is, if some integer is
> required by the schema, I do a check on my object.
>
> if (myObject.value == null) {myObject.value = -1;}
>
> But then you have to do this for all fields in the schema which
> aren't optional. And that's kind of a pain. It'd be kind of nice
> if the writer also used the default value from the schema for
> writing.
>
> Thanks,
> Gary
>
> On Feb 19, 2014 10:42 AM, "Wai Yip Tung" <wy@tungwaiyip.info
> <ma...@tungwaiyip.info>> wrote:
>
> In the schema we can define a "default" value. It is used as
> default when reading. If the field is missing from the data
> when we write, it results in an error. Is there a counterpart
> of "default" in writing so that if the field is missing, the
> writer just fill in the default value?
>
> Wai Yip
>

Re: default write value in schema?

Posted by Christophe Taton <ta...@wibidata.com>.
Hi,
I'd suggest using the record builders to construct your records: record
builders will initialize fields with their default values if you don't set
them.
C.

On Wed, Feb 19, 2014 at 9:07 AM, Gary Steelman <ga...@gmail.com>wrote:

> Hey Wai,
>
> Good question. I've had that problem recently too. My solution has been to
> set the missing field to a known default value in my code before I try to
> serialize an object. That is, if some integer is required by the schema, I
> do a check on my object.
>
> if (myObject.value == null) {myObject.value = -1;}
>
> But then you have to do this for all fields in the schema which aren't
> optional. And that's kind of a pain. It'd be kind of nice if the writer
> also used the default value from the schema for writing.
>
> Thanks,
> Gary
> On Feb 19, 2014 10:42 AM, "Wai Yip Tung" <wy...@tungwaiyip.info> wrote:
>
>> In the schema we can define a "default" value. It is used as default when
>> reading. If the field is missing from the data when we write, it results in
>> an error. Is there a counterpart of "default" in writing so that if the
>> field is missing, the writer just fill in the default value?
>>
>> Wai Yip
>>
>

Re: default write value in schema?

Posted by Gary Steelman <ga...@gmail.com>.
Hey Wai,

Good question. I've had that problem recently too. My solution has been to
set the missing field to a known default value in my code before I try to
serialize an object. That is, if some integer is required by the schema, I
do a check on my object.

if (myObject.value == null) {myObject.value = -1;}

But then you have to do this for all fields in the schema which aren't
optional. And that's kind of a pain. It'd be kind of nice if the writer
also used the default value from the schema for writing.

Thanks,
Gary
On Feb 19, 2014 10:42 AM, "Wai Yip Tung" <wy...@tungwaiyip.info> wrote:

> In the schema we can define a "default" value. It is used as default when
> reading. If the field is missing from the data when we write, it results in
> an error. Is there a counterpart of "default" in writing so that if the
> field is missing, the writer just fill in the default value?
>
> Wai Yip
>