HDFS声明及构造函数

 @InterfaceAudience.Private
@InterfaceStability.Evolving
public class Hdfs extends AbstractFileSystem { DFSClient dfs;
final CryptoCodec factory;
private boolean verifyChecksum = true; static {
HdfsConfiguration.init();
} /**
* This constructor has the signature needed by
* {@link AbstractFileSystem#createFileSystem(URI, Configuration)}
*
* @param theUri which must be that of Hdfs
* @param conf configuration
* @throws IOException
*/
Hdfs(final URI theUri, final Configuration conf) throws IOException, URISyntaxException {
super(theUri, HdfsConstants.HDFS_URI_SCHEME, true, NameNode.DEFAULT_PORT); if (!theUri.getScheme().equalsIgnoreCase(HdfsConstants.HDFS_URI_SCHEME)) {
throw new IllegalArgumentException("Passed URI's scheme is not for Hdfs");
}
String host = theUri.getHost();
if (host == null) {
throw new IOException("Incomplete HDFS URI, no host: " + theUri);
} this.dfs = new DFSClient(theUri, conf, getStatistics());
this.factory = CryptoCodec.getInstance(conf);
}

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

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

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

static {
addDeprecatedKeys(); // adds the default resources
Configuration.addDefaultResource("hdfs-default.xml");
Configuration.addDefaultResource("hdfs-site.xml"); }

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

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

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

来看下它的构造函数:

/**
* Constructor to be called by subclasses.
*
* @param uri for this file system.
* @param supportedScheme the scheme supported by the implementor
* @param authorityNeeded if true then theURI must have authority, if false
* then the URI must have null authority.
*
* @throws URISyntaxException <code>uri</code> has syntax error
*/
public AbstractFileSystem(final URI uri, final String supportedScheme,
final boolean authorityNeeded, final int defaultPort)
throws URISyntaxException {
myUri = getUri(uri, supportedScheme, authorityNeeded, defaultPort);
statistics = getStatistics(uri);
}
/**
* Get the URI for the file system based on the given URI. The path, query
* part of the given URI is stripped out and default file system port is used
* to form the URI.
*
* @param uri FileSystem URI.
* @param authorityNeeded if true authority cannot be null in the URI. If
* false authority must be null.
* @param defaultPort default port to use if port is not specified in the URI.
*
* @return URI of the file system
*
* @throws URISyntaxException <code>uri</code> has syntax error
*/
private URI getUri(URI uri, String supportedScheme,
boolean authorityNeeded, int defaultPort) throws URISyntaxException {
checkScheme(uri, supportedScheme);
// A file system implementation that requires authority must always
// specify default port
if (defaultPort < 0 && authorityNeeded) {
throw new HadoopIllegalArgumentException(
"FileSystem implementation error - default port " + defaultPort
+ " is not valid");
}
String authority = uri.getAuthority();
if (authority == null) {
if (authorityNeeded) {
throw new HadoopIllegalArgumentException("Uri without authority: " + uri);
} else {
return new URI(supportedScheme + ":///");
}
}
// authority is non null - AuthorityNeeded may be true or false.
int port = uri.getPort();
port = (port == -1 ? defaultPort : port);
if (port == -1) { // no port supplied and default port is not specified
return new URI(supportedScheme, authority, "/", null);
}
return new URI(supportedScheme + "://" + uri.getHost() + ":" + port);
}

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. Spring Boot (十四): Spring Boot 整合 Shiro-登录认证和权限管理

    这篇文章我们来学习如何使用 Spring Boot 集成 Apache Shiro .安全应该是互联网公司的一道生命线,几乎任何的公司都会涉及到这方面的需求.在 Java 领域一般有 Spring S ...

  2. Scrapy的日志等级和请求传参

    日志等级 日志信息:   使用命令:scrapy crawl 爬虫文件 运行程序时,在终端输出的就是日志信息: 日志信息的种类: ERROR:一般错误: WARNING:警告: INFO:一般的信息: ...

  3. 转 ssh-keygen 的 详解

    为了让两个linux机器之间使用ssh不需要用户名和密码.所以采用了数字签名RSA或者DSA来完成这个操作. 模型分析 假设 A (192.168.20.59)为客户机器,B(192.168.20.6 ...

  4. 服装盘点机PDA在服装行业颜色尺码仓库条码高效管理应用

    服装行业的商品管理的特点是需要管理颜色和尺码 具体逻辑就是: 什么商品,什么颜色,什么尺码,入库多少个? 什么商品,什么颜色,什么尺码,出库多少个? 什么商品,什么颜色,什么尺码,还有库存多少个? 如 ...

  5. js闭包的作用

    js闭包的用途详解 js闭包可以用在许多地方.它的最大用处有两个,一个是前面提到的可以读取函数内部的变量,另一个就是让这些变量的值始终保持在内存中.具体怎么理解呢,各位看官请仔细看好下文   我们来看 ...

  6. wpf-典型的mvvm模式通用中小型管理系统框架0

    之前就一直在想着写这么一系列博客,将前段时间(也算有点久了)自己编写的一套框架分享下,和园子里的诸位大牛交流交流,奈何文思枯竭,提键盘而无从敲起,看来只有coding时才不会有这种裤子都脱了,才发现对 ...

  7. 使用jQuery的分页插件jquery.pagination.js进行分页

    1,需要用到jquery.pagination.js和pagination.css https://pan.baidu.com/s/1G3PLQSRGjvLxl2ryqpV76w https://pa ...

  8. Notes of Daily Scrum Meeting(12.16)

    最近好几门课的大作业都到了要截止的时候了,好多天队员们都抽不出来时间做软工的项目了,这样确实 和我们的计划出入很大,不过希望老师谅解,三门课程设计确实压力很大. 今天的团队任务总结如下: 团队成员 今 ...

  9. 【Alpha】第九次Scrum meeting

    今日任务一览: 姓名 今日完成任务 所耗时间 刘乾 由于发现之前版本的模板引擎存在致命bug,所以重新更换,现在使用jinja2作为模板渲染引擎. 3 鲁聃 卤蛋今天为了给编译老师做视频,没有做软工项 ...

  10. DHCP全局配置文件解析

    作用 参数 ddns-update-style  类型 定义DNS服务动态更新的类型,类型包括:none(不支持动态更新), interim (互动更新模式)与ad-hoc(特殊更新模式) allow ...