• Home
  • LLMs
  • Python
  • Docker
  • Kubernetes
  • Java
  • Maven
  • All
  • About
ZooKeeper | Curator Framework: Set the ACL of a zNode [setACL]
  1. Notes
  2. Example

  1. Notes:
    The code bellow let you set the ACL of a zNode.

    To set the ACL of a zNode you have to define:
    ► The ACL Scheme: "world", "auth", "digest", "ip".
    ► The ACL Scheme Id: for example, the scheme "world" has a single Id "anyone".
    ► The ACL permissions: "ALL", "ADMIN", "CREATE", "DELETE", "READ", "WRITE".

    Calling the method "CuratorFramework::setACL" will initialize the ACL of a zNode and return a "Stat" object.

    You will get this error if the zNode doesn't exist in ZooKeeper:
    org.apache.zookeeper.KeeperException$NoNodeException: KeeperErrorCode = NoNode for /path

    You will get this error if the ACL is invalid:
    org.apache.zookeeper.KeeperException$InvalidACLException: KeeperErrorCode = InvalidACL for /path

    You will get this error if you don't have permissions to set the ACL of a zNode:
    org.apache.zookeeper.KeeperException$NoAuthException: KeeperErrorCode = NoAuth for /path

    In order for the code bellow to work:
    ► Make sure to update the "CONNECT_STRING" variable with your information.
  2. Example
    Here's an example of how to set the ACL of a zNode:
    final String CONNECT_STRING = "localhost:2181";
    
    final int SESSION_TIMEOUT_MS = Integer.valueOf(60 * 1000);
    final int CONNECTION_TIMEOUT_MS = Integer.valueOf(15 * 1000);
    final RetryPolicy RETRY_POLICY = new RetryOneTime(1000);
    
    // -- CuratorFrameworkFactory::newClient
    final CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient(CONNECT_STRING, SESSION_TIMEOUT_MS, CONNECTION_TIMEOUT_MS, RETRY_POLICY);
    
    // -- CuratorFramework::start
    curatorFramework.start();
    
    // -- CuratorFramework::setACL
    final String path = "/abc";
    
    // using scheme:id:perms syntax
    final List<ACL> acls = AclParser.parse("ip:127.0.0.1:cdraw,world:anyone:rwcda");
    
    // using ACL class
    {
        final ACL acl = new ACL();
    
        acl.setId(new Id("ip", "127.0.0.2"));
        acl.setPerms(ZooDefs.Perms.ALL);
        // acl.setPerms(ZooDefs.Perms.READ);
        // acl.setPerms(ZooDefs.Perms.WRITE);
        // acl.setPerms(ZooDefs.Perms.CREATE);
        // acl.setPerms(ZooDefs.Perms.DELETE);
        // acl.setPerms(ZooDefs.Perms.ADMIN);
    
        acls.add(acl);
    }
    
    final Stat stat = curatorFramework.setACL().withACL(acls).forPath(path);
    
    System.out.println(stat);
    
    // -- CuratorFramework::close
    curatorFramework.close();
© 2025  mtitek