1.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虚拟机运行
[[email protected] hadoop]# hadoop jar /usr/local/software/JarDeleteFile.jar
结果为:true
3.查看HDFS文件系统是否成功删除
[[email protected] hadoop]# hadoop dfs -ls /user/root/
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/8934.html