环境准备
安装python3环境
# centos 安装python3
yum install python3
创建激活venv
python3 -m venv .venv
source .venv/bin/activate
zookeeper
pip install kazoo
递归复制目录
from kazoo.client import KazooClient
def copy_node(zk, source_path, destination_path):
# 获取源节点的数据
data, stat = zk.get(source_path)
# 在目标路径创建节点
zk.create(destination_path, data)
# 获取子节点
children = zk.get_children(source_path)
for child in children:
# 递归复制子节点
copy_node(zk, f"{source_path}/{child}", f"{destination_path}/{child}")
zk = KazooClient(hosts='127.0.0.1:2181')
zk.start()
copy_node(zk, '/source_node', '/destination_node')
zk.stop()
设置key的值带有换行符
也可以用zkCli工具,先把值内容写入文件,再执行命令:./zkCli.sh -server 127.0.0.1:2181 set /your/znode/path “cat /tmp/path_value
”
from kazoo.client import KazooClient
# 连接到 Zookeeper
zk = KazooClient(hosts='127.0.0.1:2181')
zk.start()
# 要设置的 key
znode_path = "/your/znode/path"
# 要设置的值,包含换行符
value = """parseTreeCache:
initialCapacity: 128
maximumSize: 1024
sqlStatementCache:
initialCapacity: 2000
maximumSize: 65535"""
# 设置 Zookeeper znode 的值
zk.set(znode_path, value.encode('utf-8'))
# 关闭连接
zk.stop()