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

最新文章

  1. Singleton(单例模式)的一种实现 -- 基于【惰性】适用于【大对象】的一种生产实践
  2. C#中的BackgroundWorker控件+Delegate.Invoke (委托同步调用)
  3. MySQL(六) —— 运算符和函数
  4. 图片轮播jQuery
  5. android 控件自定义样式
  6. Scala 简介
  7. 手把手教你树莓派实现简易室内监控系统(C)之BOA服务器的搭建
  8. Django之分页
  9. Arcgis API for JS——普通分屏联动及二三维联动
  10. redis scan删除key的方法封装
  11. 洛谷P4591 [TJOI2018]碱基序列(hash dp)
  12. Python知识梳理
  13. Hibernate的集合一对多与多对一
  14. Nginx如何启用ETag,提高访问速度
  15. Delphi应用程序的调试(十)调试器选项
  16. 转 C#高性能Socket服务器SocketAsyncEventArgs的实现(IOCP)
  17. 动态规划:POJ No 2385 Apple Catching
  18. Mac新建文件夹、txt文件、其他格式文件
  19. SD卡WAV音乐播放器(quartus11.0)(FAT32)(DE2-115)
  20. 20155334 2016-2017-2 《Java程序设计》第七周学习总结

热门文章

  1. .Net Core 分布式微服务框架 - Jimu 添加 Swagger 支持
  2. HTML 列表实例
  3. harbor使用和管理
  4. Git 命令简单罗列
  5. oozie 编译与安装
  6. Daily Scrum NO.6
  7. 开源通用爬虫框架YayCrawler-运行与调试
  8. 【python】自学笔记
  9. [51CTO]新说MySQL事务隔离级别!
  10. Git从零开始(一)