Last active
August 29, 2015 14:15
-
-
Save itang/6b69a35c35e3b859a312 to your computer and use it in GitHub Desktop.
walk zookeeper nodes
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
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