FileSystem及其源码分析

  FileSystem这个抽象类提供了丰富的方法用于对文件系统的操作,包括上传、下载、删除、创建等。这里多说的文件系统通常指的是HDFS(DistributedFileSystem),其实,hadoop处理支持分布式文件系统,还提供了对诸如本地文件系统(LocalFileSystem)、FTP文件系统(FTPFIle)的支持。

  在这里我们主要介绍一下DistributedFileSystem的创建过程。如下代码:

  主要包括两个阶段:

    1. 加载配置文件

    2. 初始化文件系统

Configuration conf = new Configuration();//加载配置文件
FileSystem fs = FileSystem.get(conf);//初始化文件系统

  首先来看一下配置文件加载阶段。

  这是Configuration类的静态代码块,默认加载core-default.xml和core-site.xml这两个配置文件。

static{
//print deprecation warning if hadoop-site.xml is found in classpath
ClassLoader cL = Thread.currentThread().getContextClassLoader();
if (cL == null) {
cL = Configuration.class.getClassLoader();
}
if(cL.getResource("hadoop-site.xml")!=null) {//确保在类路径下不存在hadoop-site.xml(已过时)
LOG.warn("DEPRECATED: hadoop-site.xml found in the classpath. " +
"Usage of hadoop-site.xml is deprecated. Instead use core-site.xml, "
+ "mapred-site.xml and hdfs-site.xml to override properties of " +
"core-default.xml, mapred-default.xml and hdfs-default.xml " +
"respectively");
}
addDefaultResource("core-default.xml");
addDefaultResource("core-site.xml");
}

  接下来进入到初始化文件系统阶段:

  FileSystem的get(Configuration conf)方法调用了它的另一个方法get(getDefaultUri(conf),conf),这个方法通过判断是否采用了缓存机制,如果采用了缓存机制,则从缓存中获取,如果没有采用缓存机制,则创建新的文件系统,默认开启缓存机制。

 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_DEFAULT_NAME_KEY, DEFAULT_FS)));//通过conf中的fs.defaultFS属性获得URI(hdfs://s101)
}
public static FileSystem get(URI uri, Configuration conf) throws IOException {
String scheme = uri.getScheme();//hdfs
String authority = uri.getAuthority();//s101 if (scheme == null && authority == null) { // use default FS :默认为本地文件系统 file:///
return get(conf);
}

  //省略部分代码
  //判断是否缓存FileSystem
  String disableCacheName = String.format("fs.%s.impl.disable.cache", scheme);
  //如果不采用缓存机制,每次都创建新的FileSystem
if (conf.getBoolean(disableCacheName, false)) {
return createFileSystem(uri, conf);
}
  //如果采用缓存机制,则从CACHE中获取
return CACHE.get(uri, conf);

  先让我们来看一下这个CACHE到底是个什么东西?

  CACHE是FileSystem的一个静态内部类,内部维护一个HashMap<Key,FileSystem>(FileSystem容器),键为Key类型,Key是CACHE的一个静态内部类,内部封装了Schema(协议,这里指hdfs)、Authority(权限主机,这里指s101),Vaule就是缓存的文件系统。

 static class Cache {
  //省略......
private final Map<Key, FileSystem> map = new HashMap<Key, FileSystem>();//FileSystem容器 /** FileSystem.Cache.Key */
static class Key {
final String scheme;//hdfs
final String authority;//s101
final UserGroupInformation ugi;
//省略...
}
}

  CACHE.get(uri,conf)方法用于获得具体的FileSystem

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

  调用getInterval(uri,conf.key)方法:该方法通过createFileSystem创建新的文件系统,并将其存入缓存容器map中。

private FileSystem getInternal(URI uri, Configuration conf, Key key) throws IOException{
FileSystem fs;
synchronized (this) {
fs = map.get(key);//由于此时为一次创建FileSystem,所以此时map为null
}
if (fs != null) {
return fs;
}
//fs不为null,创建文件系统
fs = createFileSystem(uri, conf); synchronized (this) { // refetch the lock again
FileSystem oldfs = map.get(key);
if (oldfs != null) { // a file system is created while lock is releasing
fs.close(); // close the new file system
return oldfs; // return the old file system
} // now insert the new file system into the map
if (map.isEmpty()
&& !ShutdownHookManager.get().isShutdownInProgress()) {
ShutdownHookManager.get().addShutdownHook(clientFinalizer, SHUTDOWN_HOOK_PRIORITY);
}
fs.key = key;
//将文件系统存入map容器
map.put(key, fs);
if (conf.getBoolean("fs.automatic.close", true)) {
toAutoClose.add(key);
}
return fs;
}
}

下面我们来看一下 createFileSystem(uri, conf)是如何创建FileSystem的:

private static FileSystem createFileSystem(URI uri, Configuration conf
) throws IOException {
//根据conf和Schema获取对应的FileSystemClass,这里指的是DistributedFileSystem.class
Class<?> clazz = getFileSystemClass(uri.getScheme(), conf);
//通过反射创建文件系统
FileSystem fs = (FileSystem)ReflectionUtils.newInstance(clazz, conf);
//初始化文件系统
fs.initialize(uri, conf);
return fs;
}

简单了解一下getFileSystemClass的获取过程:加载

public static Class<? extends FileSystem> getFileSystemClass(String scheme,
Configuration conf) throws IOException {
if (!FILE_SYSTEMS_LOADED) {
loadFileSystems();//加载文件系统,加载了hadoop支持的9种文件系统,存放到了SERVICE_FILE_SYSTEMS=new HashMap<String,Class<? extends FileSystem>>
}
    Class<? extends FileSystem> clazz = null;
//省略
if (clazz == null) {//根据schema获得对应的文件系统的clazz
clazz = SERVICE_FILE_SYSTEMS.get(scheme);
}
if (clazz == null) {
throw new IOException("No FileSystem for scheme: " + scheme);
}
return clazz;
}

再来看一下文件系统的initialize()方法做了些什么,最主要的就是创建了DFS客户端对象,是一个DFSClient,它负责与namenode进行远程通信,是一个绝对重要的家伙。

  public void initialize(URI uri, Configuration conf) throws IOException {
super.initialize(uri, conf);
this.setConf(conf);
String host = uri.getHost();
if (host == null) {
throw new IOException("Incomplete HDFS URI, no host: " + uri);
} else {
this.homeDirPrefix = conf.get("dfs.user.home.dir.prefix", "/user");
//创建DFS客户端(每个文件系统都持有一个dfs客户端对象)
this.dfs = new DFSClient(uri, conf, this.statistics);
this.uri = URI.create(uri.getScheme() + "://" + uri.getAuthority());
//工作空间
this.workingDir = this.getHomeDirectory();
}

到此为止,FileSystem的创建过程就完成了,下面做一下总结。

FileSystem 的创建过程:

  1. 首先加载配置文件,主要是获得fs.defaultFS的属性值。

  2. 创建文件系统:

    首先从CACHE.map缓存中获得相应的文件系统。

    如果是第一次创建该文件系统,加载相应的文件系统的Class对象,通过反射创建文件系统对象,然后调用initialize()方法对初始化

    并存入CACHE.map中。

hadoop之hdfs------------------FileSystem及其源码分析的更多相关文章

  1. Qt QComboBox之setEditable和currentTextChanged及其源码分析

    目录 Qt QComboBox之setEditable和currentTextChanged以及其源码分析 前言 问题的出现 问题分析 currentTextChanged信号触发 源码分析 Qt Q ...

  2. 大数据学习笔记——HDFS写入过程源码分析(1)

    HDFS写入过程方法调用逻辑 & 源码注释解读 前一篇介绍HDFS模块的博客中,我们重点从实践角度介绍了各种API如何使用以及IDEA的基本安装和配置步骤,而从这一篇开始,将会正式整理HDFS ...

  3. 大数据学习笔记——HDFS写入过程源码分析(2)

    HDFS写入过程注释解读 & 源码分析 此篇博客承接上一篇未讲完的内容,将会着重分析一下在Namenode获取到元数据后,具体是如何向datanode节点写入真实的数据的 1. 框架图展示 在 ...

  4. 8.深入k8s:资源控制Qos和eviction及其源码分析

    转载请声明出处哦~,本篇文章发布于luozhiyun的博客:https://www.luozhiyun.com,源码版本是1.19 又是一个周末,可以愉快的坐下来静静的品味一段源码,这一篇涉及到资源的 ...

  5. pdfmake.js使用及其源码分析

    公司项目在需要将页面的文本导出成DPF,和支持打印时,一直没有做过这样的功能,花了一点时间将其做了出来,并且本着开源的思想和技术分享的目的,将自己的编码经验分享给大家,希望对大家有用. 现在是有一个文 ...

  6. 9.深入k8s:调度器及其源码分析

    转载请声明出处哦~,本篇文章发布于luozhiyun的博客:https://www.luozhiyun.com 源码版本是1.19 这次讲解的是k8s的调度器部分的代码,相对来说比较复杂,慢慢的梳理清 ...

  7. Golang的Context介绍及其源码分析

    简介 在Go服务中,对于每个请求,都会起一个协程去处理.在处理协程中,也会起很多协程去访问资源,比如数据库,比如RPC,这些协程还需要访问请求维度的一些信息比如说请求方的身份,授权信息等等.当一个请求 ...

  8. 13.深入k8s:Pod 水平自动扩缩HPA及其源码分析

    转载请声明出处哦~,本篇文章发布于luozhiyun的博客:https://www.luozhiyun.com 源码版本是1.19 Pod 水平自动扩缩 Pod 水平自动扩缩工作原理 Pod 水平自动 ...

  9. 14.深入k8s:kube-proxy ipvs及其源码分析

    转载请声明出处哦~,本篇文章发布于luozhiyun的博客:https://www.luozhiyun.com 源码版本是1.19 这一篇是讲service,但是基础使用以及基本概念由于官方实在是写的 ...

随机推荐

  1. python format 用法详解

    format 用法详解 不需要理会数据类型的问题,在%方法中%s只能替代字符串类型 单个参数可以多次输出,参数顺序可以不相同 填充方式十分灵活,对齐方式十分强大 官方推荐用的方式,%方式将会在后面的版 ...

  2. Alpha版(内部测试版)发布

    首先通过微信扫吗下载我们的软件校园服务,首先进去登录界面没账号点击注册,注册完就可以登录了,进去界面我们在二手交易这项功能里我们即可以事卖家又可以是买家如果我们卖东西点击商品出售,填写商品信息,商品图 ...

  3. CSS效果常见问题

    详细解答参见上篇博客 问题1.如何用 div 画一个 xxx box-shadow 无限投影 (堆叠成复杂图案) ::before ::after 问题2.如何产生不占空间的边框 1.box-shad ...

  4. UVa 1452 递推 Jump

    约瑟夫变形,先计算出3个数时,最后三个数字的编号. 然后以这三个数为起点,就可以递推出n个数对应的最后三个数字的编号. 递推公式都是一样的. #include <iostream> #in ...

  5. An entity cannot be annotated with both @Entity and @MappedSuperclass: com.example1.demo1.Entity.User错误

    项目问SpringDataJpa项目,在运行的过程中出现的以下错误: Caused by: org.hibernate.AnnotationException: An entity cannot be ...

  6. dubbo与zk注册中心如何对接,如何做到服务自动发现

    先看下consumer端发起调用时的链路流程: +---------------------------+ +---------------------------+ +--------------- ...

  7. MyCAT+MySQL 搭建高可用企业级数据库集群——第3章 MyCat核心配置讲解

    3-1 章节综述 3-2 常用配置文件间的关系 3-3 server.xml配置详解 3-4 log4j2.xml配置文件 3-5 rule.xml 3-6 常用分片算法(上) 3-7 常用分片算法( ...

  8. GCC内嵌汇编一些限制字符串

    /******************/ “b”将输入变量放入ebx “c”将输入变量放入ecx “d”将输入变量放入edx “s”将输入变量放入esi “d”将输入变量放入edi “q”将输入变量放 ...

  9. set(集合)类

    1.set(集合)和 map(映射) 都属于关联容器,它们都支持查询一个元素是否存在,并能有效地获取元素.实现了红黑树的平衡二叉检索树的数据结构,插入元素时,它会自动调整二叉树的排列,把元素放到适当的 ...

  10. curl 请求

    一.Linux curl用法举例: . linux curl抓取网页: 抓取百度: curl http://www.baidu.com curl http://www.baidu.com 如发现乱码, ...