HDFS的Java提供者

來源:互聯網
上載者:User

得到filesystem的執行個體

 有兩個靜態方法可以得到filesystem介面的執行個體

 public static FileSystem get(Configuration conf) throws IOException
 public static FileSystem get(URI uri, Configuration conf) throws IOException

 第一個方法得到預設的檔案系統,具體由設定檔的fs.default.name
屬性決定。

 第二個方法由URI的首碼決定是哪個檔案系統(參考前一篇文章),如果URI沒有首碼也是得到預設的檔案系統。

 使用open方法得到檔案的輸入資料流


 

 
有了filesystem的執行個體,我們就可以調用open方法得到某個檔案的輸入資料流了

 public FSDataInputStream open(Path f) throws IOException
 public abstract FSDataInputStream open(Path f, int bufferSize) throws IOException

 第一個方法用預設的buffer大小4K

 使用FSDataInputStream讀資料


 

 FSDataInputStream派生自java.io.DataInputStream,支援隨機讀取。

 public class FSDataInputStream extends DataInputStream
    implements Seekable, PositionedReadable {

    void seek(long pos) throws IOException;

    public int read(long position, byte[] buffer, int offset, int length) throws IOException;

}

 使用FSDataOutputStream寫資料

 
filesystem介面建立檔案的介面

 public FSDataOutputStream create(Path f) throws IOException

 還可以append到一個已經存在的檔案

 public FSDataOutputStream append(Path f) throws IOException

 public class FSDataOutputStream extends DataOutputStream implements Syncable {

   public void write(int b) throws IOException;

   public void write(byte b[], int off, int len) throws IOException;

 }

 filesystem的檔案系統操作方法

 public boolean mkdirs(Path f) throws IOException ;

 public void copyToLocalFile(boolean delSrc, Path src, Path dst) throws IOException;

 public abstract boolean delete(Path f, boolean recursive) throws IOException;

 public abstract boolean rename(Path src, Path dst) throws IOException;

 public abstract FileStatus[] listStatus(Path f) throws IOException;

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.