avro-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Scott Carey <sc...@richrelevance.com>
Subject Re: schema defaults not reflected in generated objects (1.3.2)
Date Mon, 07 Jun 2010 21:33:26 GMT
No, it should not initialize the field to the default.

Default values are for readers, not writers.   The intended use case is schema evolution.
  A reader on version N+1 has field foo with default 'quux', but reads data that was written
without that field.  The reader will see foo = 'quux'.

A writer must always correctly provide data for all of the fields in the schema it declared
it is writing.

The specific API does make this a bit more difficult than it needs to be, it is something
I would like to address at a later time.  In the meantime I use wrapper classes around the
SpecificRecord objects that provide restricted getters and restrictive constructors or factory
methods to prevent other users from forgetting to set a field.

-Scott

On Jun 7, 2010, at 1:53 PM, Bill de hOra wrote:

> Hi,
> 
> I.m wondering if I'm missing an incantation for 'compile 
> schema/protocol' in Avro tools to handle defaults. Given this schema,
> 
> [[[
> {
>     "name": "avro.Message",
>     "type": "record",
>     "fields": [
>         {
>             "name": "foo",
>             "type": "string",
>             "default":"quux"
>         },
>         {
>             "name": "bar",
>             "type": "string"
>         }
>     ]
> 
> }
> ]]]
> 
> and running it through 'avro-tools-1.3.2.jar compile schema', I get this 
> object
> 
> [[[
> package avro;
> 
> @SuppressWarnings("all")
> public class Message extends org.apache.avro.specific.SpecificRecordBase 
> implements org.apache.avro.specific.SpecificRecord {
>   public static final org.apache.avro.Schema SCHEMA$ = 
> org.apache.avro.Schema.parse("{\"type\":\"record\",\"name\":\"Message\",\"namespace\":\"avro\",\"fields\":[{\"name\":\"foo\",\"type\":\"string\",\"default\":\"quux\"},{\"name\":\"bar\",\"type\":\"string\"}]}");
>   public org.apache.avro.util.Utf8 foo = new Utf8("quux");
>   public org.apache.avro.util.Utf8 bar;
>   public org.apache.avro.Schema getSchema() { return SCHEMA$; }
>   public java.lang.Object get(int field$) {
>     switch (field$) {
>     case 0: return foo;
>     case 1: return bar;
>     default: throw new org.apache.avro.AvroRuntimeException("Bad index");
>     }
>   }
>   @SuppressWarnings(value="unchecked")
>   public void put(int field$, java.lang.Object value$) {
>     switch (field$) {
>     case 0: foo = (org.apache.avro.util.Utf8)value$; break;
>     case 1: bar = (org.apache.avro.util.Utf8)value$; break;
>     default: throw new org.apache.avro.AvroRuntimeException("Bad index");
>     }
>   }
> }
> ]]]
> 
> Should this line,
> 
> [[[
>   public org.apache.avro.util.Utf8 foo;
> ]]]
> 
> be,
> 
> [[[
>   public org.apache.avro.util.Utf8 foo
>     = new org.apache.avro.util.Utf8("quux");
> ]]]
> 
> ?
> 
> Fwiw, changing the object's 'foo' field above to have the default stops 
> the IPC server from barfing with an NPE (@ 
> org.apache.avro.io.BinaryEncoder.writeString(BinaryEncoder.java:133)) 
> when a client creates a message object only sets 'bar', and then sends 
> the Message to the server via .
> 
> Bill
> 
> 
> 


Mime
View raw message