java hdfs删除文件夹,从java中删除hdfs文件夹

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://:"), conf);

hdfs.delete("/path/to/your/file", isRecusrive);

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值