package api; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; public class Utils { public static FileSystem HDFS() throws Exception{ Configuration conf = new Configuration(); conf.set("fs.defaultFS","hdfs://potter2:9000"); System.setProperty("HADOOP_USER_NAME", "potter"); FileSystem fs = FileSystem.get(conf); return fs; } }
package api; import org.apache.hadoop.fs.FileStatus; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.LocatedFileStatus; import org.apache.hadoop.fs.Path; import org.apache.hadoop.fs.RemoteIterator; import org.junit.Test; /** * 刪除HDFS集羣中的全部空文件和空目錄 * @author Administrator * */
public class Empty { @Test public void tt() throws Exception { Path path = new Path("/"); Empty1(path); } public static void Empty1(Path path) throws Exception { FileSystem fs = Utils.HDFS(); //當是空文件時,判斷當前路徑下有幾個空文件夾
FileStatus[] listStatus = fs.listStatus(path); System.out.println(listStatus.length+"********"); //當根目錄沒有文件的時候會進入if裏面
if (listStatus.length == 0) { fs.delete(path,true); } System.out.println("刪除成功xxxxx"); //迭代器用於遍歷
RemoteIterator<LocatedFileStatus> listLocatedStatus = fs.listLocatedStatus(path); while (listLocatedStatus.hasNext()) { LocatedFileStatus next = listLocatedStatus.next(); //輸出文件夾的目錄
Path currentPath = next.getPath(); System.out.println(currentPath+"1111111"); //輸出上面文件夾的父親目錄
Path parent = next.getPath().getParent(); System.out.println(parent+"2222222"); if (next.isDirectory()) { //若是是空文件夾
if (fs.listStatus(currentPath).length == 0) { //刪除掉
fs.delete(currentPath,true); }else { //不是空文件夾,那麼繼續遍歷
if (fs.exists(currentPath)) { Empty1(next.getPath()); } } //若是是文件
}else { //獲取文件的長度
long fileLength = next.getLen(); //當文件是空文件時,刪除
if (fileLength ==0) { fs.delete(currentPath,true); } } int length = fs.listStatus(parent).length; if (length ==0) { fs.delete(parent,true); } } } }