首页 > 解决方案 > Hadoop:Path.getFileSystem 与 FileSystem.get

问题描述

我正在编写一个 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()?

标签: javahadoophdfs

解决方案


这取决于您是否已经有一个非空Path对象。

空安全方法是使用Configuration具有静态方法的对象FileSystem.get(conf)


推荐阅读