You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@avro.apache.org by Nick Beenham <ni...@gmail.com> on 2014/11/03 19:55:55 UTC

Serialization of Optional Fields

Hi all,

I'm running into a problem with optional fields from an xsd that don't
always get initialized in the java pojo.

I have a specific issue with Avro serialization of Java Objects. I have
POJO's generated from xsd schemas which I am then trying to serialize using
avro to place on a kafka topic. Some of the xmlElements are optional

A test message looks like this:

@XmlRootElement(name = "message")public class Testmessage {

  @XmlElement
  public String id;


  @XmlElement
  public String name;

  public Testmessage(String id, String name) {
    this.id = id;
    this.name = name;
  }

  public Testmessage() { }

  @Override
  public String toString() {
    return "Message{" +
        "id='" + id + '\'' +
        ", name=" + name +
        '}';
  }}

And the method to serialize and place on the topic is:

public void sendMessage(Testmessage msg) throws Exception{

    DatumWriter<Testmessage> writer = new
ReflectDatumWriter<Testmessage>(Testmessage.class);
    ByteArrayOutputStream os = new ByteArrayOutputStream();

    Encoder encoder = EncoderFactory.get().binaryEncoder(os, null);
    writer.write(msg, encoder);
    encoder.flush();
    os.close();
    KeyedMessage<String, byte[]> data = new KeyedMessage<String,
byte[]>(TOPIC_NAME, os.toByteArray());

    producer.send(data);
}

When I send both fields all works as expected. If I null one of the fields
or leave it out I get NPE's from the write.

java.lang.NullPointerException: in Testmessage in string null of
string in field id of    Testmessage
at org.apache.avro.reflect.ReflectDatumWriter.write(ReflectDatumWriter.java:145)
at org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:58)

Any ideas? or point me in the right direction

Thanks!


cross posted here -
http://stackoverflow.com/questions/26720257/nullpointerexception-in-avro-reflectdatumwriter

Re: Serialization of Optional Fields

Posted by Lewis John Mcgibbney <le...@gmail.com>.
Have you set a value for the Id field?
If you do not always anticipate the field having a value then maybe you
need to make it a Union field type which accepts null and String but which
default to a value of null.
hth
Lewis

On Mon, Nov 3, 2014 at 10:55 AM, Nick Beenham <ni...@gmail.com>
wrote:

> Hi all,
>
> I'm running into a problem with optional fields from an xsd that don't
> always get initialized in the java pojo.
>
> I have a specific issue with Avro serialization of Java Objects. I have
> POJO's generated from xsd schemas which I am then trying to serialize using
> avro to place on a kafka topic. Some of the xmlElements are optional
>
> A test message looks like this:
>
> @XmlRootElement(name = "message")public class Testmessage {
>
>   @XmlElement
>   public String id;
>
>
>   @XmlElement
>   public String name;
>
>   public Testmessage(String id, String name) {
>     this.id = id;
>     this.name = name;
>   }
>
>   public Testmessage() { }
>
>   @Override
>   public String toString() {
>     return "Message{" +
>         "id='" + id + '\'' +
>         ", name=" + name +
>         '}';
>   }}
>
> And the method to serialize and place on the topic is:
>
> public void sendMessage(Testmessage msg) throws Exception{
>
>     DatumWriter<Testmessage> writer = new ReflectDatumWriter<Testmessage>(Testmessage.class);
>     ByteArrayOutputStream os = new ByteArrayOutputStream();
>
>     Encoder encoder = EncoderFactory.get().binaryEncoder(os, null);
>     writer.write(msg, encoder);
>     encoder.flush();
>     os.close();
>     KeyedMessage<String, byte[]> data = new KeyedMessage<String, byte[]>(TOPIC_NAME, os.toByteArray());
>
>     producer.send(data);
> }
>
> When I send both fields all works as expected. If I null one of the fields
> or leave it out I get NPE's from the write.
>
> java.lang.NullPointerException: in Testmessage in string null of string in field id of    Testmessage
> at org.apache.avro.reflect.ReflectDatumWriter.write(ReflectDatumWriter.java:145)
> at org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:58)
>
> Any ideas? or point me in the right direction
>
> Thanks!
>
>
> cross posted here -
> http://stackoverflow.com/questions/26720257/nullpointerexception-in-avro-reflectdatumwriter
>



-- 
*Lewis*