简体   繁体   English

在Twitter chill中处理案例类(Scala与Kryo的接口)?

[英]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. Twitter-chill看起来似乎是解决如何在Scala中有效序列化而又无需过多重复的问题的好解决方案。

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 (eg 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. 我对WireFormat内置的WireFormat序列化机制有一些经验,该机制是类似于Scalding的Scala Hadoop包装器。 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. 它们具有用于多达22个参数的案例类的序列化程序,这些参数使用apply和unapply方法,并且对这些函数的参数进行类型匹配以检索类型。 (This might not be necessary in Kryo/chill.) (在Kryo / chill中可能没有必要。)

They generally just work (as long as the component members are also serializable by Kryo): 它们通常可以正常工作(只要组件成员也可以由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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM