Juh_ Juh_ - 2 years ago 164
Java Question

Delete hdfs folder from java

In a java app running on an edge node, I need to delete a hdfs folder, if it exists. I need to do that before running a mapreduce job (with spark) that output in the folder.

I found I could use the method

org.apache.hadoop.fs.FileUtil.fullyDelete(new File(url))

However, I can only make it work with local folder (i.e. file url on the running computer). I tried to use something like:

url = "hdfs://hdfshost:port/the/folder/to/delete";

being the hdfs namenode IPC. I use it for the mapreduce, so it is correct.
However it doesn't do anything.

So, what url should I use, or is there another method?

Note: here is the simple project in question.

Answer Source

I do it this way:

    Configuration conf = new Configuration();
    FileSystem  hdfs = FileSystem.get(URI.create("hdfs://<namenode-hostname>:<port>"), conf);
    hdfs.delete("/path/to/your/file", isRecusrive);

you don't need hdfs://hdfshost:port/ in your file path

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download