【黑马】<史上最全面的Hadoop入门教程> day2 zookeeper 代码
pom.iml
需要添加的代码
<!--<repositories> <repostitory> <id>cloudera</id> <url>https://repository.cloudera.com/artifactory/cloudera-repos/ </url> </repostitory> </repositories> --> <dependencies> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>2.12.0</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>2.12.0</version> </dependency> <dependency> <groupId>com.google.collections</groupId> <artifactId>google-collections</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>RELEASE</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.7.25</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.1</version> <scope>compile</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.1</version> <scope>compile</scope> </dependency> </dependencies> <build> <plugins> <!--java编译插件--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build>
操作节点代码:
package cn.hbaf.zookeeper_api; import org.apache.curator.RetryPolicy; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.framework.recipes.cache.ChildData; import org.apache.curator.framework.recipes.cache.TreeCache; import org.apache.curator.framework.recipes.cache.TreeCacheEvent; import org.apache.curator.framework.recipes.cache.TreeCacheListener; import org.apache.curator.retry.ExponentialBackoffRetry; import org.apache.zookeeper.CreateMode; import org.junit.Test; public class ZookeeperAPITest { /* 节点的watch机制 */ @Test public void watchZnode() throws Exception { //1: 定制重试策略 RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000,1); String conectionStr = "192.168.52.100:2181,192.168.52.110:2181,192.168.52.120:2181"; CuratorFramework client = CuratorFrameworkFactory.newClient(conectionStr,8000,8000,retryPolicy); client.start(); //4:创建TreeCache对象,指定要监控的节点路径 TreeCache treeCache = new TreeCache(client,"/hello2"); // 5:自定义一个监听器 treeCache.getListenable().addListener(new TreeCacheListener() { @Override public void childEvent( CuratorFramework curatorFramework, TreeCacheEvent treeCacheEvent) throws Exception { ChildData data = treeCacheEvent.getData(); if (data != null) { switch (treeCacheEvent.getType()) { case NODE_ADDED: System.out.println("监控到有新增节点"); break; case NODE_REMOVED: System.out.println("监控到有节点被移除"); break; case NODE_UPDATED: System.out.println("有节点更新"); break; default: break; } } } }); //开始监听 treeCache.start(); Thread.sleep(200000); } /* 获取节点数据 */ @Test public void getZnodeData() throws Exception { //定制重试策略 RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000,1); String conectionStr = "192.168.52.100:2181,192.168.52.110:2181,192.168.52.120:2181"; CuratorFramework client = CuratorFrameworkFactory.newClient(conectionStr,8000,8000,retryPolicy); client.start(); byte [] bytes = client.getData().forPath("/hello"); System.out.println(new String(bytes)); client.close(); } /* 设置节点数据 */ @Test public void setZnodeData() throws Exception { //定制重试策略 RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000,1); String conectionStr = "192.168.52.100:2181,192.168.52.110:2181,192.168.52.120:2181"; CuratorFramework client = CuratorFrameworkFactory.newClient(conectionStr,8000,8000,retryPolicy); client.start(); client.setData().forPath("/hello","zookeeper".getBytes()); client.close(); } /* 创建临时节点 */ @Test public void createTmpZnode() throws Exception { //1:定制重试策略 /* param1:重试时间间隔 param2:重试最大次数 * */ RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000,1); //2:获取一个客户端对象 /* * param1:服务器列表 * param2:会话超时时间 * param3:链接超时时间 * param4:重试策略 * */ String connectionStr = "192.168.52.100:2181,192.168.52.110:2181,192.168.52.120:2181"; CuratorFramework client = CuratorFrameworkFactory.newClient(connectionStr,8000,8000,retryPolicy); //3:开启客户端 client.start(); //4:创建节点 client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath("/hello3","world".getBytes()); Thread.sleep(5000); //5:关闭客户端 client.close(); } /* 创建永久节点 */ @Test public void createZnode() throws Exception { //1:定制重试策略 /* param1:重试时间间隔 param2:重试最大次数 * */ RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000,1); //2:获取一个客户端对象 /* * param1:服务器列表 * param2:会话超时时间 * param3:链接超时时间 * param4:重试策略 * */ String connectionStr = "192.168.52.100:2181,192.168.52.110:2181,192.168.52.120:2181"; CuratorFramework client = CuratorFrameworkFactory.newClient(connectionStr,8000,8000,retryPolicy); //3:开启客户端 client.start(); //4:创建节点 client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/hello2","world".getBytes()); //5:关闭客户端 client.close(); } }