Zookeeper CLI - wildcard support
Asked Answered
H

1

7

Looks like zookeeper CLI (zkCli.sh) does not support wildcard - I have not looked at the zookeeper code to figure out whether it is not possible design wise or whether I am missing something silly here.

What is the best way then to recursively delete nodes starting with a string. I would want to do something like:

./zkCli.sh rmr abc*

to delete all nodes that begin with abc. Is there any simpler way out other than using Java/Python or similar clients? Or in other words, is this achievable through only ZK CLI?

Hosanna answered 31/7, 2014 at 6:35 Comment(0)
A
11

No, the current zkCli.sh does not support wildcard removal (according to the implementation of DeleteCommand and DeleteAllCommand, which are classes used by zkCli.sh)

However, it is pretty straigh-forward to create a workaround, e.g. in Python with Kazoo. Checkout this gist, usage is: python zkDelAll.py /abc

Interestingly, * is a valid path character, so you can create a znode with with a path /abc*.

EDIT: The core implementation from the gist is the following:

from kazoo.client import KazooClient
zk = KazooClient(hosts='localhost:2181')
zk.start()
for child in zk.get_children('/'):
    if child.startswith('abc'):
        zk.delete('/' + child)
zk.stop()
Airla answered 3/1, 2015 at 15:40 Comment(3)
While this may answer the question, it would be preferable to include more of the essential parts of the answer here, and provide the link for reference.Wearproof
I did figure out eventually that it may not be possible to do this without using a client and wrote one of my own. Also changed the Zookeeper CLI code to support wildcard - put up a mail and opened a <a href="issues.apache.org/jira/browse/…> to check if it would be an useful addition to CLI. Also, figured out that abc* is a valid path and hence I used switch which accepts Java regex in my Zookeeper cli code to do bulk delete or ls for a given path.Hosanna
Thanx for the solution. It works. A simple info, if you encounter an error like this 'NotEmptyError' it is because of the child you want to delete has children. If you want to delete in this case, you just add recursive=True to zk.delete as parameter such as zk.delete('/' + child, recursive=True)Storfer

© 2022 - 2024 — McMap. All rights reserved.