| | 1 | {{{ |
| | 2 | #!html |
| | 3 | <div style="text-align: center; color:#151B8D"><big style="font-weight: bold;"><big><big> |
| | 4 | HDFS 的檔案系統操作 |
| | 5 | </big></big></big></div> <div style="text-align: center; color:#7E2217"><big style="font-weight: bold;"><big> |
| | 6 | 刪除 |
| | 7 | </big></big></div> |
| | 8 | }}} |
| | 9 | |
| | 10 | [wiki:waue/2011/0426_2_2 上一關 < ] 第三關 [[wiki:waue/2011/0426 >> 回課程大綱] |
| | 11 | |
| | 12 | = Ex3. 檢查 hdfs 上的資料,有則刪除之 = |
| | 13 | |
| | 14 | * 接續 Ex2. 開始作,並檢查 hdfs 有此資料夾: /user/hadoop/program_put_input |
| | 15 | |
| | 16 | {{{ |
| | 17 | #!java |
| | 18 | package org.nchc.hadoop; |
| | 19 | |
| | 20 | import java.io.IOException; |
| | 21 | |
| | 22 | import org.apache.hadoop.conf.Configuration; |
| | 23 | import org.apache.hadoop.fs.FileSystem; |
| | 24 | import org.apache.hadoop.fs.Path; |
| | 25 | |
| | 26 | |
| | 27 | public class CheckAndDelete { |
| | 28 | // checkAndDelete函式,檢查是否存在該資料夾,若有則刪除之 |
| | 29 | static boolean checkAndDelete(final String path, Configuration conf) { |
| | 30 | Path dst_path = new Path(path); |
| | 31 | try { |
| | 32 | // 產生操作hdfs的物件 |
| | 33 | FileSystem hdfs = dst_path.getFileSystem(conf); |
| | 34 | // 檢查是否存在 |
| | 35 | if (hdfs.exists(dst_path)) { |
| | 36 | // 有則刪除 |
| | 37 | hdfs.delete(dst_path, true); |
| | 38 | return true; |
| | 39 | }else{ |
| | 40 | return false; |
| | 41 | } |
| | 42 | |
| | 43 | } catch (IOException e) { |
| | 44 | e.printStackTrace(); |
| | 45 | return false; |
| | 46 | |
| | 47 | } |
| | 48 | |
| | 49 | } |
| | 50 | static public void main(String args[]){ |
| | 51 | Configuration conf = new Configuration(); |
| | 52 | String path = "/user/hadoop/program_put_input"; |
| | 53 | |
| | 54 | boolean status = checkAndDelete( path, conf); |
| | 55 | System.err.println("delete? :" + status); |
| | 56 | |
| | 57 | } |
| | 58 | } |
| | 59 | |
| | 60 | }}} |
| | 61 | |
| | 62 | * 請檢查 hdfs 無該資料夾: /user/hadoop/program_put_input |