FileSystem实例化过程
HDFS案例代码
Configuration configuration = new Configuration();
FileSystem fileSystem = FileSystem.get(new URI("hdfs://hadoop000:8020"), configuration); InputStream in = fileSystem.open(new Path(HDFS_PATH+"/hdfsapi/test/log4j.properties"));
OutputStream out = new FileOutputStream(new File("log4j_download.properties"));
IOUtils.copyBytes(in, out, 4096, true); //最后一个参数表示完成拷贝之后关闭输入/出流
FileSystem.java
static final Cache CACHE = new Cache();
public static FileSystem get(URI uri, Configuration conf) throws IOException {
String scheme = uri.getScheme(); //hdfs
String authority = uri.getAuthority(); //hadoop000:8020
return CACHE.get(uri, conf);
}
FileSystem get(URI uri, Configuration conf) throws IOException{
Key key = new Key(uri, conf);
return getInternal(uri, conf, key);
}
private FileSystem getInternal(URI uri, Configuration conf, Key key) throws IOException{
FileSystem fs;
synchronized (this) {
fs = map.get(key);
}
//根据URI取得一个FileSystem实例,如果允许缓存,会中从缓存中取出,否则将调用createFileSystem创建一个新实例
if (fs != null) {
return fs;
}
fs = createFileSystem(uri, conf);
synchronized (this) {
FileSystem oldfs = map.get(key);
... //放入到CACHE中秋
return fs;
}
}
private static FileSystem createFileSystem(URI uri, Configuration conf) throws IOException {
Class<?> clazz = getFileSystemClass(uri.getScheme(), conf); // 返回的是:org.apache.hadoop.hdfs.DistributedFileSystem
FileSystem fs = (FileSystem)ReflectionUtils.newInstance(clazz, conf);
fs.initialize(uri, conf); //初始化DistributedFileSystem
return fs;
}
public static Class<? extends FileSystem> getFileSystemClass(String scheme,Configuration conf) throws IOException {
if (!FILE_SYSTEMS_LOADED) { //文件系统是否被加载过,刚开始时为false
loadFileSystems();
}
Class<? extends FileSystem> clazz = null;
if (conf != null) {
clazz = (Class<? extends FileSystem>) conf.getClass("fs." + scheme + ".impl", null); //fs.hdfs.impl ,此时我们并没有在core-default.xml和core-site.xml中配置该属性
}
if (clazz == null) {
clazz = SERVICE_FILE_SYSTEMS.get(scheme); //class org.apache.hadoop.hdfs.DistributedFileSystem
}
if (clazz == null) {
throw new IOException("No FileSystem for scheme: " + scheme);
}
return clazz;
}
private static void loadFileSystems() {
synchronized (FileSystem.class) {
if (!FILE_SYSTEMS_LOADED) {
ServiceLoader<FileSystem> serviceLoader = ServiceLoader.load(FileSystem.class);
for (FileSystem fs : serviceLoader) {
SERVICE_FILE_SYSTEMS.put(fs.getScheme(), fs.getClass());
}
FILE_SYSTEMS_LOADED = true; //标识为已经从系统中加载过
}
}
}
loadFileSystems后SERVICE_FILE_SYSTEMS存在如下值:
file=class org.apache.hadoop.fs.LocalFileSystem,
ftp=class org.apache.hadoop.fs.ftp.FTPFileSystem,
hdfs=class org.apache.hadoop.hdfs.DistributedFileSystem,
hftp=class org.apache.hadoop.hdfs.web.HftpFileSystem,
webhdfs=class org.apache.hadoop.hdfs.web.WebHdfsFileSystem,
s3n=class org.apache.hadoop.fs.s3native.NativeS3FileSystem,
viewfs=class org.apache.hadoop.fs.viewfs.ViewFileSystem,
swebhdfs=class org.apache.hadoop.hdfs.web.SWebHdfsFileSystem,
har=class org.apache.hadoop.fs.HarFileSystem,
s3=class org.apache.hadoop.fs.s3.S3FileSystem,
hsftp=class org.apache.hadoop.hdfs.web.HsftpFileSystem
DistributedFileSystem.java
DFSClient dfs; //重点属性:客户端与服务端交互操作需要先拿到DFSClient @Override
public void initialize(URI uri, Configuration conf) throws IOException {
super.initialize(uri, conf);
setConf(conf); String host = uri.getHost(); //hadoop000 this.dfs = new DFSClient(uri, conf, statistics);
this.uri = URI.create(uri.getScheme()+"://"+uri.getAuthority());
this.workingDir = getHomeDirectory();
}
DFSClient.java
final ClientProtocol namenode; //重点属性:客户端与NameNode通信的PRC接口
public DFSClient(URI nameNodeUri, ClientProtocol rpcNamenode, Configuration conf, FileSystem.Statistics stats)throws IOException {
NameNodeProxies.ProxyAndInfo<ClientProtocol> proxyInfo = NameNodeProxies.createProxy(conf, nameNodeUri,ClientProtocol.class);
this.dtService = proxyInfo.getDelegationTokenService();
this.namenode = proxyInfo.getProxy(); //org.apache.hadoop.hdfs.protocolPB.ClientNamenodeProtocolTranslatorPB
}
NameNodeProxies.java
public static <T> ProxyAndInfo<T> createProxy(Configuration conf, URI nameNodeUri, Class<T> xface) throws IOException {
Class<FailoverProxyProvider<T>> failoverProxyProviderClass = getFailoverProxyProviderClass(conf, nameNodeUri, xface);
return createNonHAProxy(conf, NameNode.getAddress(nameNodeUri), xface,UserGroupInformation.getCurrentUser(), true);
}
public static <T> ProxyAndInfo<T> createNonHAProxy(Configuration conf, InetSocketAddress nnAddr, Class<T> xface,
UserGroupInformation ugi, boolean withRetries) throws IOException {
Text dtService = SecurityUtil.buildTokenService(nnAddr);
T proxy;
if (xface == ClientProtocol.class) {
proxy = (T) createNNProxyWithClientProtocol(nnAddr, conf, ugi,withRetries);
} ...
return new ProxyAndInfo<T>(proxy, dtService);
}
private static ClientProtocol createNNProxyWithClientProtocol(
InetSocketAddress address, Configuration conf, UserGroupInformation ugi,boolean withRetries) throws IOException {
//Client与NameNode的RPC交互接口
final long version = RPC.getProtocolVersion(ClientNamenodeProtocolPB.class);
ClientNamenodeProtocolPB proxy = RPC.getProtocolProxy(
ClientNamenodeProtocolPB.class, version, address, ugi, conf,
NetUtils.getDefaultSocketFactory(conf),
org.apache.hadoop.ipc.Client.getTimeout(conf), defaultPolicy)
.getProxy();
if (withRetries) {
//使用jdk的动态代理创建实例
proxy = (ClientNamenodeProtocolPB) RetryProxy.create(
ClientNamenodeProtocolPB.class,new DefaultFailoverProxyProvider<ClientNamenodeProtocolPB>(
ClientNamenodeProtocolPB.class, proxy),methodNameToPolicyMap,defaultPolicy);
}
return new ClientNamenodeProtocolTranslatorPB(proxy);
}
RetryProxy.java
public static <T> Object create(Class<T> iface,FailoverProxyProvider<T> proxyProvider, RetryPolicy retryPolicy) {
return Proxy.newProxyInstance(
proxyProvider.getInterface().getClassLoader(),
new Class<?>[] { iface },
new RetryInvocationHandler<T>(proxyProvider, retryPolicy)
);
}
获取FileSystem实例源码分析总结:
1、FileSystem.get通过反射实例化了一个DistributedFileSystem;
2、DistributedFileSystem中new DFSCilent()把他作为自己的成员变量;
3、在DFSClient构造方法里面,调用了createProxy使用RPC机制得到了一个NameNode的代理对象,就可以和NameNode进行通信;
4、整个流程:FileSystem.get()--> DistributedFileSystem.initialize() --> DFSClient(RPC.getProtocolProxy()) --> NameNode的代理。
FileSystem实例化过程的更多相关文章
- Java继承--子类的实例化过程
一个对象的实例化过程: Person p = new Person(); 1,JVM会读取指定的路径下的Person.class文件,并加载进内存,并会先加载Person的父类(如果有直接的父类的情况 ...
- python--对象实例化过程
实例化过程: class luffy_stu: def __init__(self,name,age,sex): self.name = name self.age = age self.sex = ...
- 框架源码系列九:依赖注入DI、三种Bean配置方式的注册和实例化过程
一.依赖注入DI 学习目标1)搞清楚构造参数依赖注入的过程及类2)搞清楚注解方式的属性依赖注入在哪里完成的.学习思路1)思考我们手写时是如何做的2)读 spring 源码对比看它的实现3)Spring ...
- JVM总括四-类加载过程、双亲委派模型、对象实例化过程
JVM总括四-类加载过程.双亲委派模型.对象实例化过程 目录:JVM总括:目录 一. 类加载过程 类加载过程就是将.class文件转化为Class对象,类实例化的过程,(User user = new ...
- 面向对象【day07】:类的实例化过程剖析(三)
本节内容 1.概述 2.类的语法 3.总结 一.概述 之前我们说关于python中的类,都一脸懵逼,都想说,类这么牛逼到底是什么,什么才是类?下面我们就来讲讲,什么是类?它具有哪些特性. 二.类的语法 ...
- java中子类实例化过程中的内存分配
知识点: 子类继承父类之后,实例化子类时,内存中子类是如何分配内存的呢? 下面,自己会结合一个例子,解释一下,一个子类实例化过程中,内存是如何分配的 参考博客:http://www.cnblogs.c ...
- Java子父类中的构造函数实例化过程
其实我们发现子类继承父类操作很简单,如果要是去深入的研究下会发现,实例化过程并非是我们看到的那样,我们就以代码举例来说明: 问大家,以下代码执行会输出什么呢? package com.oop; /** ...
- hdfs的FileSystem实例化
前言 在spark中通过hdfs的java接口并发写文件出现了数据丢失的问题,一顿操作后发现原来是FileSystem的缓存机制.补一课先 FileSystem实例化 FileSystem.get(c ...
- python基础8之类的实例化过程剖析
一.概述 之前我们说关于python中的类,都一脸懵逼,都想说,类这么牛逼到底是什么,什么才是类?下面我们就来讲讲,什么是类?它具有哪些特性. 二.类的语法 2.1 语法 class dog(obje ...
随机推荐
- 如何利用gatling创建一个性能测试例
[原创博文,转载请声明出处] 基于上一篇博文介绍如何源码编译创建自己的gatling可执行工具,今天介绍一下如何基于gatling做性能测试! 由于gatling的测试例脚本是基于scala写的,所以 ...
- java 输入输出项目
package hellohe; import java.util.Scanner; /** * * @author Administrator *1.导入java.util.scanner; *2. ...
- bzoj3521: [Poi2014]Salad Bar
Description 有一个长度为n的字符串,每一位只会是p或j.你需要取出一个子串S(从左到右或从右到左一个一个取出),使得不管是从左往右还是从右往左取,都保证每时每刻已取出的p的个数不小于j的个 ...
- android学习笔记34——ClipDrawable资源
ClipDrawable ClipDrawable代表从其他位图上截取一个“图片片段” 在XML文件中定义ClipDrawable对象使用<clip.../>元素,该元素的语法为: 以上语 ...
- 浅析:setsockopt()改善socket网络程序的健壮性
1. 如果在已经处于 ESTABLISHED状态下的socket(一般由端口号和标志符区分)调用closesocket(一般不会立即关闭而经历TIME_WAIT的过程)后想继续重用该socket:BO ...
- C#委托和事件讲解
委托 和 事件在 .Net Framework中的应用非常广泛,然而,较好地理解委托和事件对很多接触C#时间不长的人来说并不容易.它们就像是一道槛儿,过了这个槛的人,觉得真是太容易了,而没有过去的人每 ...
- Ubuntu下面配置问题
下开启root登陆 sudo passwd root 输入两次新密码 就能在虚拟终端下用 root登陆了,或者 su到root用户了. ctrl + alt +t 弹出终端 ctrl+ alt+ f7 ...
- SVN代码回滚命令之---merge的使用
一.改动还没被提交的情况(未commit) 这种情况下,见有的人的做法是删除work copy中文件,然后重新update,恩,这种做法达到了目的,但不优雅,因为这种事没必要麻烦服务端. 其实一个命令 ...
- spring+springMVC+mybatis的框架项目基础环境搭建
上一个项目在后台用到spring+springMVC+mybatis的框架,先新项目初步需求也已经下来,不出意外的话,应该也是用这个框架组合. 虽然在之前activiti相关的学习中所用到的框架也是这 ...
- Sublime text2用户自定义配置
[{ "keys": ["ctrl+d"], "command": "run_macro_file", "ar ...