1
1
## Chill [ ![ Build Status] ( https://secure.travis-ci.org/twitter/chill.png )] ( http://travis-ci.org/twitter/chill )
2
2
3
- Scala extensions for the [ Kryo serialization library] ( http://code.google.com/p/kryo/ ) .
3
+ Extensions for the [ Kryo serialization library] ( http://code.google.com/p/kryo/ ) including
4
+ serializers and a set of classes to ease configuration of Kryo in systems like Hadoop, Storm,
5
+ Akka, etc.
4
6
5
- Chill provides a a number of Kryo serializers and an Option-like type called the MeatLocker. The MeatLocker allows you to box Kryo-serializable objects and deserialize them lazily on the first call to ` get ` :
7
+ Chill has a set of subprojects: chill-java, chill-hadoop, chill-storm and chill-scala. Other than
8
+ chill-scala, all these projects are written in Java so they are easy to use on any JVM platform.
9
+
10
+ ## Chill-Java
11
+
12
+ The chill-java package includes the ` KryoInstantiator ` class (factory for Kryo instances)
13
+ and the ` IKryoRegistrar ` interface (adds Serializers to a given Kryo). These two are composable
14
+ to build instantiators that create instances of Kryo that have the options and serializers you
15
+ need. The benefit of this over a direct Kryo instance is that a Kryo instance is mutable and not
16
+ serializable, which limits the safety and reusability of code that works directly with them.
17
+
18
+ To deserialize or serialize easily, look at ` KryoPool ` :
19
+
20
+ ``` java
21
+ int POOL_SIZE = 10 ;
22
+ KryoPool kryo = KryoPool . withByteArrayOutputStream(POOL_SIZE , new KryoInstantiator ());
23
+ byte [] ser = kryo. toBytesWithClass(myObj);
24
+ Object deserObj = kryo. fromBytes(myObj);
25
+ ```
26
+
27
+ The KryoPool is a thread-safe way to share Kryo instances and temporary output buffers.
28
+
29
+ ### Chill Config
30
+
31
+ Hadoop, Storm, and Akka all use a configuration that is basically equivalent to a `Map[ String,
32
+ String] ` . The ` com.twitter.chill.config` package makes it easy to build up ` KryoInstantiator`
33
+ instances given a Config instance, which is an abstract class acting as a thin wrapper over
34
+ whatever configuration data the system, such as Hadoop, Storm or Akka, might give.
35
+
36
+ To configure a KryoInstantiator use ` ConfiguredInstantiator ` with either reflection,
37
+ which takes a class name and instantiates that KryoInstantiator, or an instance of KryoInstantiator
38
+ and serializes that instance to use later:
39
+ ``` scala
40
+ class TestInst extends KryoInstantiator { override def newKryo = sys.error(" blow up" ) }
41
+
42
+ // A new Config:
43
+ val conf = new JavaMapConfig
44
+ // Set-up class-based reflection of our instantiator:
45
+ ConfiguredInstantiator .setReflect(conf, classOf [TestInst ])
46
+ val cci = new ConfiguredInstantiator (conf)
47
+ cci.newKryo // uses TestInst
48
+ // Or serialize a particular instance into the config to use later (or another node):
49
+
50
+ ConfiguredInstantiator .setSerialized(conf, new TestInst )
51
+ val cci2 = new ConfiguredInstantiator (conf)
52
+ cci2.newKryo // uses the particular instance we passed above
53
+ ```
54
+
55
+ ## Chill in Scala
56
+
57
+ Scala classes often have a number of properties that distinguish them from usual Java classes. Often
58
+ scala classes are immutable, and thus have no zero argument constructor. Secondly, ` object ` in scala is
59
+ a singleton that needs to be carefully serialized. Additionally, scala classes often have synthetic
60
+ (compiler generated) fields that need to be serialized, and by default Kryo does not serialize
61
+ those.
62
+
63
+ In addition to a ` ScalaKryoInstantiator ` which generates Kryo instances with options suitable for
64
+ scala, chill provides a number of Kryo serializers for standard scala classes (see below).
65
+
66
+ ### The MeatLocker
67
+
68
+ Many existing systems use Java serialization. MeatLocker is an object that wraps a given instance
69
+ using Kryo serialization internally, but the MeatLocker itself is Java serializable.
70
+ The MeatLocker allows you to box Kryo-serializable objects and deserialize them lazily on the first call to ` get ` :
6
71
7
72
``` scala
8
73
import com .twitter .chill .MeatLocker
@@ -16,24 +81,17 @@ box.get == boxedItem.get // true!
16
81
17
82
To retrieve the boxed item without caching the deserialized value, use ` meatlockerInstance.copy ` .
18
83
19
- To serialize to bytes and deserialize from bytes:
20
-
21
- ``` scala
22
- import com .twitter .chill .KryoInjection
23
-
24
- val bytes : Array [Byte ] = KryoInjection (someItem)
25
- val option : Option [AnyRef ] = KryoInjection .invert(bytes) // None is returned on failure
26
- ```
27
-
28
- ### Handled classes
84
+ ### Serializers for Scala classes
29
85
30
- Chill provides support for singletons, scala Objects and the following types:
86
+ These are found in the ` chill-scala ` directory in the chill jar (originally this project was
87
+ only scala serializers). Chill provides support for singletons, scala Objects and the following types:
31
88
32
89
* Scala primitives
33
90
* scala.Enumeration values
34
91
* scala.Symbol
35
92
* scala.reflect.Manifest
36
93
* scala.reflect.ClassManifest
94
+ * scala.Function[ 0-22] closure cleaning (removing unused ` $outer ` references).
37
95
* Collections and sequences
38
96
* scala.collection.immutable.Map
39
97
* scala.collection.immutable.List
@@ -42,9 +100,41 @@ Chill provides support for singletons, scala Objects and the following types:
42
100
* scala.collection.mutable.{Map, Set, Buffer, WrappedArray}
43
101
* all 22 scala tuples
44
102
103
+ ## Chill-bijection
104
+ [ Bijections and Injections] ( https://github.com/twitter/bijection )
105
+ are useful when considering serialization. If you have an Injection from
106
+ ` T ` to ` Array[Byte] ` you have a serialization. Additionally, if you have a Bijection between ` A `
107
+ and ` B ` , and a serialization for ` B ` , then you have a serialization for ` A ` . See
108
+ ` BijectionEnrichedKryo ` for easy interop between bijection and chill.
109
+
110
+ ### KryoInjection: easy serialization to byte Arrays
111
+
112
+ KryoInjection is an injection from ` Any ` to ` Array[Byte] ` . To serialize using it:
113
+
114
+ ``` scala
115
+ import com .twitter .chill .KryoInjection
116
+
117
+ val bytes : Array [Byte ] = KryoInjection (someItem)
118
+ val tryDecode : scala.util.Try [Any ] = KryoInjection .invert(bytes)
119
+ ```
120
+
121
+ KryoInjection can be composed with Bijections and Injections from ` com.twitter.bijection ` .
122
+
45
123
## Maven
46
124
47
- Current version is ` 0.2.3 ` . groupid=` "com.twitter" ` artifact=` "chill_2.9.2" ` or artifact=` "chill_2.10" ` .
125
+ Chill modules are available on Maven Central. The current groupid and version for all modules is, respectively, ` "com.twitter" ` and ` 0.3.0 ` .
126
+
127
+ Current published artifacts are
128
+
129
+ * ` chill-java `
130
+ * ` chill-storm `
131
+ * ` chill-hadoop `
132
+ * ` chill_2.9.3 `
133
+ * ` chill_2.10 `
134
+ * ` chill-bijection_2.9.3 `
135
+ * ` chill-bijection_2.10 `
136
+
137
+ The suffix denotes the scala version.
48
138
49
139
## Authors
50
140
0 commit comments