java 如何使用 Curator 检查 Zookeeper 中是否存在路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12537516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I check if a path exists in Zookeeper using Curator?
提问by Cody S
I'm currently in the early stages of development to integrate Zookeeper stuff into my app. I'm using Netflix's Curator as an API for dealing with Zookeeper. I get the gist of most of it (like adding ZNodes and whatnot).
我目前处于开发的早期阶段,将 Zookeeper 的东西集成到我的应用程序中。我使用 Netflix 的 Curator 作为 API 来处理 Zookeeper。我得到了大部分的要点(比如添加 ZNodes 等等)。
But how the heck do you check if a ZNode exists? The code that seems like it would work is:
但是你到底如何检查 ZNode 是否存在呢?看起来可行的代码是:
client.checkExists().forPath(path);
...Where client
is an instance of com.netflix.curator.framework.CuratorFramework
...哪里client
是一个实例com.netflix.curator.framework.CuratorFramework
However, this call returns a org.apache.zookeeper.data.Stat
object.
但是,此调用返回一个org.apache.zookeeper.data.Stat
对象。
Using this object, how can I tell if a path exists or not?
使用此对象,如何判断路径是否存在?
Thanks
谢谢
回答by willglynn
The org.apache.zookeeper.data.Stat
object is metadata about that ZNode. (It's conceptually similar to how stat()
tells you information about a file on the filesystem, hence the name.) checkExists().forPath()
returns a Stat
if it exists, or null
if it doesn't.
该org.apache.zookeeper.data.Stat
对象是有关该 ZNode 的元数据。(它在概念上类似于如何stat()
告诉您有关文件系统上文件的信息,因此得名。)如果存在,或者不存在,则checkExists().forPath()
返回 a 。Stat
null