Hadoop:Path.getFileSystem 对比 FileSystem.get
Hadoop: Path.getFileSystem vs FileSystem.get
我正在写一个Java class创建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()?
这取决于您是否已经有一个非空 Path
对象。
null 安全方法是使用具有静态方法的 Configuration
对象 FileSystem.get(conf)
如果你有多个命名空间,那么建议使用path.getFileSystem(conf);
FileSystem.get(conf) 将始终 return default 文件系统
path.getFileSystem(conf) 将 return 相对于指定路径的文件系统,
如果是完整路径(例如hdfs://haservice2/dir1)且默认命名空间是hdfs://haservice1
FileSystem.get(conf) 将 return FS 到 hdfs://haservice1
和
path.getFileSystem(conf) 将 return FS wrt 路径,即 FS 到 hdfs://haservice2/,因此FS 对该路径的调用将成功,如果我们使用来自 FileSystem.get(conf)
的 FS 则不会出现这种情况
如果路径不包含方案和权限(例如,只是 /dir),path.getFileSystem(conf) 和 FileSystem.get(conf) 的行为是相同的。它们都将 return 由 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);
}
我正在写一个Java class创建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()?
这取决于您是否已经有一个非空 Path
对象。
null 安全方法是使用具有静态方法的 Configuration
对象 FileSystem.get(conf)
如果你有多个命名空间,那么建议使用path.getFileSystem(conf);
FileSystem.get(conf) 将始终 return default 文件系统
path.getFileSystem(conf) 将 return 相对于指定路径的文件系统,
如果是完整路径(例如hdfs://haservice2/dir1)且默认命名空间是hdfs://haservice1 FileSystem.get(conf) 将 return FS 到 hdfs://haservice1 和 path.getFileSystem(conf) 将 return FS wrt 路径,即 FS 到 hdfs://haservice2/,因此FS 对该路径的调用将成功,如果我们使用来自 FileSystem.get(conf)
的 FS 则不会出现这种情况如果路径不包含方案和权限(例如,只是 /dir),path.getFileSystem(conf) 和 FileSystem.get(conf) 的行为是相同的。它们都将 return 由 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);
}