hadoop源码阅读之FileSystem简记
本文为本人学习hadoop源码过程中所做的笔记,如有问题请大家指出,或者觉得不好,请绕行,谢谢!
fs = FileSystem.newInstance(uri, conf);
这段代码是如何得到FileSystem对象的呢?
由于FileSystem是抽象类,不能直接实例化对象,
因此只能通过其子类来完成实例化。
哪又是如何通过子类实例化的呢?
通过阅读源码,关键代码如下:
public static Class<? extends FileSystem> getFileSystemClass(String scheme,
Configuration conf) throws IOException {
if (!FILE_SYSTEMS_LOADED) {
loadFileSystems();
}
Class<? extends FileSystem> clazz = null;
if (conf != null) {
clazz = (Class<? extends FileSystem>) conf.getClass("fs." + scheme + ".impl", null);
}
if (clazz == null) {
clazz = SERVICE_FILE_SYSTEMS.get(scheme);
}
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;
}
}
}
首先,会判断FileSystem的子类对象集合是否加载,如果没有加载,
那么就通过ServiceLoader.load拿到FileSystem的子类对象集合,继而
把对象集合中的对象相关信息(这里是scheme和对应类的信息)
注意:ServiceLoader是怎么知道加载FileSystem的哪些子类呢,答案就在
hadoop-common-2.6.0.jar包下
META-INF/services/org.apache.hadoop.fs.FileSystem
和
hadoop-hdfs-2.6.0.jar包下
META-INF/services/org.apache.hadoop.fs.FileSystem
即可找到相关类
- spatialhadoop2.3源码阅读(十一) ShapeRecordReader & SpatialRecordReader[Grid Index MapReuce]
- Hadoop源码分析之FileSystem
- [hadoop源码阅读][9]-mapreduce-从wordcount开始
- Eclipse 阅读 Hadoop2.2.0 源码
- hadoop源码阅读之一:MR第二步:Mapper类
- [hadoop源码阅读][4]-org.apache.hadoop.io.compress系列3-使用压缩
- [hadoop源码阅读][0]-初衷和各种资源
- hadoop源码阅读——Mapper.class
- spatialhadoop2.3源码阅读(六) grid 索引生成方法(二)
- [hadoop源码阅读][4]-org.apache.hadoop.io
- 工作记录:win7下配置阅读hadoop源码环境
- [hadoop源码阅读][1]-源码目录结构
- win7上使用eclipse阅读hadoop源码准备
- hadoop源码阅读环境配置
- Hadoop 2.x 源码阅读的准备
- spatialhadoop2.1源码阅读(二) bin/shadoop generate命令
- [hadoop源码阅读][8]-datanode-FSDataset
- Hadoop源码阅读环境搭建
- [hadoop源码阅读][2]-package结构
- [hadoop源码阅读][9]-mapreduce-job提交过程