HDFS声明及构造函数

  1. @InterfaceAudience.Private
  2. @InterfaceStability.Evolving
  3. public class Hdfs extends AbstractFileSystem {
  4.  
  5. DFSClient dfs;
  6. final CryptoCodec factory;
  7. private boolean verifyChecksum = true;
  8.  
  9. static {
  10. HdfsConfiguration.init();
  11. }
  12.  
  13. /**
  14. * This constructor has the signature needed by
  15. * {@link AbstractFileSystem#createFileSystem(URI, Configuration)}
  16. *
  17. * @param theUri which must be that of Hdfs
  18. * @param conf configuration
  19. * @throws IOException
  20. */
  21. Hdfs(final URI theUri, final Configuration conf) throws IOException, URISyntaxException {
  22. super(theUri, HdfsConstants.HDFS_URI_SCHEME, true, NameNode.DEFAULT_PORT);
  23.  
  24. if (!theUri.getScheme().equalsIgnoreCase(HdfsConstants.HDFS_URI_SCHEME)) {
  25. throw new IllegalArgumentException("Passed URI's scheme is not for Hdfs");
  26. }
  27. String host = theUri.getHost();
  28. if (host == null) {
  29. throw new IOException("Incomplete HDFS URI, no host: " + theUri);
  30. }
  31.  
  32. this.dfs = new DFSClient(theUri, conf, getStatistics());
  33. this.factory = CryptoCodec.getInstance(conf);
  34. }

Hdfs继承了AbstractFileSystem这个抽象类,其中有一个静态块,执行HdfsConfiguration的初始化方法,我们先来看下这个方法

  1. /**
  2. * This method is here so that when invoked, HdfsConfiguration is class-loaded if
  3. * it hasn't already been previously loaded. Upon loading the class, the static
  4. * initializer block above will be executed to add the deprecated keys and to add
  5. * the default resources. It is safe for this method to be called multiple times
  6. * as the static initializer block will only get invoked once.
  7. *
  8. * This replaces the previously, dangerous practice of other classes calling
  9. * Configuration.addDefaultResource("hdfs-default.xml") directly without loading
  10. * HdfsConfiguration class first, thereby skipping the key deprecation
  11. */
  12. public static void init() {
  13. }

init是一个空函数,它存在的作用,只是为了类加载,当类加载的时候,静态块中的方法将会执行从而增加过时的Key和Resource。因为它本身是一个空函数,它被反复调用时安全的,因为静态块中的方法只会被调用一次,使用这个方法来替代之前不安全的直接类调用Configuration.addDefaultResource("hdfs-default.xml")方法。所以,他的静态块中肯定会包含这个方法,我们来看下是不是这样.

  1. static {
  2. addDeprecatedKeys();
  3.  
  4. // adds the default resources
  5. Configuration.addDefaultResource("hdfs-default.xml");
  6. Configuration.addDefaultResource("hdfs-site.xml");
  7.  
  8. }

看完这些,回到HDFS的父类AbstractFileSystem

  1. /**
  2. * This class provides an interface for implementors of a Hadoop file system
  3. * (analogous to the VFS of Unix). Applications do not access this class;
  4. * instead they access files across all file systems using {@link FileContext}.
  5. *
  6. * Pathnames passed to AbstractFileSystem can be fully qualified URI that
  7. * matches the "this" file system (ie same scheme and authority)
  8. * or a Slash-relative name that is assumed to be relative
  9. * to the root of the "this" file system .
  10. */
  11. @InterfaceAudience.Public
  12. @InterfaceStability.Evolving /*Evolving for a release,to be changed to Stable */
  13. public abstract class AbstractFileSystem {

AbstractFileSystem提供了一个实现Hadoop 文件系统的接口,应用访问文件通过FileContext而不需要访问这个类,路径名称一旦匹配了这个文件系统,就会视为合法的URI,否则的会认为是该文件系统根目录的相对路径?(这个不太确定)

来看下它的构造函数:

  1. /**
  2. * Constructor to be called by subclasses.
  3. *
  4. * @param uri for this file system.
  5. * @param supportedScheme the scheme supported by the implementor
  6. * @param authorityNeeded if true then theURI must have authority, if false
  7. * then the URI must have null authority.
  8. *
  9. * @throws URISyntaxException <code>uri</code> has syntax error
  10. */
  11. public AbstractFileSystem(final URI uri, final String supportedScheme,
  12. final boolean authorityNeeded, final int defaultPort)
  13. throws URISyntaxException {
  14. myUri = getUri(uri, supportedScheme, authorityNeeded, defaultPort);
  15. statistics = getStatistics(uri);
  16. }
  1. /**
  2. * Get the URI for the file system based on the given URI. The path, query
  3. * part of the given URI is stripped out and default file system port is used
  4. * to form the URI.
  5. *
  6. * @param uri FileSystem URI.
  7. * @param authorityNeeded if true authority cannot be null in the URI. If
  8. * false authority must be null.
  9. * @param defaultPort default port to use if port is not specified in the URI.
  10. *
  11. * @return URI of the file system
  12. *
  13. * @throws URISyntaxException <code>uri</code> has syntax error
  14. */
  15. private URI getUri(URI uri, String supportedScheme,
  16. boolean authorityNeeded, int defaultPort) throws URISyntaxException {
  17. checkScheme(uri, supportedScheme);
  18. // A file system implementation that requires authority must always
  19. // specify default port
  20. if (defaultPort < 0 && authorityNeeded) {
  21. throw new HadoopIllegalArgumentException(
  22. "FileSystem implementation error - default port " + defaultPort
  23. + " is not valid");
  24. }
  25. String authority = uri.getAuthority();
  26. if (authority == null) {
  27. if (authorityNeeded) {
  28. throw new HadoopIllegalArgumentException("Uri without authority: " + uri);
  29. } else {
  30. return new URI(supportedScheme + ":///");
  31. }
  32. }
  33. // authority is non null - AuthorityNeeded may be true or false.
  34. int port = uri.getPort();
  35. port = (port == -1 ? defaultPort : port);
  36. if (port == -1) { // no port supplied and default port is not specified
  37. return new URI(supportedScheme, authority, "/", null);
  38. }
  39. return new URI(supportedScheme + "://" + uri.getHost() + ":" + port);
  40. }

getUri主要是通过给定的URI生成FS的URI

Hadoop源码阅读-HDFS-day1的更多相关文章

  1. Mac搭建Hadoop源码阅读环境

    1.本次Hadoop源码阅读环境使用的阅读工具是idea,Hadoop版本是2.7.3.需要安装的工具包括idea.jdk.maven.protobuf等 2.jdk,使用的版本是1.8版,在jdk官 ...

  2. Hadoop源码阅读环境搭建(IDEA)

    拿到一份Hadoop源码之后,经常关注的两件事情就是 1.怎么阅读?涉及IDEA和Eclipse工程搭建.IDEA搭建,选择源码,逐步导入即可:Eclipse可以选择后台生成工程,也可以选择IDE导入 ...

  3. 【深入浅出 Yarn 架构与实现】1-2 搭建 Hadoop 源码阅读环境

    本文将介绍如何使用 idea 搭建 Hadoop 源码阅读环境.(默认已安装好 Java.Maven 环境) 一.搭建源码阅读环境 一)idea 导入 hadoop 工程 从 github 上拉取代码 ...

  4. 详细讲解Hadoop源码阅读工程(以hadoop-2.6.0-src.tar.gz和hadoop-2.6.0-cdh5.4.5-src.tar.gz为代表)

    首先,说的是,本人到现在为止,已经玩过.                   对于,这样的软件,博友,可以去看我博客的相关博文.在此,不一一赘述! Eclipse *版本 Eclipse *下载 Jd ...

  5. IntelliJ IDEA 配置 Hadoop 源码阅读环境

    1.下载安装IDEA https://www.jetbrains.com/idea/download/#section=windows 2.下载hadoop源码 https://archive.apa ...

  6. hadoop源码阅读

    1.Hadoop的包的功能分析 2.由于Hadoop的MapReduce和HDFS都有通信的需求,需要对通信的对象进行序列化.Hadoop并没有采用java的序列化,而是引入它自己的系统.org.ap ...

  7. Apache Hadoop 源码阅读

    总之一句话,这些都是hadoop-2.2.0的源代码里有的.也就是不光只是懂理论,编程最重要,还是基本功要扎实啊.... 在hadoop-2.2.0的源码里,按Ctrl + Shift + T . 跳 ...

  8. Apache Hadoop 源码阅读(陆续更新)

    不多说,直接上干货! 总之一句话,这些都是hadoop-2.2.0的源代码里有的.也就是不光只是懂理论,编程最重要,还是基本功要扎实啊.... 在hadoop-2.2.0的源码里,按Ctrl + Sh ...

  9. Hadoop源码之HDFS(1)--------通信方式

    说起hadoop这个东西,只能说真是个伟大的发明,而本人对cutting大神也是无比的崇拜,记得刚接触hadoop的时候,还觉得这个东西挺多余的,但是现在想想,这个想法略傻逼...... 2006-2 ...

  10. Hadoop 源码阅读技巧

    http://www.cnblogs.com/xuxm2007/category/388607.html     个人谈谈阅读hadoop源代码的经验.首先,不得不说,hadoop发展到现在这个阶段, ...

随机推荐

  1. Svn 提示错误:previous operation has not finished 解决方案

    svn提交遇到恶心的问题,可能是因为上次cleanup中断后,进入死循环了. 解决方案: 找到你项目的.svn文件,查看是否存在wc.db 网上下载SQLite Expert工具,手动打开wc.db, ...

  2. 【Tableau】电商广告投放的地域分析

    分析师的职责是利用处理数据获取信息,提炼规律,帮助企业正确决策业务方向. 所以,一个好的分析师绝不能被数据所困,既要深入业务,理解业务,也要高瞻远瞩,以领导者的思维借助数据分析的辅助做出判断. [结构 ...

  3. Hyperledger Fabric的一些密码学常识

    Hash 哈希(Hash)算法主要作用是将一段任意长度的数据,经过计算转换成一段定长的数据. 这种算法的特性是:几乎不能通过Hash的结果推导出原文.并且几乎没有可能找到两个不同的信息,对两个信息进行 ...

  4. 了不起的Node.js--之四

    阻塞与非阻塞IO 绝大多数对node.js的讨论都把关注点放在了其处理高并发的能力上.Node框架给开发者提供了构建高性能网络应用的强大能力. 我使用的开发工具是Mac版的WebStorm,这个工具支 ...

  5. App云测试服务对比

    前言: 我们都知道在测试移动app时最耗时的是在各种测试设备进行测试, 因为不论是安卓还是iOS都已经碎片化了.而云测试看似是解决这一问题的有效途径.因此选择哪种云测试平台来协助测试人员进行各种测试就 ...

  6. Redis学习笔记之入门基础知识——简介

    非关系型数据库,存储的数据类型:字符串(STRING).列表(LIST).集合(SET).散列表(HASH).有序集合(ZSET) 持久化:时间点转储(point-in-time-dump)(快照). ...

  7. 调研ios开发环境的演变

    一:ios的发展演变: 以下两句为百度百科IOS,可自行查阅,不多赘述,就Ctrl+c,Ctrl+v两句表示一下. 2007年1月9日苹果公司在Macworld展览会上公布,随后于同年的6月发布第一版 ...

  8. 实现文字左右滚动 javascript

    参考链接:http://www.86y.org/art_detail.aspx?id=587 代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1 ...

  9. OpenFlow PacketOut消息机制

    OpenFlow PacketOut消息机制 前言 由于最近实验的进行,遇到一个比较棘手的问题,就是利用控制器主动发送packet消息的问题,期间遇到一些问题,后来在RYU群中得到群友左木的帮助成功解 ...

  10. Spring源码解析二:IOC容器初始化过程详解

    IOC容器初始化分为三个步骤,分别是: 1.Resource定位,即BeanDefinition的资源定位. 2.BeanDefinition的载入 3.向IOC容器注册BeanDefinition ...