Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# java -cp zookeeper-3.3.0.jar:log4j-1.2.15.jar:conf org.apache.zookeeper.server.quorum.QuorumPeerMain zoo.cfg
### Basics: creating & deleting nodes
jruby-1.4.0 > require 'zookeeper'
jruby-1.4.0 > @zk = ZooKeeper.new(:host => "localhost:2181", :watcher => SilentWatcher.new)
=> #<ZooKeeper:0x175b28d8>
jruby-1.4.0 > @zk.exists("/test")
=> nil
jruby-1.4.0 > @zk.create(:path => "/test", :data => "test_data")
=> "/test"
jruby-1.4.0 > @zk.exists("/test")
=> #<ZooKeeper::Stat:0x2beb717e @child_list_version=0, @last_modified_zxid=91, @last_modified_time=1272213077611, @version=0, @ephemeral_owner=0, @created_zxid=91, @created_time=1272213077611, @acl_list_version=0>
jruby-1.4.0 > @zk.get("/test")
=> ["test_data", #<ZooKeeper::Stat:0x1d9dbdc4 @child_list_version=0, @last_modified_zxid=91, @last_modified_time=1272213077611, @version=0, @ephemeral_owner=0, @created_zxid=91, @created_time=1272213077611, @acl_list_version=0>]
jruby-1.4.0 > @zk.get("/non_existent_path")
KeeperException::NoNode: KeeperException::NoNode
### Children nodes
jruby-1.4.0 > @zk.create(:path => "/test/node-", :data => "test_data", :sequence => true)
=> "/test/node-0000000047"
jruby-1.4.0 > @zk.create(:path => "/test/node-", :data => "test_data", :ephemeral => true, :sequence => true)
=> "/test/node-0000000048"
jruby-1.4.0 > @zk.children("/test")
=> ["/test/node-0000000047", "/test/node-0000000048"]
jruby-1.4.0 > @zk.close
=> nil
jruby-1.4.0 >@zk = ZooKeeper.new(:host => "localhost:2181", :watcher => SilentWatcher.new)
=> #<ZooKeeper:0x5e7b4ed7>
jruby-1.4.0 > @zkchildren("/test")
=> ["/test/node--0000000047"]
### Callbacks and async operations
jruby-1.4.0 > cb = lambda {|r| puts "wooo :#{r.inspect}"}
=> #<Proc:0x1e4218cb@(irb):44>
jruby-1.4.0 > context = Time.now
=> Sun Apr 25 12:37:21 -0400 2010
jruby-1.4.0 > @zk.create(:path => "/test", :data => "test_data", :callback => cb, :context => context)
=> nil
wooo :[-110, "/test", #<Java::JavaUtil::Date:0x71cd427a>, nil]
### Data versioning
jruby-1.4.0 > @zk2 = ZooKeeper.new(:host => "localhost:2181")
=> #<ZooKeeper:0x476dc5c9>
jruby-1.4.0 > @zk.set(:path => "/test", :data => "test_data_1", :version => 0)
KeeperException::BadVersion: KeeperException::BadVersion
jruby-1.4.0 > @zk.set(:path => "/test", :data => "test_data_1", :version => 5)
=> #<Java::ComYahooZookeeperData::Stat:0x6f69b66e>
jruby-1.4.0 > @zk.exists(:path => "/test", :data => "test_data_1", :version => 5)
=> #<ZooKeeper::Stat:0x29dd8664 @child_list_version=11, @last_modified_zxid=132, @last_modified_time=1272214292662, @version=6, @ephemeral_owner=0, @created_zxid=91, @created_time=1272213077611, @acl_list_version=0>
jruby-1.4.0 > @watcher = EventWatcher.new
=> #<EventWatcher:0x3c3ca56f @received_disconnected=false, @events=[]>
jruby-1.4.0 > @zk1 = ZooKeeper.new(:host => "localhost:2181", :watcher => @watcher)
=> #<ZooKeeper:0x17f920cf>
jruby-1.4.0 > @zk2 = ZooKeeper.new(:host => "localhost:2181", :watcher => SilentWatcher.new)
=> #<ZooKeeper:0x2b9a68ee>
### Event processing & Watchers
jruby-1.4.0 > class EventWatcher
jruby-1.4.0 ?> def process(event)
jruby-1.4.0 ?> puts "Got event: #{event}"
jruby-1.4.0 ?> end
jruby-1.4.0 ?> end
jruby-1.4.0 > @watcher = EventWatcher.new
=> #<EventWatcher:0x575e6691 @received_disconnected=false, @events=[]>
jruby-1.4.0 > @zk1.get(:path => "/test", :watch => true)
=> ["foo", #<ZooKeeper::Stat:0x1fea8dbd @child_list_version=11, @last_modified_zxid=137, @last_modified_time=1272214512545, @version=7, @ephemeral_owner=0, @created_zxid=91, @created_time=1272213077611, @acl_list_version=0>]
jruby-1.4.0 > @zk1 = ZooKeeper.new(:host => "localhost:2181", :watcher => @watcher)
=> #<ZooKeeper:0x683a3e1b>
jruby-1.4.0 > Got event: #<ZooKeeper::WatcherEvent:0x56ab436d>
@zk2 = ZooKeeper.new(:host => "localhost:2181", :watcher => SilentWatcher.new)
=> #<ZooKeeper:0x6c6742d0>
jruby-1.4.0 > @zk1.get(:path => "/test", :watch => true)
=> ["foo", #<ZooKeeper::Stat:0x788ebb5a @child_list_version=11, @last_modified_zxid=139, @last_modified_time=1272214591550, @version=9, @ephemeral_owner=0, @created_zxid=91, @created_time=1272213077611, @acl_list_version=0>]
jruby-1.4.0 > @zk2.set(:path => "/test", :data => "foo")
Got event: #<ZooKeeper::WatcherEvent:0x1393537d>
=> #<Java::ComYahooZookeeperData::Stat:0x2d4e3d95>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment