Skip to content

Instantly share code, notes, and snippets.

@ggreg
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ggreg/11329634 to your computer and use it in GitHub Desktop.
Save ggreg/11329634 to your computer and use it in GitHub Desktop.
An example of using Scala's pickling library to store an instance of a non-trivial (nested case classes) type in a byte array and load it back. It comes from a simple prototype (yes the byte array is copied to deserialize the data) that stores some data in zookeeper.
import scala.pickling._
import json._
import org.apache.curator.framework.{ CuratorFrameworkFactory }
import org.apache.curator.retry.{ ExponentialBackoffRetry }
import org.apache.zookeeper.CreateMode
abstract class State
case class Scheduled extends State
abstract class Event
case class TaskEvent(state: State) extends Event
val retryPolicy = new ExponentialBackoffRetry(1000, 3)
val client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", retryPolicy);
client.start();
val stored_event = TaskEvent(Scheduled()).pickle.value.getBytes
val seq_path = "/tasks/task-"
val path = client.create().withMode(CreateMode.PERSISTENT_SEQUENTIAL).forPath(seq_path, TaskEvent(Scheduled()).pickle.value.getBytes)
val stored_event = client.getData().forPath(path)
val event = (new String(stored_event)).unpickle[TaskEvent]
// It returns a TaskEvent = TaskEvent(Scheduled())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment