我正在撰寫一個 Java 類來創建一個 HDFS 目錄,但想在創建目錄之前檢查該目錄是否存在。我不確定是使用 Path.getFileSystem() 還是 FileSystem.get():
Configuration conf = new Configuration();
Path path = new Path(args[1]);
FileSystem fs = path.getFileSystem(conf);
if(fs.exists(path)) {
System.err.println("Dir already exists");
}
boolean status = fs.mkdirs(path);
或者我應該使用 FileSystem.get() 方法:
Configuration conf = new Configuration();
Path path = new Path(args[1]);
FileSystem fs = FileSystem.get(conf);
boolean status = fs.mkdirs(path);
if(!status) {
System.err.println("Dir already exists");
}
什么時候適合使用其中之一:Path.getFileSystem() 或 FileSystem.get()?
uj5u.com熱心網友回復:
這取決于您是否已經有一個非空Path物件。
空安全方法是使用Configuration帶有靜態方法的物件FileSystem.get(conf)
uj5u.com熱心網友回復:
如果您有多個命名空間,則建議使用path.getFileSystem(conf);
FileSystem.get(conf)將始終回傳默認檔案系統
path.getFileSystem(conf)將回傳與指定路徑相關的檔案系統,
如果它是完整路徑(例如hdfs://haservice2/dir1)并且默認命名空間是hdfs://haservice1 FileSystem.get(conf)將回傳 FS 到hdfs://haservice1 并且 path.getFileSystem(conf)將將作為 FS 的 FS wrt 路徑回傳到hdfs://haservice2/,因此對該路徑的 FS 呼叫將成功,如果我們使用FileSystem.get(conf) 中的 FS,情況就不會如此
如果路徑不包含方案和權限(例如,只是 /dir),則 path.getFileSystem(conf) 和 FileSystem.get(conf) 的行為將相同。它們都將回傳由Fs.defaultFs配置指定的默認檔案系統。
在代碼方面, path.getFileSystem(conf) 也僅使用決議的 URI 呼叫 FileSystem.get(..) 。
* Return the FileSystem that owns this Path.
*
* @param conf the configuration to use when resolving the FileSystem
* @return the FileSystem that owns this Path
* @throws java.io.IOException thrown if there's an issue resolving the
* FileSystem
*/
public FileSystem getFileSystem(Configuration conf) throws IOException {
return FileSystem.get(this.toUri(), conf);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/316025.html
上一篇:如何計算區塊鏈/IPFS和Hadoop/HDFS的可用性?
下一篇:HDFS查找丟失塊的最后已知位置
