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中某個目錄下所有檔案。
| 03 |
import java.io.IOException; |
| 05 |
import org.apache.hadoop.conf.Configuration; |
| 06 |
import org.apache.hadoop.fs.FSDataOutputStream; |