Last active
August 31, 2016 22:42
-
-
Save elianka/d3f2f38319c6ca59596d07ba75d40ada to your computer and use it in GitHub Desktop.
zookeeper copy dedicated path
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
#!/usr/bin/env python | |
#this code dependcy on kazoo. https://kazoo.readthedocs.io/en/latest/index.html | |
#pip install kazoo | |
import sys,os | |
from kazoo.client import KazooClient | |
def copy2remote(path, srczk, dstpath, dstzk): | |
print "create node in remote ", dstpath | |
dstzk.ensure_path(dstpath) | |
dstzk.set(dstpath, srczk.get(path)[0]) | |
def traversing_copy(path, zk, dstpath, dstzk): | |
#split the last '/' | |
if path[len(path)-1] == '/': | |
path = path[:-1] | |
if dstpath[len(dstpath)-1] == '/': | |
dstpath = dstpath[:-1] | |
#get children | |
children = zk.get_children(path) | |
if len(children) == 0: | |
#leaf node | |
# create node in remote and set value | |
#print "create node in remote ", zk.get(path)[0], " \n" | |
copy2remote(path, zk, dstpath, dstzk) | |
return | |
#sub tree node | |
for child in children: | |
node = path+'/'+child | |
dstnode = dstpath+'/'+child | |
traversing_copy(node, zk, dstnode, dstzk) | |
def main(): | |
zk = KazooClient(hosts='10.9.75.186:2181', read_only=True) | |
zk.start() | |
dstzk = KazooClient(hosts='10.9.66.99:2181') | |
dstzk.start() | |
traversing_copy("/marathon2", zk, "/marathon10/", dstzk) | |
'''path = "/marathon2/state/" | |
children = zk.get_children(path) | |
#print children | |
for child in children: | |
node = path+child | |
#print zk.get(node) | |
subchild = zk.get_children(node) | |
print "%s child %d"%(node,len(subchild)) | |
''' | |
zk.stop() | |
dstzk.stop() | |
if __name__ == "__main__": | |
main() | |
raw_input() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment