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

关闭的代码:

相关推荐
大大大大晴天3 天前
Hudi技术内幕:RecordPayload到RecordMerger
大数据
SelectDB3 天前
秒级弹性、最高降本 70%:SelectDB Serverless 如何重塑云数仓资源效率
大数据·后端·云原生
WhoAmI3 天前
MapReduce框架原理解析一:InputFormat
大数据·hadoop
WhoAmI3 天前
MapReduce框架原理解析三:OutputFormat
大数据·hadoop
WhoAmI3 天前
MapReduce框架原理解析二:Shuffle
大数据·hadoop
大大大大晴天4 天前
Hudi技术内幕:Key Generation原理与实践
大数据
得物技术7 天前
从埋点需求到规则资产:Hermes Agent 重构得物数仓工作流
大数据·llm·ai编程
久美子8 天前
AI驱动数仓建设的Harness工程实践——本体建模、知识分层与上下文工程
大数据
大树888 天前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
大志哥1238 天前
ES和Logstash日志链路系统上线后遭遇切片爆炸(解决)
大数据·elasticsearch