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

Switch to Threaded View
Avro >> mail # user >> Are optional fields possible?


Copy link to this message
-
Are optional fields possible?
Hi all,

Is it possible in the schema to indicate that a filed is optional?

I have things like:

          {"name": "userId", "type": "string", "default": null},
          {
           "name": "level",
           "type":
           {
          "name": "LEVEL_TYPES",
           "type": "enum",
          "symbols" : ["DEBUG", "INFO", "WARN", "ERROR"]
       },
       "default": 0
          },

and Java serliazation code:

HttpTransceiver client = new HttpTransceiver(server);
LogEvent proxy = (LogEvent)
SpecificRequestor.getClient(LogEvent.class, client);
Event e = new Event();
e.level=LEVEL_TYPES.DEBUG;
... more setters
proxy.log(e);

but I seem to have to set a value to every field in the schema, or else I get:

Exception in thread "main" java.lang.NullPointerException
at org.apache.avro.specific.SpecificDatumWriter.writeEnum(SpecificDatumWriter.java:54)
at org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:63)
at org.apache.avro.generic.GenericDatumWriter.writeRecord(GenericDatumWriter.java:89)
at org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:62)
at org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:55)
at org.apache.avro.specific.SpecificRequestor.writeRequest(SpecificRequestor.java:69)
at org.apache.avro.ipc.Requestor.request(Requestor.java:109)
at org.apache.avro.specific.SpecificRequestor.invoke(SpecificRequestor.java:52)
at $Proxy0.log(Unknown Source)
at org.gbif.portalanalytics.client.GBIFLogger.info(GBIFLogger.java:41)
at org.gbif.portalanalytics.client.GBIFLogger.main(GBIFLogger.java:67)

Is it expected to have to have a value on each field?
For Enums this means I would need a NULL in the Enum explicitly I think.

Thanks for any pointers!
Tim