一、兩個重要的類
1. Configuration類
創建一個Configuration物件時,其構造方法會默認加載hadoop中的兩個組態檔,分別是hdfs-site.xml以及core-site.xml,這兩個檔案中會有訪問hdfs所需的引數值,指定了hdfs的地址,通過這個地址訪問hdfs,
一個應用程式只創建一個Configuration,
2. FileSystem類
該抽象類類提供了豐富的方法用于對檔案系統進行操作,比如創建目錄、洗掉檔案、重命名等,
二、常用函式
1. 判斷路徑是否存在
FileSystem.exists(Path path)
path: 路徑
2. 向HDFS中上傳檔案
FileSystem.copyFromLocalFile(Path src, Path dst)
src: 本地檔案路徑
dst: HDFS檔案系統中的目的路徑
3. 從HDFS中下載檔案
FileSystem.copyToLocalFile(Path src, Path dst)
src: HDFS中檔案路徑
dst: 本地檔案系統中的目的路徑
4. 洗掉HDFS中指定檔案
FileSystem.delete(Path path, Boolen recursive)
path: 指定檔案的路徑
recursive: 是否遞回洗掉,是則為true,否則為false
5. 創建HDFS中的檔案
FileSystem.mkdirs(Path path)
path: 檔案目錄
6. 將檔案從源路徑移動到目的路徑
FileSystem.rename(Path src, Path dst)
src: HDFS中源路徑
dst: HDFS中目的路徑
7.獲取檔案資訊(大小、權限等)
首先需要創建一個陣列存放該目錄下所有檔案的FileStatus物件,
FileStatus[] fileStatuses = hfs.listStatus(HDFSPath);
//hfs為自己創建的FileSystem物件
·fileStatuses[i].getLen(): 回傳的值型別為long,檔案大小,
·fileStatuses[i].getOwner(): 回傳值型別為String,所有者資訊,
·fileStatuses[i].getPath(): 回傳值型別為Path,檔案路徑,
·fileStatuses[i].getPermission(): 回傳值型別為FsPermission,檔案權限,
·fileStatuses[i].getModificationTime(): 回傳值型別為long,修改時間,
判斷目錄是否為空:
if(fileStatuses.length > 0){
System.out.println("該目錄不為空!");
}else{
System.out.println("該目錄為空");
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/336196.html
標籤:其他
上一篇:Java API實作HDFS有關目錄與檔案的相關功能
下一篇:ElasticSearch的由來
