{"id":1237,"date":"2022-02-23T22:59:20","date_gmt":"2022-02-23T14:59:20","guid":{"rendered":"http:\/\/www.eait.co\/?p=1237"},"modified":"2022-02-23T22:59:50","modified_gmt":"2022-02-23T14:59:50","slug":"zookeeper-api","status":"publish","type":"post","link":"https:\/\/notes.coremix.net\/?p=1237","title":{"rendered":"Zookeeper API"},"content":{"rendered":"<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport org.apache.zookeeper.*;\r\nimport org.apache.zookeeper.data.Stat;\r\nimport org.junit.Before;\r\nimport org.junit.Test;\r\n\r\nimport java.io.IOException;\r\nimport java.util.List;\r\n\r\npublic class ZK {\r\n    private static String connectString = &quot;bigdata166:2181,bigdata167:2181,bigdata168:2181&quot;;\r\n    private static int sessionTimeout = 2000;\r\n    private ZooKeeper zkClient = null;\r\n\r\n    \/**\r\n     * \u76d1\u542c\u65b9\u6cd5\uff0c\u9996\u5148\u8fd0\u884c\r\n     *\r\n     * @throws IOException\r\n     *\/\r\n    @Before\r\n    public void init() throws IOException {\r\n        zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {\r\n            @Override\r\n            public void process(WatchedEvent watchedEvent) {\r\n                \/\/\u83b7\u53d6\u4e1a\u52a1\r\n                System.out.println(watchedEvent.getType() + &quot;===&quot; + watchedEvent.getPath());\r\n                try {\r\n                    zkClient.getChildren(&quot;\/&quot;, true);\r\n                } catch (KeeperException e) {\r\n                    e.printStackTrace();\r\n                } catch (InterruptedException e) {\r\n                    e.printStackTrace();\r\n                }\r\n\r\n            }\r\n        });\r\n    }\r\n\r\n    \/**\r\n     * \u53c2\u65704\uff1a\u4e34\u65f6\uff0c\u6c38\u4e45\uff0c\u5e8f\u5217\u5316 \u6c38\u4e45+\u5e8f\u5217\u5316\uff0c\u4e34\u65f6\u2014\u2014+\u5e8f\u5217\u5316\r\n     *\r\n     * @throws KeeperException\r\n     * @throws InterruptedException\r\n     *\/\r\n    @Test\r\n    public void create() throws KeeperException, InterruptedException {\r\n        zkClient.create(&quot;\/bigdata16&quot;,\r\n                &quot;TestShall&quot;.getBytes(),\r\n                ZooDefs.Ids.OPEN_ACL_UNSAFE,\r\n                CreateMode.PERSISTENT);\/\/\u5168\u90e8\u5f00\u653e\u6743\u9650,\u521b\u5efa\u6c38\u4e45\u6743\u9650\r\n    }\r\n\r\n    \/**\r\n     * \u5224\u65ad\u8282\u70b9\u662f\u5426\u5b58\u5728\r\n     * @throws KeeperException\r\n     * @throws InterruptedException\r\n     *\/\r\n    @Test\r\n    public void exist() throws KeeperException, InterruptedException {\r\n        Stat exists = zkClient.exists(&quot;\/bigdata16&quot;, false);\r\n        System.out.println(exists==null? &quot;\u65e0\u6b64\u8282\u70b9&quot;:&quot;\u6709\u6b64\u8282\u70b9&quot;);\r\n    }\r\n\r\n    \/**\r\n     * \u83b7\u53d6\u5b50\u8282\u70b9\uff08\u5751\uff1a\u5f53Sting\u4e2d\u67d0\u53f0\u673a\u5668\u5173\u6389Server\u540e\uff0c\u4f1a\u62a5\u9519\uff09\r\n     * @throws KeeperException\r\n     * @throws InterruptedException\r\n     *\/\r\n    @Test\r\n    public void getChildren() throws KeeperException, InterruptedException {\r\n        List&lt;String&gt; children = zkClient.getChildren(&quot;\/&quot;, true);\r\n        for (String child : children) {\r\n            System.out.println(child);\r\n        }\r\n\r\n        Thread.sleep(Long.MAX_VALUE);\r\n        \r\n    }\r\n}\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>import org.apache.zookeeper.*; import org.apache.zookee [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22,30],"tags":[],"class_list":["post-1237","post","type-post","status-publish","format-standard","hentry","category-java","category-bigdata"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/posts\/1237","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1237"}],"version-history":[{"count":2,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/posts\/1237\/revisions"}],"predecessor-version":[{"id":1247,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/posts\/1237\/revisions\/1247"}],"wp:attachment":[{"href":"https:\/\/notes.coremix.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1237"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1237"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1237"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}