我可以在Zookeeper中递归创建路径吗?
我将 ZooKeeper 拉入项目中进行一些并发管理,第一个我尝试的事情对我来说是非常明显的(使用 zkpython 绑定):
zh = zookeeper.init('localhost:2181')
zookeeper.create(zh, '/path/to/a/node', '', [ZOO_OPEN_ACL_UNSAFE])
并且我因为遇到麻烦而返回了 NoNodeException
。
在反思这一点并查看文档(例如它们)之后,我一直无法找到一种方法来执行与 mkdir -p
等效的方法,其中 ZooKeeper 将为我创建丢失的父节点。
我是否遗漏了任何内容,或者无论我喜欢与否,我只是坚持为路径的每个部分发出单独的 create() ?
I'm pulling ZooKeeper into a project for some concurrency management, and the first thing I tried was something that, to me, was quite obvious (using the zkpython binding):
zh = zookeeper.init('localhost:2181')
zookeeper.create(zh, '/path/to/a/node', '', [ZOO_OPEN_ACL_UNSAFE])
And I got back a NoNodeException
for my trouble.
After reflecting on this and reviewing the docs (such as they are), I've been unable to find a way to do the equivalent of a mkdir -p
where ZooKeeper will create the missing parent nodes for me.
Am I missing anything, or am I just stuck issuing separate create()s for each part of a path whether I like it or not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须为路径的每个元素发出单独的 create() 。 Zookeeper 仅内置原子操作。递归创建路径不再是原子操作。如果操作在创建一半路径元素后挂起,Zookeeper 无法知道您想要它做什么。
我不知道 python 中是否已经有 Zookeeper 辅助库。 java (zkClient) 中有一个可以让您通过多次调用 create() 创建递归路径。
You're stuck to issue separate create()s for each element of the path. Zookeeper has only atomic operations build in. Creating a path recursively is not an atomic operation anymore. Zookeeper could not know, what you want it to do, if the operation hangs after creating half of the path elements.
I don't know, if there's already a Zookeeper helper library in python. There is one in java (zkClient) that will let you create recursive paths by calling create() multiple times.
如果您发出单独的 create() ,则可能会在完成一半时被中断或失败。要使调用原子化,您可以使用新的 multi() API。请参阅此答案。
如果路径或其一部分可能已经存在,那么在发出下一个之前等待每个 create() 完成将会不必要地缓慢。在这种情况下,您可以使用异步 API 来加速该过程。请参阅此答案。
如果您只是想避免额外的调用,您可以使用 Netflix 的 curator 库,该库具有 < code>creatingParentsIfNeeded 方法,但请注意它可能会很慢。请参阅此答案。
If you issue separate create()s, you might get interrupted or fail when you're halfway through. To make the call atomic, you can use the new multi() API. See this answer.
If the path or a part of it might already exist, then waiting for each create() to finish before issuing the next would be unnecessarily slow. In this case, you can use the asynchronous API to speed up the process. See this answer.
If you just want to avoid the extra calls, you can use Netflix's curator library which has a
creatingParentsIfNeeded
method, but be advised that it might be slow. See this answer.Kazoo 有一个
ensure_path(path)< /code> 操作
,尽管它不被视为原子操作。使用它至少可以节省您为递归创建编写自己的代码的需要。
Kazoo has an
ensure_path(path)
operation, although it isn't considered atomic. Using this would at least save you the need to write your own code for a recursive create.