Last active
February 4, 2023 06:24
-
-
Save mattyb149/66e204aca2b6d0005cc2 to your computer and use it in GitHub Desktop.
Recursively list Zookeeper nodes and data
This file contains hidden or 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
@Grab('org.apache.zookeeper:zookeeper:3.4.6') | |
import org.apache.zookeeper.* | |
import org.apache.zookeeper.data.* | |
import org.apache.zookeeper.AsyncCallback.StatCallback | |
import static org.apache.zookeeper.ZooKeeper.States.* | |
final int TIMEOUT_MSEC = 5000 | |
final int RETRY_MSEC = 100 | |
def num_retries = 0 | |
def print_data = args?.length > 1 ? Boolean.valueOf(args[1]) : false | |
def path = args?.length > 0 ? [args[0]] : ['/'] | |
noOpWatcher = { event -> } as Watcher | |
listKids = { parentList, level -> | |
if(parentList != null) { | |
parentList.each { parent -> | |
parentPath = parent?.startsWith('/') ? parent : ('/'+parent) | |
level.times() {print ' '} | |
println parentPath | |
dataStat = new Stat() | |
try { | |
bytes = zk.getData(parentPath, true, dataStat) | |
if(dataStat?.dataLength > 0 && bytes && print_data) { | |
level.times() {print ' '} | |
println new String(bytes) | |
} | |
} | |
catch(e) {} | |
try { | |
kids = zk.getChildren(parentPath, true) | |
if(kids && kids.size() > 0) { | |
listKids(kids.collect{parentPath+(parentPath.endsWith('/') ? '' : '/') +it}, level+1) | |
} | |
} | |
catch(e) {} | |
} | |
} | |
} | |
zk = new ZooKeeper('localhost:2181', TIMEOUT_MSEC , noOpWatcher) | |
while( zk.state != CONNECTED && num_retries < (TIMEOUT_MSEC / RETRY_MSEC) ) { | |
Thread.sleep(RETRY_MSEC) | |
num_retries++ | |
} | |
if(zk.state != CONNECTED) { | |
println "No can do bro, after $TIMEOUT_MSEC ms the status is ${zk.state}" | |
//System.exit(1) | |
} | |
else { | |
listKids(path, 0) | |
} | |
zk.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment