从java中删除hdfs文件夹

Juh*_*uh_ 9 java hadoop hdfs

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

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

org.apache.hadoop.fs.FileUtil.fullyDelete(new File(url))
Run Code Online (Sandbox Code Playgroud)

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

url = "hdfs://hdfshost:port/the/folder/to/delete";
Run Code Online (Sandbox Code Playgroud)

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

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

注意:是一个简单的项目.

小智 18

这个网站适合我.

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

import org.apache.hadoop.fs.*;

...
Configuration conf = new Configuration();

Path output = new Path("/the/folder/to/delete");
FileSystem hdfs = FileSystem.get(URI.create("hdfs://namenode:port"),conf);

// delete existing directory
if (hdfs.exists(output)) {
    hdfs.delete(output, true);
}

Job job = Job.getInstance(conf, "word count");
...
Run Code Online (Sandbox Code Playgroud)

您无需hdfs://hdfshost:port显式添加.


Tuc*_*ker 10

我是这样做的:

    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://<namenode-hostname>:<port>"), conf);
    hdfs.delete("/path/to/your/file", isRecursive);
Run Code Online (Sandbox Code Playgroud)

您不需要hdfs://hdfshost:port/在文件路径中

  • 似乎现在不推荐使用delete方法. (3认同)