Skip to content
This repository has been archived by the owner on Sep 13, 2023. It is now read-only.
/ salat-avro Public archive

Fast bi-directional Scala case class to Avro serialization

License

Notifications You must be signed in to change notification settings

Banno/salat-avro

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

69 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

salat-avro

salat-avro is library to quickly encode and decode Scala case classes to and from serialized Apache Avro encodings using salat. It is very fast.

Usage

Using salat-avro is very similar to how you would use salat. Given a case class and an implicit context, it will give you a grater which can serialize and deserialize.

A major difference is that you must supply your own Encoder and Decoder. These can be had from the Avro EncoderFactory and DecoderFactory

Here’s a quick example:

import com.banno.salat.avro._
import global._

val oldAlice = Alice("abcd", Some(new DateTime()), 123, None, Bob("abc"))

val baos = new ByteArrayOutputStream
val encoder = EncoderFactory.get().binaryEncoder(baos, null)

grater[Alice].serialize(oldAlice, encoder)

val bytes = baos.toByteArray
val decoder = DecoderFactory.get().binaryDecoder(bytes, null)

val sameAlice: Alice = grater[Alice].asObject(decoder)

In the case that you need an Avro schema (say for the JsonEncoder), you can get it via:

val schema: Schema = grater[Alice].asAvroSchema

Types that salat-avro supports

  • Boolean
  • String
  • Int
  • BigDecimal
  • Enumeration values
  • JodaTime date times
  • Option types of any other type salat-avro supports
  • Other case classes

multi-grater

Salat-avro also supports the idea of a multi-grater. This is combining multiple graters into one which can serialize and deserialize any of the contained types. For example

import com.banno.salat.avro._
import global._

val oldAlice = Alice("abcd", Some(new DateTime()), 123, None, Bob("abc"))
val multiGrater = grater[Alice] + grater[Edward]

val baos = new ByteArrayOutputStream
val encoder = EncoderFactory.get().binaryEncoder(baos, null)

multiGrater.serialize(oldAlice, encoder)

val bytes = baos.toByteArray
val decoder = DecoderFactory.get().binaryDecoder(bytes, null)

val sameAlice = multiGrater.asObject(decoder)

In this setup, the type of `sameAlice` is a case class, and can’t be of type `Alice` since `multiGrater.asObject` could return either an `Edward` or an `Alice`.

Why?

With many serialization schemes, you have an external schema that you use to generate your classes in your language du jour. This can be a hassle if you’re only using Scala to serialize and deserialize. It introduces a clunky step using a un-natural interface. It also becomes a problem with evolving schemas. Avro allows us to supply a schema at runtime which it will store along side the serialized instance. Avro will use that to reconcile any differences that may have occured in the schema between when it was serialized and when it is being deserialized.

salat uses the ScalaSig (scalap) library for inspecting case classes without the use of reflection. It is also able to extract values without reflection. This makes it fast.

Installation

salat-avro is not yet published anywhere. Sorry. The git repository must be cloned locally and a sbt publish-local must be given. It currently has a dependency on salat 0.0.7-SNAPSHOT and avro 1.5.

Still To Do

There are some basic types left to do:

  • Array/List/Seq/Set
  • Map
  • etc.

Look at salat-core’s tests for inspiration.

Contributing

Fork away, commit, and send off a pull request to me. Make sure your tests pass before you send the pull request.

License

salat-avro is licensed under the Apache 2 License.

Copyright 2011 T8 Webware

About

Fast bi-directional Scala case class to Avro serialization

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages