【HDFS API編程】查看目標文件夾下的所有文件、遞歸查看目標文件夾下的所有文件


使用hadoop命令:hadoop fs -ls /hdfsapi/test  我們能夠查看HDFS文件系統/hdfsapi/test目錄下的所有文件信息

那么使用代碼怎么寫呢?直接先上代碼:(這之后貼上去的代碼怎么就全灰色了?....)

public class HDFSApp {

    public static final String HDFS_PATH = "hdfs://hadoop000:8020";
    FileSystem fileSystem = null;
    Configuration configuration = null;

    @Before
    public void setUp() throws Exception{
        System.out.println("setUp-----------");
        configuration = new Configuration();
        configuration.set("dfs.replication","1");

        /**
         * 構造一個訪問制定HDFS系統的客戶端對象
         * 第一個參數:HDFS的URI
         * 第二個參數:客戶端制定的配置參數
         * 第三個參數:客戶端的身份,說白了就是用戶名
         */
        fileSystem = FileSystem.get(new URI(HDFS_PATH),configuration,"hadoop");
    }
  /** * 查看目標文件夾下的所有文件 * @throws Exception */ @Test public void listFiles() throws Exception{ FileStatus[] statuses = fileSystem.listStatus(new Path("/hdfsapi/test")); for(FileStatus file : statuses){ String isDir = file.isDirectory() ? "文件夾" : "文件"; String permission = file.getPermission().toString(); short replication = file.getReplication(); long length = file.getLen(); String path = file.getPath().toString(); System.out.println(isDir + "\t" + permission + "\t" + replication + "\t" + length + "\t" + path); } }
   @After
    public void tearDown(){
        configuration = null;
        fileSystem = null;
        System.out.println("----------tearDown------");
    }
}

運行測試類:
setUp-----------
log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
文件    rw-r--r--    3    14    hdfs://hadoop000:8020/hdfsapi/test/a.txt
文件    rw-r--r--    1    28    hdfs://hadoop000:8020/hdfsapi/test/c.txt
文件    rw-r--r--    1    181367942    hdfs://hadoop000:8020/hdfsapi/test/jdk.zip
文件    rw-r--r--    1    2732    hdfs://hadoop000:8020/hdfsapi/test/t.txt
文件夾    rwxr-xr-x    0    0    hdfs://hadoop000:8020/hdfsapi/test/testdir
----------tearDown------

首先我們找到fileSystem的listStatus方法,這個方法怎么用?還是那句話:哪里不會Ctrl點哪里。我們點進去能看到方法的源碼信息,能夠知道該方法的返回值是一個FileStatus[]數組類型,所要傳入的參數是目標目錄Path:

 /**
   * List the statuses of the files/directories in the given path if the path is
   * a directory.
   * <p>
   * Does not guarantee to return the List of files/directories status in a
   * sorted order.
   *列出給定路徑中文件/目錄的狀態(如果路徑為目錄。不保證返回排序順序。 
   * @param f given path
   * @return the statuses of the files/directories in the given patch
   * @throws FileNotFoundException when the path does not exist;
   *         IOException see specific implementation
   */
  public abstract FileStatus[] listStatus(Path f) throws FileNotFoundException, 
                                                         IOException;

既然返回的是一個數組類型,我們自然會想到用循環來遍歷,但是FileStatus 這個又是什么呢?Ctrl點進去:

/** Interface that represents the client side information for a file.
 *表示文件的客戶端信息的接口。
 */
@InterfaceAudience.Public
@InterfaceStability.Stable
public class FileStatus implements Writable, Comparable {

  private Path path;
  private long length;
  private boolean isdir;
  private short block_replication;
  private long blocksize;
  private long modification_time;
  private long access_time;
  private FsPermission permission;
  private String owner;
  private String group;
  private Path symlink;

FileStatus這是一個表示文件的客戶端信息的接口,貼上了一些類的成員變量,我們能從中知道這個里面包含了文件的這么多信息接口。自然就能夠使用類里的方法進行訪問取得文件的相關信息了。

測試成功,但是我們發現一個問題,就是這個方法就如hadoop fs -ls /hdfsapi/test 一樣用戶只能查看到當前目錄下的文件信息,倘若文件夾test下還有文件夾testdir,testdir文件夾里還有文件就無法顯示了,所以我們來看看怎么進行遞歸查看目標文件夾下的所有文件。

首先通過hadoop命令遞歸查看: hadoop fs -ls -R /hdfsapi/test    (趕緊試試去 recursive 遞歸)

那么通過代碼怎么實現呢?

我們之前使用的是fileSystem下的listStatus方法,那么我們繼續查看API有沒有能夠使用的,我們看到有一個方法是listFiles

 /**
   * List the statuses and block locations of the files in the given path.
   * Does not guarantee to return the iterator that traverses statuses
   * of the files in a sorted order.
   * 
   * If the path is a directory, 
   *   if recursive is false, returns files in the directory;
   *   if recursive is true, return files in the subtree rooted at the path.
   * If the path is a file, return the file's status and block locations.
   * 
   * @param f is the path
   * @param recursive if the subdirectories need to be traversed recursively
   *
   * @return an iterator that traverses statuses of the files
   *
   * @throws FileNotFoundException when the path does not exist;
   *         IOException see specific implementation
   */
  public RemoteIterator<LocatedFileStatus> listFiles(
      final Path f, final boolean recursive)
  throws FileNotFoundException, IOException {}

所以需要我們不僅善於查看API還要善於查找API。

於是遞歸查看目標文件夾下的所有文件代碼這么寫:

/**
     * 遞歸查看目標文件夾下的所有文件
     * @throws Exception
     */
    @Test
    public void listFilesRecursive() throws Exception{

        RemoteIterator<LocatedFileStatus> files = fileSystem.listFiles(new Path("/hdfsapi/test"),true);

        while (files.hasNext()){
            LocatedFileStatus file = files.next();
            String isDir = file.isDirectory() ? "文件夾" : "文件";
            String permission = file.getPermission().toString();
            short replication = file.getReplication();
            long length = file.getLen();
            String path = file.getPath().toString();
            System.out.println(isDir + "\t" + permission + "\t" + replication + "\t" + length + "\t" + path);
        }
    }

運行測試類:
setUp-----------
log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
文件    rw-r--r--    3    14    hdfs://hadoop000:8020/hdfsapi/test/a.txt
文件    rw-r--r--    1    28    hdfs://hadoop000:8020/hdfsapi/test/c.txt
文件    rw-r--r--    1    181367942    hdfs://hadoop000:8020/hdfsapi/test/jdk.zip
文件    rw-r--r--    1    2732    hdfs://hadoop000:8020/hdfsapi/test/t.txt
文件    rw-r--r--    1    11    hdfs://hadoop000:8020/hdfsapi/test/testdir/h.txt
----------tearDown------

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM