hadoop FileSystem是否要close

先来说结论,最好不要close,让hadoop自己close,否则容易把进程里其他获取fs的地方一起关了。这是因为 FileSystem.get(Configuration)做了缓存的原因。当然可以设置

conf.setBoolean("fs.hdfs.impl.disable.cache", true);

就不缓存fs,但是这可能会导致性能问题,因为每个获取都要建立一个和namenode的连接。但是假如不关,会导致泄露吗,其实不会,是因为hdfs自己做了关闭的操作,它有一个shutdown的hook会负责最后关掉这些FileSystem。

缓存代码如下:

public static FileSystem get(Configuration conf) throws IOException {
    return get(getDefaultUri(conf), conf);
  }

  /** Returns the FileSystem for this URI's scheme and authority.  The scheme
   * of the URI determines a configuration property name,
   * <tt>fs.<i>scheme</i>.class</tt> whose value names the FileSystem class.
   * The entire URI is passed to the FileSystem instance's initialize method.
   */
  public static FileSystem get(URI uri, Configuration conf) throws IOException {
    String scheme = uri.getScheme();
    String authority = uri.getAuthority();

    if (scheme == null && authority == null) {     // use default FS
      return get(conf);
    }

    if (scheme != null && authority == null) {     // no authority
      URI defaultUri = getDefaultUri(conf);
      if (scheme.equals(defaultUri.getScheme())    // if scheme matches default
          && defaultUri.getAuthority() != null) {  // & default has authority
        return get(defaultUri, conf);              // return default
      }
    }
    
    String disableCacheName = String.format("fs.%s.impl.disable.cache", scheme);
    if (conf.getBoolean(disableCacheName, false)) {
      return createFileSystem(uri, conf);
    }

    return CACHE.get(uri, conf);
  }

 FileSystem get(URI uri, Configuration conf) throws IOException{
      Key key = new Key(uri, conf);
      return getInternal(uri, conf, key);
    }

即根据key来判断缓存的,key有4个字段来判断是不是同一个key,schema和authority是取值于uri,而我们不传取得就是fs.defaultFS的配置值。Ugi取得是当前用户,unique直接写了个默认值0。所以这四个字段一致就会取到同一个fs。

key结构如下:

关闭的代码:

相关推荐
Yvonne9781 小时前
创建三个节点
java·大数据
bug404_4 小时前
分布式大语言模型服务引擎vLLM论文解读
人工智能·分布式·语言模型
OJAC近屿智能4 小时前
苹果新品今日发布,AI手机市场竞争加剧,近屿智能专注AI人才培养
大数据·人工智能·ai·智能手机·aigc·近屿智能
lucky_syq5 小时前
Spark算子:大数据处理的魔法棒
大数据·分布式·spark
昔我往昔7 小时前
项目中分库分表的分布式ID如何生成
分布式
m0_748233647 小时前
【分布式】Hadoop完全分布式的搭建(零基础)
大数据·hadoop·分布式
圣享科技SMARTLIC7 小时前
企业软件合规性管理:构建高效、安全的软件资产生态
大数据·安全·浮动许可证监控·许可证管理·浮动许可证优化·软件资产管理·浮动许可证管理
roman_日积跬步-终至千里7 小时前
【分布式理论14】分布式数据库存储:分表分库、主从复制与数据扩容策略
数据库·分布式
京东零售技术8 小时前
京东广告基于 Apache Doris 的冷热数据分层实践
大数据
D愿你归来仍是少年8 小时前
解决Python升级导致PySpark任务异常方案
大数据·开发语言·python·spark