Skip to content

Instantly share code, notes, and snippets.

@itang
Last active August 29, 2015 14:15
Show Gist options
  • Save itang/6b69a35c35e3b859a312 to your computer and use it in GitHub Desktop.
Save itang/6b69a35c35e3b859a312 to your computer and use it in GitHub Desktop.
walk zookeeper nodes
require 'zookeeper'
class ZKWalk
def initialize(zk = Zookeeper.new('localhost:2181'))
@zk = zk
end
def walk (path, max_depth = -1 )
def _walk(path, max_depth, depth = 0)
if max_depth != -1 && depth > max_depth
return
end
puts %Q(#{" " * 2 * depth}#{path}:#{@zk.get(:path => path)[:data]})
if cs = @zk.get_children(path: path)[:children]
depth += 1
cs.each do |x|
full_path = path + ((path.end_with? "/") ? x : "/" + x)
_walk(full_path, max_depth, depth)
end
depth -= 1
end
end
_walk(path, max_depth, 0)
end
end
if __FILE__ == $0
it = ZKWalk.new
it.walk '/', ARGV[0].to_i || -1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment