This is one simple program to connect to the server and fetch some data on
znode.
Please go through it it may help you.
by,
Sunil Singh

import java.io.*;
import java.util.List;
//import java.util.List;
import org.apache.zookeeper.*;
import org.apache.zookeeper.Watcher.Event.KeeperState;
//import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Stat;
//import org.apache.zookeeper.data.ACL;
//import org.apache.zookeeper.ZooDefs.Ids;
//import org.apache.zookeeper.CreateMode;


class DisplayChange 
{
        static ZooKeeper zk;
        
        
        //connection establishment
        public void connect() throws IOException, KeeperException
        {
                zk = new ZooKeeper("10.10.10.10:2181", 3000, new Watcher()
                {
                        public void process(WatchedEvent event)
                        {
                                if(event.getState()== KeeperState.SyncConnected)
                                        System.out.println("Connected to 
server");
                        }
                });
                
        }
        
        // connection close
        public void close() throws InterruptedException
        {
                zk.close();
                System.out.println("Connection closed");
        }

        public static void main(String args[])throws KeeperException, 
IOException,
InterruptedException
        {
                DisplayChange myzoo = new DisplayChange();
                myzoo.connect();
                //setting watch with GetData();
                Stat stat = new Stat();
                byte [] getdata = zk.getData("/zk/abc", true, stat);
                String s = new String(getdata);
                String s2 = new String(s); //saved data of privious znode
                //System.out.println("First time data of znode is  :"+s);
        
                //setting new data for znode.
                byte [] newdata = {'c','o','n','f','i','g','_','=','4'};
                zk.setData("/zk/abc", newdata,-1);
                //zk.setData(path, data, version)
                
                byte [] getnewdata = zk.getData("/zk/abc", true, stat);
                String snew = new String(getnewdata);
                System.out.println("new data of /zk/abc is  :"+snew);
                System.out.println("The privious data of /zk/abc:"+s2);
                
                
        
        }
        
}       



--
View this message in context: 
http://zookeeper-user.578899.n2.nabble.com/Zookeeper-programming-with-java-Intial-Stage-Programming-tp7577832p7577854.html
Sent from the zookeeper-user mailing list archive at Nabble.com.

Reply via email to