从java中删除hdfs文件夹 [英] Delete hdfs folder from java

查看:1366
本文介绍了从java中删除hdfs文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我发现我可以使用该方法

  org.apache.hadoop.fs.FileUtil.fullyDelete(new File(url))
  url =hdfs:// hdfshost:port / the / folder / to / delete ; 

with 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://< namenode-hostname>:< port>),conf);
hdfs.delete(/ path / to / your / file,isRecusrive);

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

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";

with hdfs://hdfshost:port 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.

解决方案

I do it this way:

    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", isRecusrive);

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

这篇关于从java中删除hdfs文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆