Monday, November 17, 2014

Connect to Zookeeper using Scala

One of the advantages of Scala is the run time shell. Quick experiment: connect to zookeeper using a scala shell.

Download a scala distro. I used 2.11.2. Download Zookeeper, I used Zookeeper-3.5 from github.

Optional: Recompile Zookeeper after modifying ivy.xml to use jline-2.12 to prevent library incompatibilities with the scala shell. Zookeeper-branch-3.5 uses jline 2.11; will work without modifying the jline version. Run >ivy package to create the zookeeper jar and copy it to scala-2.11.2/lib directory.

cd into scala-2.11.2 and run >bin/scala

if there are no error messages your zookeeper jar is correct and does not cause conflicts with the scala command line, start the zookeeper server >./zkServer.sh
and run in the scala command line:

scala> import org.apache.zookeeper._
scala> val zk = new ZooKeeper("2181", 15000, new Watcher{def process(event:WatchedEvent){println(event)}})
log4j:WARN No appenders could be found for logger (org.apache.zookeeper.ZooKeeper).
log4j:WARN Please initialize the log4j system properly.
zk: org.apache.zookeeper.ZooKeeper = State:CONNECTING sessionid:0x0 local:null remoteserver:null lastZxid:0 xid:1 sent:0 recv:0 queuedpkts:0 pendingresp:0 queuedevents:0

scala>

No comments:

Post a Comment