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