Zookeeper学習(五):ZKclientの使用

5559 ワード

1.セッションの作成
public class createSession {

	public static void main(String[] args) {
		//zk     
		String ZKServers = "192.168.30.164:2181,192.168.30.165:2181,192.168.30.166:2181";
	
		/**
		 *     
		 * new SerializableSerializer()         ,          
		 */
		ZkClient zkClient = new ZkClient(ZKServers,10000,10000,new SerializableSerializer());
		
		System.out.println("conneted ok!");
		
	}
}

2.ノードの作成
public class createNode {

	public static void main(String[] args) {
		//zk     
		String ZKServers = "192.168.30.164:2181,192.168.30.165:2181,192.168.30.166:2181";
		ZkClient zkClient = new ZkClient(ZKServers,10000,10000,new SerializableSerializer());
		
		System.out.println("conneted ok!");
		
		User user = new User();
		user.setId(1);
		user.setName("testUser");
		
		/**
		 * "/testUserNode" :     
		 * user:     
		 * CreateMode.PERSISTENT:       
		 */
		String path = zkClient.create("/testUserNode", user, CreateMode.PERSISTENT);
		//         
		System.out.println("created path:"+path);
	}
}
//  :            implements Serializable
public class User implements Serializable{
	
	private Integer id;
	private String name;
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

3.ノードのデータを取得する
public class getData {
	public static void main(String[] args) {
		        //zk     
				String ZKServers = "192.168.30.164:2181,192.168.30.165:2181,192.168.30.166:2181";
				ZkClient zkClient = new ZkClient(ZKServers,10000,10000,new SerializableSerializer());
				System.out.println("conneted ok!");
				
				Stat stat = new Stat();
				//         
				User  user = zkClient.readData("/testUserNode",stat);
				System.out.println(user.getName());
				System.out.println(stat);
	}
}

4.ノードが存在するか否かを判定する
public class getData {
	public static void main(String[] args) {
		        //zk     
				String ZKServers = "192.168.30.164:2181,192.168.30.165:2181,192.168.30.166:2181";
				ZkClient zkClient = new ZkClient(ZKServers,10000,10000,new SerializableSerializer());
				System.out.println("conneted ok!");
				
				boolean e = zkClient.exists("/testUserNode");
				//   true       ,false     
				System.out.println(e);
	}
}

5.ノードの削除
public class getData {
	public static void main(String[] args) {
		        //zk     
				String ZKServers = "192.168.30.164:2181,192.168.30.165:2181,192.168.30.166:2181";
				ZkClient zkClient = new ZkClient(ZKServers,10000,10000,new SerializableSerializer());
				System.out.println("conneted ok!");
				
				//        ,  true    
				boolean e1 = zkClient.delete("/testUserNode");
				//          
				boolean e2 = zkClient.deleteRecursive("/test");
				
				//   true       ,false      
				System.out.println(e1);
	}
}

6.データの更新
public static void main(String[] args) {
		        //zk     
				String ZKServers = "192.168.30.164:2181,192.168.30.165:2181,192.168.30.166:2181";
				ZkClient zkClient = new ZkClient(ZKServers,10000,10000,new SerializableSerializer());
				System.out.println("conneted ok!");
				
				User user = new User();
				user.setId(2);
				user.setName("testUser2");
				/**
				 * testUserNode      
				 * user        
				 */
				zkClient.writeData("/testUserNode", user);
	}

7.サブスクリプションノードの情報変更(ノードの作成、ノードの削除、サブノードの追加)
public class SubscribeChildChanges {
	private static class ZKChildListener implements IZkChildListener{
        /**
         * handleChildChange:                
         * parentPath:         
         * currentChilds:        
         */
		public void handleChildChange(String parentPath, List currentChilds) throws Exception {
			
			System.out.println(parentPath);
			System.out.println(currentChilds.toString());
			
		}
		
	}
	
	public static void main(String[] args) throws InterruptedException {
        //zk     
		String ZKServers = "192.168.30.164:2181,192.168.30.165:2181,192.168.30.166:2181";
		ZkClient zkClient = new ZkClient(ZKServers,10000,10000,new SerializableSerializer());
		System.out.println("conneted ok!");
		/**
		 * "/testUserNode"      ,                
		 */
		zkClient.subscribeChildChanges("/testUserNode3", new ZKChildListener());
		Thread.sleep(Integer.MAX_VALUE);
    }
}

8.サブスクリプションノードのデータ内容の変化
public class SubscribeDataChanges {
	private static class ZKDataListener implements IZkDataListener{

		public void handleDataChange(String dataPath, Object data) throws Exception {
			
			System.out.println(dataPath+":"+data.toString());
		}

		public void handleDataDeleted(String dataPath) throws Exception {
			
			System.out.println(dataPath);
			
		}
       
		
	}
	
	public static void main(String[] args) throws InterruptedException {
        //zk     
		String ZKServers = "192.168.30.164:2181,192.168.30.165:2181,192.168.30.166:2181";
		ZkClient zkClient = new ZkClient(ZKServers,10000,10000,new SerializableSerializer());
		System.out.println("conneted ok!");

		zkClient.subscribeDataChanges("/testUserNode", new ZKDataListener());
		Thread.sleep(Integer.MAX_VALUE);
		
    }
}