@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()
Wednesday, October 1, 2014
List Zookeeper Nodes and Data with Groovy
Here's a quick Groovy script to recursively list Zookeeper nodes (and optionally, data), also on Gist here. What does this have to do with PDI, you may ask? Stay tuned ;)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment