Last active
August 29, 2015 14:00
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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