Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I get a Scala case class definition from an Avro schema definition?

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

like image 596
Julian Peeters Avatar asked Mar 25 '13 03:03

Julian Peeters


2 Answers

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.

like image 161
Connor Doyle Avatar answered Oct 01 '22 20:10

Connor Doyle


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

like image 30
Guillaume Massé Avatar answered Oct 01 '22 19:10

Guillaume Massé