从java中删除hdfs文件夹

在边缘节点上运行的Java应用程序中,我需要删除hdfs文件夹(如果存在)。 我需要在运行在文件夹中输出的mapreduce作业(带有spark)之前执行此操作。

我发现我可以使用这种方法

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

但是,我只能使用本地文件夹(即正在运行的计算机上的文件URL)。 我尝试使用类似的东西:

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

使用hdfs://hdfshost:port是hdfs namenode IPC。 我用它来mapreduce,所以它是正确的。 但它没有做任何事情。

那么,我应该使用什么url,还是有其他方法?

注意: 这是一个简单的项目。

我是这样做的:

  Configuration conf = new Configuration(); conf.set("fs.hdfs.impl",org.apache.hadoop.hdfs.DistributedFileSystem.class.getName()); conf.set("fs.file.impl",org.apache.hadoop.fs.LocalFileSystem.class.getName()); FileSystem hdfs = FileSystem.get(URI.create("hdfs://:"), conf); hdfs.delete("/path/to/your/file", isRecursive); 

你的文件路径中不需要hdfs://hdfshost:port/

这个网站适合我。

只需在我的WordCount程序中添加以下代码即可:

 import org.apache.hadoop.fs.*; ... Configuration conf = new Configuration(); Path output = new Path("/the/folder/to/delete"); FileSystem hdfs = FileSystem.get(conf); // delete existing directory if (hdfs.exists(output)) { hdfs.delete(output, true); } Job job = Job.getInstance(conf, "word count"); ... 

您不需要显式添加hdfs://hdfshost:port