avoid generating schema as string
You could use SchemaBuilder
to avoid that problem and give you a type-safe way to programmatically define a schema.
If there is not a specific reason to start with Java (which can be done with @AvroEncode
annotation or ReflectData), it is far easier to start with an IDL file
A direct translation of those POJO classes would look like this
protocol EventProtocol {
record Book {
union {null, string} name;
union {null, string} author;
}
record LibraryEvent {
union {null, string} name;
int id;
union {null, Book} book;
}
}
Then the Maven plugin mentioned in the docs would create those two classes with the AVSC schema embedded within each class as a static field.
You still need to "fill record explicitly" since this just creates the classes, doesn't call any setter methods
If you are still against being schema-first, and if you are using Kafka with the Confluent Avro Serializers, they have a section on using reflection