I have a GenericRecord object of schema A, which is also a generated Avro Java class. Is it possible for me to cast this object into actual A type somehow?
You can convert an Avro GenericRecord to a SpecificRecord using deepCopy. For example, if "Foo" is the specific record:
Foo foo = (Foo) SpecificData.get().deepCopy(genericRecord.getSchema(), genericRecord)
This assumes that the SpecificRecord class corresponding to the schema passed is available to Avro.
Note that this approach does not support schema evolution: there is only one schema passed to the deepCopy and it is used for both reading and writing. This means the fields are copied based on their position, so the SpecificRecord schema must match the GenericRecord schema exactly.
See http://apache-avro.679487.n3.nabble.com/Direct-conversion-from-Generic-Record-to-Specific-Record-tp4029324p4029339.html.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With