Good Zookeeper Tutorial with Java client
2017-11-29 20:23
483 查看
参考: https://stackoverflow.com/questions/33524537/good-zookeeper-tutorial-with-java-client
I was trying to use Zookeeper in our project. Could run the server..Even test it using zkcli.sh .. All good.. But couldn't find a good tutorial for me to connect to this server using Java ! All I need in Java API is a method
![](https://images2017.cnblogs.com/blog/575868/201711/575868-20171129203600948-1686373521.png)
-----------------------------------------
Finally, this is the simplest and most basic program I came up with which will help you with ZooKeeper "Getting Started":
pom.xml
编译后,运行的结果:
![](https://images2017.cnblogs.com/blog/575868/201711/575868-20171129201654729-322218108.png)
项目的结构:
![](https://images2017.cnblogs.com/blog/575868/201711/575868-20171129201725558-1821863269.png)
执行 mvn clean 之后:
![](https://images2017.cnblogs.com/blog/575868/201711/575868-20171129201759729-1357105138.png)
执行 mvn assembly:assembly 之后,编译后的结果为:
![](https://images2017.cnblogs.com/blog/575868/201711/575868-20171129202026886-1407013827.png)
两个jar包的内容是不一样的。
java -cp target/new-app-0.0.1-SNAPSHOT-jar-with-dependencies.jar com.fly.app.ZkConnect
![](https://images2017.cnblogs.com/blog/575868/201711/575868-20171129202121886-225013091.png)
或者:java -cp target/new-app-0.0.1-SNAPSHOT.jar:/data/tools/zookeeper-3.4.11/lib/slf4j-api-1.6.1.jar:/data/tools/zookeeper-3.4.11/zookeeper-3.4.11.jar:/data/tools/zookeeper-3.4.11/lib/*.jar com.fly.app.ZkConnect
I was trying to use Zookeeper in our project. Could run the server..Even test it using zkcli.sh .. All good.. But couldn't find a good tutorial for me to connect to this server using Java ! All I need in Java API is a method
![](https://images2017.cnblogs.com/blog/575868/201711/575868-20171129203600948-1686373521.png)
-----------------------------------------
Finally, this is the simplest and most basic program I came up with which will help you with ZooKeeper "Getting Started":
package com.fly.app; import java.util.Date; import java.util.List; import java.util.concurrent.CountDownLatch; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; import org.apache.zookeeper.Watcher.Event.KeeperState; import org.apache.zookeeper.ZooDefs.Ids; import org.apache.zookeeper.ZooKeeper; public class ZkConnect { private ZooKeeper zk; private CountDownLatch connSignal = new CountDownLatch(0); //host should be 127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002 public ZooKeeper connect(String host) throws Exception { zk = new ZooKeeper(host, 3000, new Watcher() { public void process(WatchedEvent event) { if (event.getState() == KeeperState.SyncConnected) { connSignal.countDown(); } } }); connSignal.await(); return zk; } public void close() throws InterruptedException { zk.close(); } public void createNode(String path, byte[] data) throws Exception { zk.create(path, data, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } public void updateNode(String path, byte[] data) throws Exception { zk.setData(path, data, zk.exists(path, true).getVersion()); } public void deleteNode(String path) throws Exception { zk.delete(path, zk.exists(path, true).getVersion()); } public static void main (String args[]) throws Exception { ZkConnect connector = new ZkConnect(); ZooKeeper zk = connector.connect("54.169.132.0,52.74.51.0"); String newNode = "/deepakDate"+new Date(); connector.createNode(newNode, new Date().toString().getBytes()); List<String> zNodes = zk.getChildren("/", true); for (String zNode: zNodes) { System.out.println("ChildrenNode " + zNode); } byte[] data = zk.getData(newNode, true, zk.exists(newNode, true)); System.out.println("GetData before setting"); for ( byte dataPoint : data) { System.out.print ((char)dataPoint); } System.out.println("GetData after setting"); connector.updateNode(newNode, "Modified data".getBytes()); data = zk.getData(newNode, true, zk.exists(newNode, true)); for ( byte dataPoint : data) { System.out.print ((char)dataPoint); } connector.deleteNode(newNode); } }
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.fly.app</groupId> <artifactId>new-app</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>new-app</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.6</maven.compiler.source> <maven.compiler.target>1.6</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.4</version> <configuration> <descriptorRefs> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
编译后,运行的结果:
![](https://images2017.cnblogs.com/blog/575868/201711/575868-20171129201654729-322218108.png)
项目的结构:
![](https://images2017.cnblogs.com/blog/575868/201711/575868-20171129201725558-1821863269.png)
执行 mvn clean 之后:
![](https://images2017.cnblogs.com/blog/575868/201711/575868-20171129201759729-1357105138.png)
执行 mvn assembly:assembly 之后,编译后的结果为:
![](https://images2017.cnblogs.com/blog/575868/201711/575868-20171129202026886-1407013827.png)
两个jar包的内容是不一样的。
java -cp target/new-app-0.0.1-SNAPSHOT-jar-with-dependencies.jar com.fly.app.ZkConnect
![](https://images2017.cnblogs.com/blog/575868/201711/575868-20171129202121886-225013091.png)
或者:java -cp target/new-app-0.0.1-SNAPSHOT.jar:/data/tools/zookeeper-3.4.11/lib/slf4j-api-1.6.1.jar:/data/tools/zookeeper-3.4.11/zookeeper-3.4.11.jar:/data/tools/zookeeper-3.4.11/lib/*.jar com.fly.app.ZkConnect
![](https://images2017.cnblogs.com/blog/575868/201711/575868-20171129202240276-1683704995.png)
相关文章推荐
- Java Exception Handling Tutorial with Examples and Best Practices
- Java Debugging with Eclipse - Tutorial
- Java Apache HTTPClient Tutorial 深度学习(二)
- Error creating bean with name 'memcachedClient'...java.lang.OutOfMemoryError
- RESTful Java Client With RESTEasy Client Framework
- Could not open client transport with JDBC Uri: jdbc:hive2://slaver2:10000: java.net.ConnectException
- Programming with ZooKeeper - A basic tutorial
- Memcached Java Client with sample program--reference
- RESTful Java client with RESTEasy client framework
- Java E-mail Client with GUI
- Apache Curator is a Java/JVM client library for Apache ZooKeeper
- RESTful Java client with Apache HttpClient
- bug--java访问hdfs (Server IPC version 9 cannot communicate with client version 4 错误)
- Zookeeper——JAVA Client
- Java Annotations Tutorial with Custom Annotation
- JAVA + SSL Tutorial (server and client examples)
- java socket client to python server with utf-8
- Java 8 Lambda Expressions Tutorial with Examples
- Implements a Java FTP client with sun.net.*
- 【HBase】org.apache.zookeeper.client.ZooKeeperSaslClient: SecurityException: java.lang.SecurityExcepti