在 twitter chill(Kryo 的 Scala 接口)中处理案例类? [英] Handling case classes in twitter chill (Scala interface to Kryo)?

查看:63
本文介绍了在 twitter chill(Kryo 的 Scala 接口)中处理案例类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Twitter-chill 看起来是一个很好的解决方案,可以解决如何在 Scala 中有效地序列化而不需要过多的样板.

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

我对内置在 Scoobi 中的 WireFormat 序列化机制有一些经验,这是一个类似于 Scalding 的 Scala Hadoop 包装器.它们具有最多 22 个参数的 case 类的序列化程序,这些参数使用 apply 和 unapply 方法,并对这些函数的参数进行类型匹配以检索类型.(这在 Kryo/chill 中可能没有必要.)

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

推荐答案

它们一般都能正常工作(只要组件成员也可以被 Kryo 序列化):

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]

这篇关于在 twitter chill(Kryo 的 Scala 接口)中处理案例类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆