Skip to content

Scala library for Avro schema generation, encoding and decoding

License

Notifications You must be signed in to change notification settings

bobbyrauchenberg/avronaut

Repository files navigation

Build Status codecov

Avronaut

Scala Avro library

Currently in active development

Schema Generation

Auto-generate Avro schemas from case classes

type CP                 = String :+: Boolean :+: Int :+: CNil

case class CoproductUnion(cupcat: CP)

AvroSchema[CoproductUnion].schema

Will, if successful give a Right containing an Avro schema

Right({
	"type": "record",
	"name": "CoproductUnion",
	"namespace": "unit.schema.CoproductUnionSpec",
	"doc": "",
	"fields": [{
		"name": "cupcat",
		"type": ["int", "boolean", "string"]
	}]
})

Decoder

Decode an Avro GenericRecord to a case class

case class RecordWithMultipleFields(field1: Boolean, field2: String, field3: Int)

case class ReaderStringRecord(field2: String)

AvroSchema[RecordWithMultipleFields].schema.map { writerSchema =>
          val genericRecord = new GenericData.Record(writerSchema)
          genericRecord.put(0, true)
          genericRecord.put(1, "cupcat")
          genericRecord.put(2, 45)
          genericRecord
        }.flatMap(record => Decoder.decode[ReaderStringRecord](record))

Will, if successful, decode the relevent fields of the GenericRecord to your caseclass, using the provided reader schema

Right(ReaderStringRecord("cupcat"))

Encoder

Encode a case class to an Avro GenericRecord

case class RecordWithUnion(field: Option[String])

val record = RecordWithUnion("cupcat".some)

Encoder.encode[RecordWithUnion](record)

Will, if successful, encode the case class to a Right of GenericRecord

Right({"field":"cupcat"})