HDFS的Java訪問接口


得到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;
本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/fiberlijun/archive/2009/11/16/4814647.aspx
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章