【HDFS API編程】查看文件塊信息

如今咱們把文件都存在HDFS文件系統之上,如今有一個jdk.zip文件存儲在上面,咱們想知道這個文件在哪些節點之上?切成了幾個塊?每一個塊的大小是怎麼樣?先上測試類代碼:html

   /**
     * 查看文件塊信息
     * @throws Exception
     */
    @Test  
    public void getFileBlockLocations() throws Exception{
        FileStatus fileStatus = fileSystem.getFileStatus(new Path("/hdfsapi/test/jdk.zip"));
        BlockLocation[] blocks = fileSystem.getFileBlockLocations(fileStatus,0,fileStatus.getLen());
        for(BlockLocation block : blocks){
            for(String name : block.getNames() )
            System.out.println(name + " : " + block.getOffset() + " : " + block.getLength());
        }
    }

咱們使用fileSystem的getFileStatus方法得到文件的狀態信息,而後使用fileSystem下的getFileBlockLocations方法獲取塊的信息,而後對塊的信息進行迭代。咱們點開block.getNames()方法,它返回的是一個字符串類型,由於文件被切成塊了因此會有不少的名字,因此咱們再對name進行迭代而後進行輸出每一個塊的名字+偏移量+長度。對於API方法的使用仍是那句話:哪裏不會Ctrl點哪裏,源碼裏面註釋寫的清楚着呢。apache

測試類輸出:
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.
192.168.42.110:50010 : 0 : 134217728
192.168.42.110:50010 : 134217728 : 47150214
----------tearDown------
相關文章
相關標籤/搜索