How to define Avro merge in java

I defined an Avro schema consisting of a record that contains a union of two (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 look very clear, but I hope you understand: I have a record consisting of some data ("SomeCommonData" and "MoreCommonData") and a combination of two different types of records ("FirstOption" and "FirstOption", SecondOption ") At the time of serialization / deserialization, I should be able to create one of two subrecords and serialize "MyCompositeRecord".

I have not tried to generate code for the circuit, since I plan to use only general records. However, I am not sure what and how such shared records can be serialized. I can not find a single example on the Internet. I am going to use java for serialization / deserialization. I was able to create a reader script for the circuit as 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 to proceed from here to actually create a record?

thanks

+4
source share
1 answer

In principle, for combining, this is no different from setting any other field:

 GenericRecord datum = new GenericData.Record(schema); datum.set(1, data); 

where 1 is the number of the connection field, and data is the value.

If you look at getDefaultValue in AvroEditor - Helper , you will see the default values ​​that I use for each type of Avro. Arrays must implement a GenericArray .

+2
source

All Articles