This code below returns type T and uses SpecificDatumReader<T>.
final BinaryDecoder decoder = DecoderFactory.get().binaryDecoder(...);
final DatumReader<T> datumReader = new SpecificDatumReader<T>(v2Schema,
v1Schema);
try {
final T record = datumReader.read(null, decoder);
return record;
} catch (java.io.IOException ioe) {
// Handle it
}
On Mon, Apr 19, 2021 at 4:30 PM Stephen Kittelson <
[email protected]> wrote:
> Let's say I used avro-tools to generate code for v2 of "mySchema", and a
> message comes on a queue that was written with v1 of "mySchema". If I
> understand right, the best way to handle the situation is to realize that
> the message was written with v1 of "mySchema", get the v1 schema using a
> schema registry, and deserialize the message using v1.
>
> At this point, is it at all possible to somehow convert the resulting
> objects into the corresponding fields and objects from the avro-tools
> generated code for v2?
>
> So far, it looks like the only way to handle the message results in a
> "GenericRecord" which requires string keys to access the values of the
> fields. I'd much prefer to use the generated code for v2 and turn any typos
> into compile errors instead of runtime errors, know the data type of the
> value while coding, and avoid setting up a bunch of enums to contain the
> string keys.
>
> I also asked this on StackOverflow:
> https://stackoverflow.com/questions/67170438/how-to-convert-an-avro-message-deserialized-with-an-older-version-of-the-schema
> <https://urldefense.com/v3/__https://stackoverflow.com/questions/67170438/how-to-convert-an-avro-message-deserialized-with-an-older-version-of-the-schema__;!!NCc8flgU!MEeZAEsURGjKdqMhk5sA6zxMvb16BTwHvOnHY2fSmdNueabHsiG7syKxaa_0rm6I$>
>
> --
> Stephen
>