serialization - How to define Avro union in java -
i have defined avro schema composed of record contains union of 2 (or more) different records such as:
{ "type":"record", "name":"mycompositerecord", "fields": [ {"name":"somecommondata","type":"string"}, {"name":"morecommondata","type":"float"}, {"name":"compositerecord","type": [ { "type":"record", "name":"firstoption", "fields": [ {"name":"x","type":"string"}, {"name":"y","type":"long"} ] }, { "type":"record", "name":"secondoption", "fields": [ {"name":"z","type":"int"}, {"name":"w","type":"float"}, {"name":"m","type":"double"}, {"name":"l","type":"boolean"} ] } ] } ] }
it doesn't clear hope idea: have record composed of data ("somecommondata" , "morecommondata") , union of 2 different types of records ("firstoption" , "secondoption"). @ serialization/deserialization time should able create either 1 of 2 sub-records , serialize "mycompositerecord".
i haven't tried generating code schema since i'm planning on using generic records. however, i'm not sure if , how such generic records can serialized. can't find example online. i'm going use java serialized/deserialize. able create writer/reader schema follows:
schema.parser parser = new schema.parser(); schema schema = parser.parse(composite_schema); datumwriter<genericrecord> writer = new genericdatumwriter<>(schema); datumreader<genericrecord> reader = new genericdatumreader<>(schema); genericrecord datum = new genericdata.record(schema);
any ideas on how proceed here build record?
thanks
basically union no different setting other field:
genericrecord datum = new genericdata.record(schema); datum.set(1, data);
where 1 union field number , data value being set.
if @ getdefaultvalue in avroeditor - helper, see default values use each avro type. arrays must implement genericarray.
Comments
Post a Comment