Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 116 lines (82 sloc) 4.45 kB
d02f693 Automatically handle session expiry, connection watcher API, updated …
John Corwin authored
1 # scala-zookeeper-client
2
3 ## About
4
36a91ea fix readme
John Corwin authored
5 A Scala client for [Apache ZooKeeper](http://hadoop.apache.org/zookeeper/), "a centralized service for maintaining configuration information, naming, providing distributed synchronization, and providing group services".
d02f693 Automatically handle session expiry, connection watcher API, updated …
John Corwin authored
6
7 ZooKeeper provides a Java client library that's perfectly usable from Scala. This just wraps some idioms and niceties around that library to make it as Scala-friendly as possible. It also ships with tests, so you can have some confidence that you'll be able to interact with ZooKeeper from Scala in a predictable and reliable way.
8
9 The scala-zookeeper-client will automatically handle session expired events by creating a new connection to the Zookeeper servers. We also provide several commonly used operations and utilities on top of Zookeeper's minimal API:
10
11 * Create a path of nodes, similar to 'mkdir -p'
12 * Recursively delete a tree of nodes
13 * Watch a node forever
14 * Monitor a node's child set
15 * For a given node, maintain a map from the node's children to the each child's data
16
17 ## Usage
18
19 ### Basic usage:
20
21 import com.twitter.zookeeper.ZooKeeperClient
22
23 val zk = new ZooKeeperClient("localhost:2181")
24
25 zk.createPath("/a/b/c")
26 zk.createPath("/a/b/d")
27 val children = zk.getChildren("/a/b") // Seq("c", "d")
28
29 zk.set("/a/b/c", "foo".getBytes())
30 val s = new String(zk.get("/a/b/c")) // "foo"
31
32 zk.deleteRecursive("/a") // delete "a" and all children
33
34 ### Advanced features:
35
36 Monitor a node forever:
37
38 import com.twitter.zookeeper.ZooKeeperClient
39 import org.apache.zookeeper.CreateMode
40
41 val zk = new ZooKeeperClient("localhost:2181")
42 zk.create("/test-node", "foo".getBytes, CreateMode.PERSISTENT)
43 zk.watchNode("/test-node", { (data : Option[Array[Byte]]) =>
44 data match {
45 case Some(d) => println("Data updated: %s".format(new String(d)))
46 case None => println("Node deleted")
47 }
48 })
49 zk.set("/test-node", "bar".getBytes)
50 zk.set("/test-node", "baz".getBytes)
51 zk.delete("/test-node")
52
53 Monitor a node's children:
54
55 import com.twitter.zookeeper.ZooKeeperClient
56 import org.apache.zookeeper.CreateMode
57
58 val zk = new ZooKeeperClient("localhost:2181")
59 zk.create("/parent", null, CreateMode.PERSISTENT)
60 zk.watchChildren("/parent", { (children : Seq[String]) =>
61 println("Children: %s".format(children.mkString(", ")))
62 })
63 zk.create("/parent/child1", null, CreateMode.PERSISTENT)
64 zk.create("/parent/child2", null, CreateMode.PERSISTENT)
65 zk.delete("/parent/child1")
66 zk.create("/parent/child3", null, CreateMode.PERSISTENT)
67 zk.deleteRecursive("/parent")
68
69 For a given node, automatically maintain a map from the node's children to the each child's data:
70
71 import com.twitter.zookeeper.ZooKeeperClient
72 import org.apache.zookeeper.CreateMode
73 import scala.collection.mutable
74
75 val zk = new ZooKeeperClient("localhost:2181")
76 val childMap = mutable.Map[String, String]()
77
78 zk.create("/parent", null, CreateMode.PERSISTENT)
79 zk.watchChildrenWithData("/parent", childMap, {data => new String(data)})
80
81 zk.create("/parent/a", "foo".getBytes, CreateMode.PERSISTENT)
82 zk.create("/parent/b", "bar".getBytes, CreateMode.PERSISTENT)
83 println("child map: %s".format(childMap)) // NOTE: real code should synchronize access on childMap
84
85 zk.delete("/parent/a")
86 zk.set("/parent/b", "bar2".getBytes)
87 zk.create("/parent/c", "baz".getBytes, CreateMode.PERSISTENT)
88 println("child map: %s".format(childMap)) // NOTE: real code should synchronize access on childMap
89
90 Get the internal zookeeper handle to pass to other libraries:
91
92 import com.twitter.zookeeper.ZooKeeperClient
93 import org.apache.zookeeper.ZooKeeper
94
95 def zkListener(zkHandle : ZooKeeper) {
96 // use zkHandle
97 println("Internal zookeeper handle changed: %s".format(zkHandle))
98 // Note that zkListener will be called with a new handle whenever the Zookeeper session
99 // has expired and a new connection to the Zookeeper servers is created
100 }
101
102 val zk = new ZooKeeperClient("localhost:2181", zkListener _)
103
104 ## Authors
105
36a91ea fix readme
John Corwin authored
106 * [John Corwin](http://github.com/jcorwin)
107 * [Steve Jenson](http://github.com/stevej)
108 * [Matt Knox](http://github.com/mattknox)
109 * [Ian Ownbey](http://github.com/imownbey)
110 * [Ryan King](http://github.com/ryanking)
111 * [Alex Payne](http://github.com/al3x)
d02f693 Automatically handle session expiry, connection watcher API, updated …
John Corwin authored
112
113 ## License
114
115 Apache License, Version 2.0. See the LICENSE file for more information.
Something went wrong with that request. Please try again.