Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to define Avro union in java
    primarykey
    data
    text
    <p>I have defined an Avro schema composed of a record which contains a union of two (or more) different records such as:</p> <pre><code>{ "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"} ] } ] } ] } </code></pre> <p>It doesn't look very clear but I hope you get the idea: I have a record composed of some data ("SomeCommonData" and "MoreCommonData") and a union of two different types of records ("FirstOption" and "SecondOption"). At serialization/deserialization time I should be able to create either one of the two sub-records and serialize a "MyCompositeRecord".</p> <p>I haven't tried generating code for the schema since I'm planning on using just generic records. However, I'm not sure if and how such generic records can be serialized. I can't find any example online. I'm going to use java to serialized/deserialize. I was able to create a writer/reader for the schema as follows:</p> <pre><code>Schema.Parser parser = new Schema.Parser(); Schema schema = parser.parse(COMPOSITE_SCHEMA); DatumWriter&lt;GenericRecord&gt; writer = new GenericDatumWriter&lt;&gt;(schema); DatumReader&lt;GenericRecord&gt; reader = new GenericDatumReader&lt;&gt;(schema); GenericRecord datum = new GenericData.Record(schema); </code></pre> <p>Any ideas on how to proceed from here to actually build the record?</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload