1. 程式人生 > >zookeeper遍歷和設定acl

zookeeper遍歷和設定acl

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;




import org.apache.hadoop.hbase.util.Bytes;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Id;
import org.apache.zookeeper.data.Stat;












public class ZKGetChild_setACL {




     // 會話超時時間,設定為與系統預設時間一致




     private static final int SESSION_TIMEOUT=30000;




    




     // 建立 ZooKeeper 例項




     ZooKeeper zk;




    




     // 建立 Watcher 例項




     Watcher wh=new Watcher(){




            public void process(org.apache.zookeeper.WatchedEvent event)




            {




                    System.out.println(event.toString());




            }




     };




    




     // 初始化 ZooKeeper 例項




     private void createZKInstance() throws IOException




     {             




            zk=new ZooKeeper("10.xxx.xxx.24:2181",ZKGetChild_setACL.SESSION_TIMEOUT,this.wh);








     }




    




     private void getChild(String path) throws IOException,InterruptedException,KeeperException




     {
     
    Id id = new Id("ip", "10.xxx.xxx.23/27");  
         ACL acl = new ACL(ZooDefs.Perms.ALL, id);  
         List<ACL> acls = new ArrayList<ACL>();  
         acls.add(acl);  
         zk.setACL(path, acls, -1);
         System.out.println("set ACL succ:"+path);




            List<String> child_list=zk.getChildren(path, false);
           for(String child:child_list){
          System.out.println("child:"+child);
          if(path.equals("/")){
          child=path+""+child;
          System.out.println("A:"+child);
          }else{
          child=path+"/"+child;
          System.out.println("B:"+child);
          }
          
          System.out.println(new String(zk.getData(child,false,null)));
          
          //child=path+""+child;
          Stat stat = zk.exists(child, false);
          List<ACL> child_acl=zk.getACL(child, stat);
          for (ACL a : child_acl) {
        //System.out.println(a.getPerms());
        System.out.println(a);
        
        System.out.println("**********");
          }
          
          getChild(child);
           }
     }
     
     
     private void confAcl(String path) throws KeeperException, InterruptedException 




     {
     
    Id id = new Id("world", "anyone"); //放開許可權
    Id id2 = new Id("ip", "10.xxx.xxx.24"); //限定ip
    
         ACL acl = new ACL(ZooDefs.Perms.ALL, id); 
         ACL acl2 = new ACL(ZooDefs.Perms.ALL, id2); 
         
         
         List<ACL> acls = new ArrayList<ACL>();  
         acls.add(acl);  
         acls.add(acl2);
        
         zk.setACL(path, acls, -1);
         System.out.println("set ACL succ:"+path);




        
     }




     private void ZKClose() throws  InterruptedException
     {
            zk.close();
     }




     public static void main(String[] args) throws IOException,InterruptedException,KeeperException {




            ZKGetChild_setACL dm=new ZKGetChild_setACL();




            dm.createZKInstance( );




            dm.confAcl("/hbase/master");




            dm.ZKClose();




     }




}