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

关闭的代码:

相关推荐
covco11 小时前
矩阵管理系统指南:拆解星链引擎的架构设计与全链路落地实践
大数据·人工智能·矩阵
陕西字符11 小时前
2026 西安 豆包获客优化技术深度解析:企来客科技 AI 全域获客系统测评
大数据·人工智能
virtaitech13 小时前
算力浪费与算力饥渴并存,OrionX社区版免费开放能否破解这一困局?
大数据·人工智能·gpu算力
covco13 小时前
分布式架构实战:全平台矩阵管理系统的技术实现与性能优化
分布式·矩阵·架构
咖啡里的茶i13 小时前
在Docker环境中安装Hadoop cluster 实验报告一
hadoop·docker·容器
青春万岁!!14 小时前
hive模型数据异常-作业调度问题
大数据·数据仓库·hive
QYR-分析14 小时前
压力电气转换器行业市场现状与发展前景分析
大数据·人工智能
人机与认知实验室14 小时前
人机协同的命门:权限与信任
大数据·人工智能
QEasyCloud202217 小时前
领星、聚水潭与金蝶云星空三方系统对接技术方案
大数据
江瀚视野17 小时前
DeepWay深向盈利拐点趋近意味着什么?
大数据·人工智能