• Home
  • LLMs
  • Python
  • Docker
  • Kubernetes
  • Java
  • Maven
  • All
  • About
ZooKeeper | Curator Framework: Set the data of a zNode with a specific data version [setData::withVersion]
  1. Notes
  2. Example

  1. Notes:
    The code bellow let you set the data of a zNode if its data version matches the specified version.

    Calling the method "CuratorFramework::setData" will initialize the data 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 version of the zNode (dataVersion) is incorrect:
    org.apache.zookeeper.KeeperException$BadVersionException: KeeperErrorCode = BadVersion 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 data of a zNode if its data version matches the specified version:
    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::setData::withVersion
    final String path = "/abc";
    final String data = "zNode data ...";
    final int version = 1;
    final Stat stat = curatorFramework.setData().withVersion(version).forPath(path, data.getBytes());
    
    System.out.println(stat);
    
    // -- CuratorFramework::close
    curatorFramework.close();
© 2025  mtitek