Changes between Initial Version and Version 1 of waue/2010/0115-del


Ignore:
Timestamp:
Jan 19, 2010, 7:24:42 PM (14 years ago)
Author:
waue
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • waue/2010/0115-del

    v1 v1  
     1
     2 = 刪除 hdfs 上得檔案 =
     3
     4{{{
     5#!java
     6
     7import java.io.IOException;
     8
     9import org.apache.hadoop.conf.Configuration;
     10import org.apache.hadoop.fs.FileSystem;
     11import org.apache.hadoop.fs.Path;
     12
     13
     14public class CheckAndDelete {
     15        // checkAndDelete函式,檢查是否存在該資料夾,若有則刪除之
     16        static boolean checkAndDelete(final String path, Configuration conf) {
     17                Path dst_path = new Path(path);
     18                try {
     19                        // 產生操作hdfs的物件
     20                        FileSystem hdfs = dst_path.getFileSystem(conf);
     21                        // 檢查是否存在
     22                        if (hdfs.exists(dst_path)) {
     23                                // 有則刪除
     24                                hdfs.delete(dst_path, true);
     25                               
     26                        }
     27
     28                } catch (IOException e) {
     29                        e.printStackTrace();
     30                        return false;
     31                }
     32                return true;
     33        }
     34        static public void main(String args[]){
     35                Configuration conf = new Configuration();
     36                String path = "/user/waue/input";
     37
     38                boolean status = checkAndDelete( path, conf);
     39                System.err.println("create? :" + status);
     40               
     41        }
     42}
     43
     44}}}