Name: chill
Owner: Twitter, Inc.
Description: Scala extensions for the Kryo serialization library
Created: 2012-10-15 22:01:08.0
Updated: 2018-01-17 07:45:18.0
Pushed: 2018-01-14 15:38:16.0
Homepage: https://twitter.com/scalding
Size: 816
Language: Java
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
Extensions for the Kryo serialization library including serializers and a set of classes to ease configuration of Kryo in systems like Hadoop, Storm, Akka, etc.
t
mpile # to build chill
blishM2 # to publish chill to your local .m2 repo
blish-local # publish to local ivy repo.
Chill has a set of subprojects: chill-java, chill-hadoop, chill-storm and chill-scala. Other than chill-scala, all these projects are written in Java so they are easy to use on any JVM platform.
The chill-java package includes the KryoInstantiator
class (factory for Kryo instances)
and the IKryoRegistrar
interface (adds Serializers to a given Kryo). These two are composable
to build instantiators that create instances of Kryo that have the options and serializers you
need. The benefit of this over a direct Kryo instance is that a Kryo instance is mutable and not
serializable, which limits the safety and reusability of code that works directly with them.
To deserialize or serialize easily, look at KryoPool
:
POOL_SIZE = 10;
Pool kryo = KryoPool.withByteArrayOutputStream(POOL_SIZE, new KryoInstantiator());
[] ser = kryo.toBytesWithClass(myObj);
ct deserObj = kryo.fromBytes(myObj);
The KryoPool is a thread-safe way to share Kryo instances and temporary output buffers.
Hadoop, Storm, and Akka all use a configuration that is basically equivalent to a Map[String,
String]
. The com.twitter.chill.config
package makes it easy to build up KryoInstantiator
instances given a Config instance, which is an abstract class acting as a thin wrapper over
whatever configuration data the system, such as Hadoop, Storm or Akka, might give.
To configure a KryoInstantiator use ConfiguredInstantiator
with either reflection,
which takes a class name and instantiates that KryoInstantiator, or an instance of KryoInstantiator
and serializes that instance to use later:
s TestInst extends KryoInstantiator { override def newKryo = sys.error("blow up") }
new Config:
conf = new JavaMapConfig
et-up class-based reflection of our instantiator:
iguredInstantiator.setReflect(conf, classOf[TestInst])
cci = new ConfiguredInstantiator(conf)
newKryo // uses TestInst
serialize a particular instance into the config to use later (or another node):
iguredInstantiator.setSerialized(conf, new TestInst)
cci2 = new ConfiguredInstantiator(conf)
.newKryo // uses the particular instance we passed above
Scala classes often have a number of properties that distinguish them from usual Java classes. Often
scala classes are immutable, and thus have no zero argument constructor. Secondly, object
in scala is
a singleton that needs to be carefully serialized. Additionally, scala classes often have synthetic
(compiler generated) fields that need to be serialized, and by default Kryo does not serialize
those.
In addition to a ScalaKryoInstantiator
which generates Kryo instances with options suitable for
scala, chill provides a number of Kryo serializers for standard scala classes (see below).
Many existing systems use Java serialization. MeatLocker is an object that wraps a given instance
using Kryo serialization internally, but the MeatLocker itself is Java serializable.
The MeatLocker allows you to box Kryo-serializable objects and deserialize them lazily on the first call to get
:
rt com.twitter.chill.MeatLocker
boxedItem = MeatLocker(someItem)
oxedItem is java.io.Serializable no matter what it contains.
box = roundTripThroughJava(boxedItem)
get == boxedItem.get // true!
To retrieve the boxed item without caching the deserialized value, use meatlockerInstance.copy
.
These are found in the chill-scala
directory in the chill jar (originally this project was
only scala serializers). Chill provides support for singletons, scala Objects and the following types:
$outer
references).Bijections and Injections are useful when considering serialization. If you have an Injection from T
to Array[Byte]
you have a serialization. Additionally, if you have a Bijection between A
and B
, and a serialization for B
, then you have a serialization for A
. See BijectionEnrichedKryo
for easy interop between bijection and chill.
KryoInjection is an injection from Any
to Array[Byte]
. To serialize using it:
rt com.twitter.chill.KryoInjection
bytes: Array[Byte] = KryoInjection(someItem)
tryDecode: scala.util.Try[Any] = KryoInjection.invert(bytes)
KryoInjection can be composed with Bijections and Injections from com.twitter.bijection
.
To use, add a key to your config like:
akka.actor.serializers {
kryo = "com.twitter.chill.akka.AkkaSerializer"
}
Then for the super-classes of all your message types, for instance, scala.Product, write:
kka.actor.serialization-bindings {
"scala.Product" = kryo
If you want to use the chill.config.ConfiguredInstantiator
see ConfiguredAkkaSerializer
otherwise, subclass AkkaSerializer
and override kryoInstantiator
to control how the Kryo
object is created.
To learn more and find links to tutorials and information around the web, check out the Chill Wiki.
The latest ScalaDocs are hosted on Chill's Github Project Page.
Discussion occurs primarily on the Chill mailing list. Issues should be reported on the GitHub issue tracker.
Pull requests and bug reports are always welcome!
We use a lightweight form of project governance inspired by the one used by Apache projects. Please see Contributing and Committership for our code of conduct and our pull request review process. The TL;DR is send us a pull request, iterate on the feedback + discussion, and get a +1 from a Committer in order to get your PR accepted.
The current list of active committers (who can +1 a pull request) can be found here: Committers
A list of contributors to the project can be found here: Contributors
Chill modules are available on Maven Central. The current groupid and version for all modules is, respectively, "com.twitter"
and 0.9.1
and each scala project is published for 2.10
, 2.11
and 2.12
. Search search.maven.org when in doubt.
chill-scala
is not published separately; to use it, reference chill
. To add the dependency to your project using SBT:
"com.twitter" %% "chill" % "0.9.1"
Copyright 2012 Twitter, Inc.
Licensed under the Apache License, Version 2.0.