HDFS的Java客户端操作代码(HDFS删除文件或目录)

时间:2022-09-21 08:33:11

1.HDFS删除文件或目录

HDFS的Java客户端操作代码(HDFS删除文件或目录)HDFS的Java客户端操作代码(HDFS删除文件或目录)
 1 package Hdfs;
2
3 import java.io.IOException;
4 import java.net.URI;
5
6 import org.apache.hadoop.conf.Configuration;
7 import org.apache.hadoop.fs.FileSystem;
8 import org.apache.hadoop.fs.Path;
9 /**
10 * 删除HDFS上的文件或目录实例
11 * @author SimonsZhao
12 *
13 */
14 public class DeleteFile {
15 public static void main(String[] args) {
16 String uri="hdfs://neusoft-master:9000/user/root/test1";
17 Configuration conf = new Configuration();
18 try {
19 FileSystem fs = FileSystem.get(URI.create(uri),conf);
20 Path delPath = new Path("hdfs://neusoft-master:9000/user/root/test1");
21 boolean isDeleted = fs.delete(delPath,false);
22 //boolean isDeleted = fs.delete(delPath,true);//递归删除
23 System.out.println(isDeleted);
24 } catch (IOException e) {
25 e.printStackTrace();
26 }
27 }
28 }
HDFS非递归删除目录或文件

2.打成Jar包并提交至linux虚拟机运行

[root@neusoft-master hadoop]# hadoop jar /usr/local/software/JarDeleteFile.jar 

  HDFS的Java客户端操作代码(HDFS删除文件或目录)

结果为:true

3.查看HDFS文件系统是否成功删除

[root@neusoft-master hadoop]# hadoop dfs -ls /user/root/

  HDFS的Java客户端操作代码(HDFS删除文件或目录)