Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to convert a generic record to specific record with the same schema?

Tags:

java

avro

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?

like image 209
totoromeow Avatar asked Dec 03 '25 15:12

totoromeow


1 Answers

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.

like image 77
Raman Avatar answered Dec 05 '25 05:12

Raman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!