wiki:III140118/Lab12

◢ <實作十一> | <回課程大綱> ▲ | <實作十三> ◣

實作十二 Lab12

Hadoop FileSystem API 實作(三)
判斷檔案是否存在、屬性為何
Testing HDFS Directories and Files

請先連線至 nodeN.3du.me , N 為您的報名編號
  • 延續上一個實作,我們也可以參考 FsShell.java 的 test(String argv[], int i) 函數,來撰寫檢驗 HDFS 狀態的程式
     751   int test(String argv[], int i) throws IOException {
     752     if (!argv[i].startsWith("-") || argv[i].length() > 2)
     753       throw new IOException("Not a flag: " + argv[i]);
     754     char flag = argv[i].toCharArray()[1];
     755     Path f = new Path(argv[++i]);
     756     FileSystem srcFs = f.getFileSystem(getConf());
     757     switch(flag) {
     758       case 'e':
     759         return srcFs.exists(f) ? 0 : 1;
     760       case 'z':
     761         return srcFs.getFileStatus(f).getLen() == 0 ? 0 : 1;
     762       case 'd':
     763         return srcFs.getFileStatus(f).isDir() ? 0 : 1;
     764       default:
     765         throw new IOException("Unknown flag: " + flag);
     766     }
     767   }
    
  • 讓我們來執行觀察程式運作的情形,請剪貼以下步驟,並嘗試思考觀察到的結果:
    cd ~/hadoop_labs/lab006
    ant
    hadoop jar isFile.jar input
    hadoop jar isFile.jar file
    hadoop jar isFile.jar empty
    
Last modified 10 years ago Last modified on Jan 18, 2014, 12:24:19 AM