FileSystem及其源碼分析
FileSystem這個抽象類提供了豐富的方法用于對文件系統(tǒng)的操作,包括上傳、下載、刪除衙耕、創(chuàng)建等。這里多說的文件系統(tǒng)通常指的是HDFS(DistributedFileSystem)勺远,其實橙喘,hadoop處理支持分布式文件系統(tǒng),還提供了對諸如本地文件系統(tǒng)(LocalFileSystem)胶逢、FTP文件系統(tǒng)(FTPFIle)的支持厅瞎。
在這里我們主要介紹一下DistributedFileSystem的創(chuàng)建過程。如下代碼:
主要包括兩個階段:
1. 加載配置文件
2. 初始化文件系統(tǒng)
Configuration conf = new Configuration();//加載配置文件
FileSystem fs = FileSystem.get(conf);//初始化文件系統(tǒng)
首先來看一下配置文件加載階段初坠。
這是Configuration類的靜態(tài)代碼塊和簸,默認加載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");
}
接下來進入到初始化文件系統(tǒng)階段:
FileSystem的get(Configuration conf)方法調用了它的另一個方法get(getDefaultUri(conf),conf)碟刺,這個方法通過判斷是否采用了緩存機制锁保,如果采用了緩存機制,則從緩存中獲取半沽,如果沒有采用緩存機制爽柒,則創(chuàng)建新的文件系統(tǒng),默認開啟緩存機制者填。
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 :默認為本地文件系統(tǒng) file:///
return get(conf);
}
//省略部分代碼
//判斷是否緩存FileSystem
String disableCacheName = String.format("fs.%s.impl.disable.cache", scheme);
//如果不采用緩存機制浩村,每次都創(chuàng)建新的FileSystem
if (conf.getBoolean(disableCacheName, false)) {
return createFileSystem(uri, conf);
}
//如果采用緩存機制,則從CACHE中獲取
return CACHE.get(uri, conf);
先讓我們來看一下這個CACHE到底是個什么東西幔托?
CACHE是FileSystem的一個靜態(tài)內部類穴亏,內部維護一個HashMap<Key,FileSystem>(FileSystem容器)蜂挪,鍵為Key類型重挑,Key是CACHE的一個靜態(tài)內部類,內部封裝了Schema(協(xié)議棠涮,這里指hdfs)谬哀、Authority(權限主機,這里指s101)严肪,Vaule就是緩存的文件系統(tǒng)史煎。
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);
}
下面我們來看一下 createFileSystem(uri, conf)是如何創(chuàng)建FileSystem的:
private static FileSystem createFileSystem(URI uri, Configuration conf
) throws IOException {
//根據(jù)conf和Schema獲取對應的FileSystemClass谦屑,這里指的是DistributedFileSystem.class
Class<?> clazz = getFileSystemClass(uri.getScheme(), conf);
//通過反射創(chuàng)建文件系統(tǒng)
FileSystem fs = (FileSystem)ReflectionUtils.newInstance(clazz, conf);
//初始化文件系統(tǒng)
fs.initialize(uri, conf);
return fs;
}
再來看一下文件系統(tǒng)的initialize()方法做了些什么,最主要的就是創(chuàng)建了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");
//創(chuàng)建DFS客戶端(每個文件系統(tǒng)都持有一個dfs客戶端對象)
this.dfs = new DFSClient(uri, conf, this.statistics);
this.uri = URI.create(uri.getScheme() + "://" + uri.getAuthority());
//工作空間
this.workingDir = this.getHomeDirectory();
}
總結
FileSystem 的創(chuàng)建過程:
- 首先加載配置文件悍手,主要是獲得fs.defaultFS的屬性值。
- 創(chuàng)建文件系統(tǒng):
首先從CACHE.map緩存中獲得相應的文件系統(tǒng)袍患。
如果是第一次創(chuàng)建該文件系統(tǒng)坦康,加載相應的文件系統(tǒng)的Class對象,通過反射創(chuàng)建文件系統(tǒng)對象诡延,然后調用initialize()方法對初始化
并存入CACHE.map中滞欠。
skeleton
- fs.create()
- fs.open()
- fs.getFileStatus()
- fs.getFileBlockLocation()