我正在尝试通过Twitter的chill-scala库使用Kryo序列化Scala类的实例。它来自一个库(外部jar),因此,我认为需要在Kryo注册。
如何使用chill-scala将类注册到(de)序列化?
这是我的代码的核心,主要基于检查chill-scala测试套件。
// This is from the chill-scala test suite
def serialize[T](t: T): Array[Byte] = ScalaKryoInstantiator.defaultPool.toBytesWithClass(t)
def deserialize[T](bytes: Array[Byte]): T =
ScalaKryoInstantiator.defaultPool.fromBytes(bytes).asInstanceOf[T]
/**
* Save a value in cache.
*/
def save[T](key: String, value: T, expiration: Int = 0): Future[T] = {
cache.put(key, serialize[T](value), expiration, TimeUnit.SECONDS)
Future.successful(value)
}
/**
* Finds a value in the cache.
*/
def find[T: ClassTag](key: String): Future[Option[T]] = Future {
val result = deserialize[T](cache.get(key).asInstanceOf[Array[Byte]])
Option(result)
}
当我运行它时,它会抛出
com.esotericsoftware.kryo.KryoException: Unable to find class: <name_of_external_class>
更一般地说,是否存在关于如何使用chill-scala的文档?该软件包的作者显然做了大量的工作,我看到了一些积极的参考 - 但没有文档。
感谢您的任何指示,
拜伦
答案 0 :(得分:0)
我认为chill只是Kryo的扩展,它为受支持的scala类提供了比Kryo默认FieldSerializer
更好的序列化器。
所以,如果你不知道如何使用寒冷,你应该尝试阅读the docs of Kryo。
而且,
除外Unable to find class: <name_of_external_class>
可能是因为该类不在您的类路径中。它可能与Kryo无关。
请注意,即使没有在Kryo中注册,该课程仍可由Kryo序列化。
如果您需要使用寒冷的好例子,Apache Spark的源代码是一个不错的选择。此外,this repo中的小例子是可以接受的。