| | 7 | * 延續上一個實作,我們也可以參考 FsShell.java 的 test(String argv[], int i) 函數,來撰寫檢驗 HDFS 狀態的程式 |
| | 8 | {{{ |
| | 9 | #!java |
| | 10 | 751 int test(String argv[], int i) throws IOException { |
| | 11 | 752 if (!argv[i].startsWith("-") || argv[i].length() > 2) |
| | 12 | 753 throw new IOException("Not a flag: " + argv[i]); |
| | 13 | 754 char flag = argv[i].toCharArray()[1]; |
| | 14 | 755 Path f = new Path(argv[++i]); |
| | 15 | 756 FileSystem srcFs = f.getFileSystem(getConf()); |
| | 16 | 757 switch(flag) { |
| | 17 | 758 case 'e': |
| | 18 | 759 return srcFs.exists(f) ? 0 : 1; |
| | 19 | 760 case 'z': |
| | 20 | 761 return srcFs.getFileStatus(f).getLen() == 0 ? 0 : 1; |
| | 21 | 762 case 'd': |
| | 22 | 763 return srcFs.getFileStatus(f).isDir() ? 0 : 1; |
| | 23 | 764 default: |
| | 24 | 765 throw new IOException("Unknown flag: " + flag); |
| | 25 | 766 } |
| | 26 | 767 } |
| | 27 | }}} |