I think that the easiest way is to
make a class with list,
for example:
public class AvroObj {
private List<TestObj> list;
public List<TestObj> getList() {
return list;
}
public void setList(List<TestObj> list) {
this.list = list;
}
}
create a schema:
Schema schema = ReflectData.get().getSchema(AvroObj.class);
serialize it to bytes using ReflectDatumWriter (i offer it cause with another datum writers you may catch ClassCastException)
try(ByteArrayOutputStream out = new ByteArrayOutputStream()) {
BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(out, null);
DatumWriter<AvroObj> writer = new ReflectDatumWriter<>(schema);
writer.write(avroObj, encoder);
encoder.flush();
byte[] bytes = out.toByteArray();
}
then send bytes with kafka producer.
deserialise bytes that consumer received:
DatumReader<AvroObj> reader1 = new ReflectDatumReader<AvroObj>(schema);
Decoder decoder = DecoderFactory.get().binaryDecoder(bytes, null);
AvroObj decodedAvroObj = reader1.read(null, decoder);