Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing nested fields in AVRO GenericRecord (Java/Scala)

Tags:

java

scala

avro

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.

like image 357
rye Avatar asked Jan 06 '23 13:01

rye


2 Answers

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.

like image 114
rye Avatar answered Jan 09 '23 02:01

rye


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)
like image 41
sksamuel Avatar answered Jan 09 '23 03:01

sksamuel