Schema-to-case-class code generation for working with Avro in Scala.
avrohugger-core
: Generate source code dynamically at runtime for evaluation at a later stepavrohugger-tools
: Generate source code at the command line with the avrohugger-tools jar.sbt-avrohugger
: Generate source code at compile time with an sbt plugin.
Generates Scala case classes:
-
Vanilla case classes (for use with Scalavro, Salat-Avro, gfc-avro, etc.)
-
Case classes that implement
SpecificRecordBase
(for use with the Avro Specific API - Scalding, Spark, Avro, etc.).
Supports generating case classes with arbitrary fields of the following datatypes:
- INT -> Int
- LONG -> Long
- FLOAT -> Float
- DOUBLE -> Double
- STRING -> String
- BOOLEAN -> Boolean
- NULL -> Null
- MAP -> Map
- ENUM ->
generate
: scala.Enumeration,generate-specific
: Java Enum - BYTES -> //TODO
- FIXED -> //TODO
- ARRAY -> List
- UNION -> Option
- RECORD -> case class
####avrohugger-core
Get the dependency with:
"com.julianpeeters" % "avrohugger-core" %% "0.4.0"
Instantiate a Generator
with Standard
or SpecificRecord
source formats. Then use
tToFile(input: T, outputDir: String): Unit
or
tToStrings(input: T): List[String]
where 'T' can be File
, Schema
, or String
:
import avrohugger._
import format._
val schemaFile = new File("path/to/schema")
val generator = new Generator(SpecificRecord)
generator.fileToFile(schemaFile, "optional/path/to/output") // default output path = "target/generated-sources"
####avrohugger-tools
Download the avrohugger-tools jar for Scala 2.10 or Scala 2.11(20MB!) and use it like the avro-tools jar Usage: [-string] (schema|protocol|datafile) input... outputdir
:
-
'generate' generates Scala case class definitions java -jar /path/to/avrohugger-tools_2.11-0.4.0-assembly.jar generate schema user.avsc .
-
'generate-specific' generates case class definitions that extend SpecificRecordBase java -jar /path/to/avrohugger-tools_2.11-0.4.0-assembly.jar generate-specific schema user.avsc .
####sbt-avrohugger
Also available as an sbt plugin found here that adds a generate
or generate-specific
task to compile
(an alternative to macros).
- Support more avro types: fixed, bytes.
The scripted
task runs all tests.
As per Doug Cutting's recommendations in the avro compiler tests, the string-based tests in test
are augmented by scripted
tests that generate and compile source that is run in de/serialization tests.
Depends on Avro and Treehugger. avrohugger-tools
is based on avro-tools.
Contributors: