I'm trying to write data to ZooKeeper using transaction in the Apache Curator library in Java.
Here's my code:
private CuratorFramework client;
...
private void writeToZK() {
String nodePath = "/path/to/node";
client.inTransaction().create().forPath(nodePath).and().commit(); // problem
}
(I'm using curator v2.9.0)
This throws:
org.apache.zookeeper.KeeperException$NoNodeException: KeeperErrorCode = NoNode
at org.apache.zookeeper.KeeperException.create(KeeperException.java:111)
at org.apache.zookeeper.ZooKeeper.multiInternal(ZooKeeper.java:949)
at org.apache.zookeeper.ZooKeeper.multi(ZooKeeper.java:915)
I dug into ZooKeeper.multiInternal(), it got zookeeper response containing
OpResult: err = -101, type = -1.
If I replace the "// problem" line with this, it works:
client.create().creatingParentsIfNeeded().forPath(nodePath); // runs with no problem
Any idea?
In zookeeper every node are defined with a path. Please have a look to the documentation: https://zookeeper.apache.org/doc/trunk/zookeeperOver.html They are organised like a standard file system. Which mean you cannot create > "/path/to/node" if "/path/to" does not exist. The solution you propose is correct because if the parent of your node does not exist it will be automatically created by zookeeper.