Hadoop权威指南:HDFS-目录,查询文件系统,删除文件
生活随笔
收集整理的這篇文章主要介紹了
Hadoop权威指南:HDFS-目录,查询文件系统,删除文件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
- Hadoop權威指南:HDFS-目錄,查詢文件系統,刪除文件
- 目錄
- 查詢文件系統
- 文件元數據:FileStatus
- 列出文件
- 文件模式
- PathFilter對象
- 刪除數據
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 對象, 以編程方式控制通配符
- 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;}@Overridepublic 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異常
在一個表達式中使用通配符來匹配多個文件是比較方便的,無需列舉每個文件和目錄來指定輸入,該操作稱為"通配"?
轉載于:https://www.cnblogs.com/bovenson/p/5730752.html
總結
以上是生活随笔為你收集整理的Hadoop权威指南:HDFS-目录,查询文件系统,删除文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: node_modules
- 下一篇: Web前端开发css基础样式总结