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

大城市里の小女人 提交于 2019-12-06 23:47:37

问题


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.)


回答1:


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]


来源:https://stackoverflow.com/questions/20850569/handling-case-classes-in-twitter-chill-scala-interface-to-kryo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!