17

To facilitate working with Avro in Scala, I'd like to define a case class based on the schema stored with a .avro file. I could try:

  1. Writing a .scala case class definition by hand.
  2. Programmatically writing strings to a .scala file
  3. Spoof the case class definition with a bytecode library like ObjectWeb's ASM
  4. SpecificCompiler tricks?
  5. Modifying an existing case classed definition at runtime?

Thanks, any advice is appreciated. -Julian

Julian Peeters
  • 853
  • 1
  • 6
  • 19

2 Answers2

14

I've been hacking on a little project called Scalavro to go the other way (Scala types to Avro schemas). It also gives you direct binary I/O.

Simple Example:

package com.gensler.scalavro.tests
import com.gensler.scalavro.types.AvroType

case class Person(name: String, age: Int)

val personAvroType = AvroType[Person]
personAvroType.schema

which yields:

{
  "name": "Person",
  "type": "record",
  "fields": [
    {"name": "name", "type": "string"},
    {"name": "age", "type": "int"}
  ],
  "namespace": "com.gensler.scalavro.tests"
}

There are many more examples on the project site (linked above) and in the scalatest specs.

I have plans to host the binaries on Sonatype OSS in the near future, but for now you can pull the source from github and sbt publish-local if you want to try it out.

Update:
Scalavro is now available on Maven Central.

Connor Doyle
  • 1,812
  • 14
  • 22
2

Look like you got your hands dirty with type providers at https://github.com/julianpeeters/avro-scala-macro-annotations

Guillaume Massé
  • 8,004
  • 8
  • 44
  • 57