Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling case classes in twitter chill (Scala interface to Kryo)?

Twitter-chill looks like a good solution to the problem of how to serialize efficiently in Scala without excessive boilerplate.

However, I don't see any evidence of how they handle case classes. Does this just work automatically or does something need to be done (e.g. creating a zero-arg constructor)?

I have some experience with the WireFormat serialization mechanism built into Scoobi, which is a Scala Hadoop wrapper similar to Scalding. They have serializers for case classes up to 22 arguments that use the apply and unapply methods and do type matching on the arguments to these functions to retrieve the types. (This might not be necessary in Kryo/chill.)

like image 534
Urban Vagabond Avatar asked Dec 31 '13 01:12

Urban Vagabond


1 Answers

They generally just work (as long as the component members are also serializable by Kryo):

case class Foo(id: Int, name: String)

// setup
val instantiator = new ScalaKryoInstantiator
instantiator.setRegistrationRequired(false)
val kryo = instantiator.newKryo()

// write
val data = Foo(1,"bob")
val buffer = new Array[Byte](4096]
val output = new Output(buffer)
kryo.writeObject(output, data)

// read
val input = new Input(buffer)
val data2 = kryo.readObject(input,classOf[Foo]).asInstanceOf[Foo]
like image 168
Arne Claassen Avatar answered Oct 17 '22 12:10

Arne Claassen