Home | About | Sematext search-lucene.com search-hadoop.com
 Search Hadoop and all its subprojects:

Switch to Threaded View
Avro >> mail # user >> Setting field default value's programmatically

Copy link to this message
Setting field default value's programmatically
Hi user@,
Over in Apache Gora we've nearly made the upgrade of our Avro dependency
from legacy 1.3.3 to 1.7.5. For those that may be interested, the branch is
here [0].
The current issue I am working on concerns the following

In Gora we define our objects using Avro Schema's before using our
Velocity-driven [1] GoraCompiler [2] to compile the schema's down into
persistent Java beans.

Right now we wish for our compiler to recognize if a field default value
(within the schema is absent) and simply enforce a default value of null.

An example schema can be found here [3]. So instead of forcing the user to

{"name": "url", "type": "string", "default": ""},

we would instead prefer for them to be able to assume that a field can/may
be empty and they
can carry or as usual like so...

{"name": "url", "type": "string"},

Now, as an example to explain the problem I am having, if we assume that
the example schema [3] has been compiled and that I am constructing a new
persistent WebPage object like so

WebPage page1 = WebPage.newBuilder().build();

we come back to the velocity template [1] build() method where the default
value for each field is specified like so

(${this.javaType($field.schema())}) defaultValue(fields()[$field.pos()]);

We've therefore tried substituting the above with a function which does the

  private ${this.javaType($field.schema())} getNewValue() {
     ${this.javaType($field.schema())} newValue = null;
     if (defaultValue(fields()[$field.pos()]) != null) {
         newValue = (${this.javaType($field.schema())})
     return newValue;

However I always get the following
AvroRuntimeException: Field url type:STRING pos:1 not set and has no
default value

Can someone please put me out of my short term misery and explain

1) Why my code is not working in this case? and
2) Why org.apache.avro.data.RecordBuilderBase#defaultValue(Field field) is
not returning null if no default is specified in the schema... which is the

Apologies for the long post. I hope I've explained thoroughly enough. I
really thank you for your time here.

[0] https://svn.apache.org/repos/asf/gora/branches/GORA_94/
[1] http://s.apache.org/CAX
[2] http://s.apache.org/lMZ
[3] http://s.apache.org/rF