I have a GenericRecord with nested fields. When I use genericRecord.get(1)
it returns an Object that contains the nested AVRO data.
I want to be able to access that object like genericRecord.get(1).get(0)
, but I can't because AVRO returns an Object.
Is there an easy way around this?
When I do something like returnedObject.get("item")
it says item not a member of returnedObject
.
I figured out one way to do it. Cast the returned Object
as a GenericRecord
.
Example (scala):
val data_nestedObj = (data.get("nestedObj")).asInstanceOf[GenericRecord]
Then I can access a nested field within that new GenericRecord by doing:
data_nestedObj.get("nestedField")
This works well enough for me.
You could use an avro serialization library to help you. For example https://github.com/sksamuel/avro4s (I am the author) but there are others.
You just need to define a case class for the type of data you are getting, and this can include nested case classes. For example,
case class Boo(d: Boolean)
case class Foo(a: String, b: Int, c: Boo)
Then you create an instance of the RecordFormat typeclass.
val format = RecordFormat[Foo]
Then finally, you can use that to extract records or create records.
val record = format.to(someFoo)
or
val foo = format.from(someRecord)
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