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结构如下:

关闭的代码:

相关推荐
晟诺数字人5 分钟前
2026年海外直播变革:数字人如何改变游戏规则
大数据·人工智能·产品运营
惊讶的猫5 分钟前
rabbitmq实践小案例
分布式·rabbitmq
vx_biyesheji00019 分钟前
豆瓣电影推荐系统 | Python Django 协同过滤 Echarts可视化 深度学习 大数据 毕业设计源码
大数据·爬虫·python·深度学习·django·毕业设计·echarts
2501_9436953318 分钟前
高职大数据与会计专业,考CDA证后能转纯数据分析岗吗?
大数据·数据挖掘·数据分析
实时数据34 分钟前
通过大数据的深度分析与精准营销策略,企业能够有效实现精准引流
大数据
禁默1 小时前
打破集群通信“内存墙”:手把手教你用 CANN SHMEM 重构 AIGC 分布式算子
分布式·重构·aigc
子榆.1 小时前
CANN 性能分析与调优实战:使用 msprof 定位瓶颈,榨干硬件每一分算力
大数据·网络·人工智能
新芒1 小时前
暖通行业两位数下滑,未来靠什么赢?
大数据·人工智能
忆~遂愿2 小时前
CANN ATVOSS 算子库深度解析:基于 Ascend C 模板的 Vector 算子子程序化建模与融合优化机制
大数据·人工智能
惊讶的猫2 小时前
rabbitmq初步介绍
分布式·rabbitmq