使用 FileSystem JAVA API 對 HDFS 進行讀、寫、刪除等操作__JAVA

來源:互聯網
上載者:User
Hadoop檔案系統 
基本的檔案系統命令操作, 通過hadoop fs -help可以擷取所有的命令的詳細協助檔案。 

Java抽象類別org.apache.hadoop.fs.FileSystem定義了hadoop的一個檔案系統介面。該類是一個抽象類別,通過以下兩種靜態Factory 方法可以過去FileSystem執行個體: 
public static FileSystem.get(Configuration conf) throws IOException 
public static FileSystem.get(URI uri, Configuration conf) throws IOException 

具體方法實現: 
1、public boolean mkdirs(Path f) throws IOException 
一次性建立所有目錄(包括父目錄), f是完整的目錄路徑。 

2、public FSOutputStream create(Path f) throws IOException 
建立指定path對象的一個檔案,返回一個用於寫入資料的輸出資料流 
create()有多個重載版本,允許我們指定是否強制覆蓋已有的檔案、檔案備份數量、寫入檔案緩衝區大小、檔案塊大小以及檔案許可權。 

3、public boolean copyFromLocal(Path src, Path dst) throws IOException 
將本地檔案拷貝到檔案系統 

4、public boolean exists(Path f) throws IOException 
檢查檔案或目錄是否存在 

5、public boolean delete(Path f, Boolean recursive) 
永久性刪除指定的檔案或目錄,如果f是一個空目錄或者檔案,那麼recursive的值就會被忽略。只有recursive=true時,一個非空目錄及其內容才會被刪除。 

6、FileStatus類封裝了檔案系統中檔案和目錄的中繼資料,包括檔案長度、塊大小、備份、修改時間、所有者以及許可權資訊。 

通過"FileStatus.getPath()"可查看指定HDFS中某個目錄下所有檔案。 

01 package hdfsTest;
02  
03 import java.io.IOException;
04  
05 import org.apache.hadoop.conf.Configuration;
06 import org.apache.hadoop.fs.FSDataOutputStream;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.