avro-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Nick Beenham <nick.been...@gmail.com>
Subject Serialization of Optional Fields
Date Mon, 03 Nov 2014 18:55:55 GMT
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

Mime
View raw message