| 1 | |
| 2 | = 刪除 hdfs 上得檔案 = |
| 3 | |
| 4 | {{{ |
| 5 | #!java |
| 6 | |
| 7 | import java.io.IOException; |
| 8 | |
| 9 | import org.apache.hadoop.conf.Configuration; |
| 10 | import org.apache.hadoop.fs.FileSystem; |
| 11 | import org.apache.hadoop.fs.Path; |
| 12 | |
| 13 | |
| 14 | public 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 | }}} |