Hadoop權威指南:HDFS-目錄,查詢文件系統,刪除文件
目錄
目錄
FileSystem
實例提供了創建目錄的方法
public boolean mkdirs(Path f) throws IOException
這個方法一次性創建所有必要但還沒有的父目錄
通常不需要顯式創建一個目錄,因為調用create()
方法寫入文件時會自動創建所有父目錄
查詢文件系統
文件元數據:FileStatus
FileStatus
類封裝了文件系統中文件和目錄的元數據包括文件長度,塊大小,副本,修改時間,所有者,權限信息FileSystem
的getFileStatus
方法用於獲取文件或目錄的FileStatus
對象- 使用
exists()
方法檢查文件或者目錄是否存在
列出文件
使用FileSystem
的listStatus()
方法
public FileStatus[] listStatus(Path f) throws IOException
public FileStatus[] listStatus(Path f, PathFilter filter) throws IOException
public FileStatus[] listStatus(Path[] files) throws IOException
public FileStatus[] listStatus(Path[] files, PathFilter filter) throws IOException
- 傳入的Path參數可以是一個文件,也可以是一個目錄
- 允許使用PathFilter來限制匹配的文件和目錄
顯示Hadoop文件系統中一組路徑的文件信息
代碼
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FileUtil;
import org.apache.hadoop.fs.Path;
import java.io.IOException;
import java.net.URI;
public class ListStatus {
public static void main(String[] args) throws IOException {
String uri = args[0];
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(URI.create(uri), conf);
Path[] paths = new Path[args.length];
for (int i=0; i < paths.length; ++i) {
paths[i] = new Path(args[i]);
}
FileStatus[] status = fs.listStatus(paths);
// stat2Path2方法將一個FileStatus對象數組轉換為一個Path對象數組
Path[] listedPaths = FileUtil.stat2Paths(status);
for (Path p : listedPaths) {
System.out.println(p);
}
}
}
編譯
javac ListStatus.java
運行
hadoop ListStatus hdfs://localhost:9000/user/hadoop/input hdfs://localhost:9000/user/hadoop/output
文件模式
Hadoop為執行通配[1]提供了兩個FileSystem
方法
public FileStatus[] globStatus(Path pathPattern) throws IOException
public FileStatus[] globStatus(Path pathPattern, PathFilter filter) throws IOException
globStatus()
方法返回與其路徑匹配於指定模式的所有文件的FileStatus
對象數組,並按路徑排序PathFilter
命令作為可選項可以進一步對匹配結果進行限制
Hadoop支持的通配符與Unix bash的相同
通配符 | 名稱 | 匹配 |
---|---|---|
* | 星號 | 匹配0或多個字符 |
? | 問號 | 匹配單一字符 |
[ab] | 字符類 | 匹配{a,b}集合中的一個字符 |
[^ab] | 非字符類 | 匹配非{a,b}集合中的一個字符 |
[a-b] | 字符范圍 | 匹配一個在a-b范圍內的字符(包括a,b),a在字典順序上要小於或等於b |
[^a-b] | 非字符范圍 | 匹配一個不在a-b范圍內的字符(包括a,b),a在字典順序上要小於或等於b |
{a,b} | 或選擇 | 匹配包含a或b中的一個的表達式 |
\c | 轉義字符 | 匹配元字符c |
PathFilter對象
- 通配符模式並不總能描述我們想要訪問的文件集
FileSystem
中的listStatus()
和globStatus()
方法提供了可選的PathFilter
對象, 以編程方式控制通配符
package org.apache.hadoop.fs;
public interface PathFilter {
boolean accept(Path path);
}
pathFilter
和java.io.FileFilter
一樣,是Path
對象, 而不是File
對象
PathFilter用於排除匹配正則表達式的路徑
代碼
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.PathFilter;
public class RegexExcludePathFilter implements PathFilter {
private final String regex;
public RegexExcludePathFilter(String regex) {
this.regex = regex;
}
@Override
public boolean accept(Path path) {
return !path.toString().matches(regex);
}
}
刪除數據
使用 FileSystem
的 delete()
方法可以永久性刪除文件或目錄
public boolean delete(Path f, boolean recursive) throws IOException
- 如果f是一個文件或空目錄, 那么
recursive
的值會被忽略 - 只有在
recursive
值為true
時,非空目錄及其內容才會被刪除, 否則會拋出IOException異常
在一個表達式中使用通配符來匹配多個文件是比較方便的,無需列舉每個文件和目錄來指定輸入,該操作稱為"通配" ↩︎