我可以在Zookeeper中递归创建路径吗?

发布于 2024-09-11 16:57:47 字数 505 浏览 12 评论 0原文

我将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

等风也等你 2024-09-18 16:57:47

您必须为路径的每个元素发出单独的 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.

半仙 2024-09-18 16:57:47

如果您发出单独的 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.

夏末的微笑 2024-09-18 16:57:47

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文