hadoop2.7.3版本FileSystem调用过程(1)

之前接触过一段时间的HDFS,但是没有深入了解原理,今天有时间了整理一下。


FileSystem简介

FileSystem是hadoop定义的抽象的文件系统,而hdfs只是其中实现的文件系统之一。FileSystem提供了很多操作文件和文件夹的方法,如创建文件,删除文件,重命名,获取文件夹下面的子文件及判断是否是目录等。此外还提供了读取文件,追加内容的功能,但是hdfs暂时不支持追加功能。

FileSystem使用

使用FileSystem.get方法获取FileSystem对象.

第一个方法是用过Configuration对象获取,会使用conf中设置的fs.defaultFS值作为URI,如果没有指定就会使用本地文件。

public static FileSystem get(Configuration conf) throws IOException {
    return get(getDefaultUri(conf), conf);
}
public static URI getDefaultUri(Configuration conf) {
   return URI.create(fixName(conf.get("fs.defaultFS", "file:///")));
}

第二个重写方法是通过URI和Configuration对象获取,该方法首先获取uri的scheme和authority属性
,如果都为空,调用第一个方法,如果只有authority 为空会检验conf中fs.defaultFS属性的scheme与scheme是否一致,一致的情况下使用fs.defaultFS值。conf中fs.scheme.impl.disable.cache属性是禁用文件系统缓存,默认为false,如果禁用缓存每次都会重新初始化FileSystem对象,否则调用CACHE.get(uri, conf)获取FileSystem对象。

注意:禁用缓存后生成的FileSystem需要手动关闭

    public static FileSystem get(URI uri, Configuration conf) {
    String scheme = uri.getScheme();//协议
    String authority = uri.getAuthority();//host:port

    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);
  }

createFileSystem方法里面就是根据uri的scheme属性获取需要的class对象,然后通过反射进行初始化。

private static FileSystem createFileSystem(URI uri, Configuration conf
      ) throws IOException {
    Class<?> clazz = getFileSystemClass(uri.getScheme(), conf);
    FileSystem fs = (FileSystem)ReflectionUtils.newInstance(clazz, conf);
    fs.initialize(uri, conf);
    return fs;
}

getFileSystemClass方法首先判断是否已经调用了loadFileSystems方法,如果已经调用过,首先会从conf对象中获取fs.scheme .impl属性,没有获取到就会从SERVICE_FILE_SYSTEMS缓存中查找。

public static Class<? extends FileSystem> getFileSystemClass(String scheme,
      Configuration conf) throws IOException {
     
    if (!FILE_SYSTEMS_LOADED) {
      loadFileSystems();
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值