Search code examples
pythonvolumemapr

How to check if a MapR volume exists with Python?


I am struggling with MapR volumes... I am trying to extend a volume structure which has also folders. In other words I want to check if a volume path exists and if not to create it, but I don't know how I can make that check. I am not that familiar with MapR and volumes.


Solution

  • There are a couple of answers here. The first thing to establish, however, is that for many purposes, a volume in MapR (now known as the HPE Ezmeral Data Fabric) looks just like a directory. That means that, for many purposes, "checking to see if a volume exists" just looks like "check to see if a directory exists". That is, of course, trivial in Python.

    But a MapR volume does have some management super-powers that directories don't have. As such, you might want to know whether a volume with the desired mount point exists. This python code should be roughly what you need:

    import subprocess
    import json
    s = subprocess.run(["maprcli", "volume", "list", "-json"], stdout=subprocess.PIPE)
    for x in json.loads(s.stdout)['data']:
        if x['mountdir'] == "/user/tdunning":
            print("Found %s mounted at %s" % (x['volumename'], x['mountdir']))
    

    This will do fine up to thousands of volumes. Some people have millions of volumes, however, so you probably need to limit your search using the volume name based on some convention. That can be done in a very similar way as the code above, but you probably want to use a command like maprcli volume info -name tdunning.home -json instead.

    See the following links for more info on these commands:

    https://docs.datafabric.hpe.com/62/ReferenceGuide/volume-info.html

    https://docs.datafabric.hpe.com/62/ReferenceGuide/volume-list.html