HDFS Client加载配置过程源码解析

本文详细解析了HDFS客户端配置加载的过程,包括Configuration类的addResource()和set()方法,以及通过hadoop fs -D命令行参数设置配置的方式。通过对源码的深入分析,展示了配置加载的内部实现,涉及资源配置、属性设置和加载流程。
摘要由CSDN通过智能技术生成

目录

  1. addResource()方法

  2. conf.set(“aaa”, “bbb”)

  3. hadoop fs -D

Configuration类是hadoop的配置类,而客户端获取配置最常用的方式,就是Java Configuration类的addResource()方法和set()方法。此外,还可以通过shell加 -D 的方式,获取指定配置项。本文将通过以下代码,深入源码,探究Client的配置加载过程。

本次分析,hadoop版本为3.3.4

@Test
public void test2() throws Exception {
    Configuration conf = new Configuration();
    conf.addResource(new Path("/Users/didi/core-site.xml"));
    conf.addResource(new Path("/Users/didi/hdfs-site.xml"));
    conf.set("aaa", "bbb");
    FsShell shell = new FsShell(conf);
    String[] args = {"-D", "ccc=ddd", "-cat", "hdfs://ns1/input/test1.txt"};
    int res;
    try {
        res = ToolRunner.run(shell, args);
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        shell.close();
    }
    System.exit(res);
}
  1. addResource()方法
    我们从conf.addResource(new Path(“/Users/didi/core-site.xml”));开始分析。

(1). addResource()方法有很多重载的方法:

但是,实际上他们都在做同一件事情:新建resource类把这些配置来源包装起来,然后存在resources里。其中,resource是Configuration的内部类,专门用来标记这些配置项来源;resources则是有序存放resource的ArrayList。addResource()方法如下:

public void addResource(Path file) {
addResourceObject(new Resource(file));
}

private synchronized void addResourceObject(Resource resource) {
resources.add(resource); // add to resources
restrictSystemProps |= resource.isParserRestricted();
loadProps(properties, resources.size() - 1, false);
}
(2). 这里,loadProps()方法将resources中除去本次放置的资源外的其他资源全部进行加载:

private synchronized void loadProps(final Properties props,
final int startIdx, final boolean fullReload) {
if (props != null) {
Map backup =
updatingResource != null
? new ConcurrentHashMap<>(updatingResource) : null;
loadResources(props, resources, startIdx, fullReload, quietmode);
if (overlay != null) {
props.putAll(overlay);
if (backup != null) {
for (Map.Entry item : overlay.entrySet()) {
String key = (String) item.getKey();
String[] source = backup.get(key);
if (source != null) {
updatingResource.put(key, source);
}
}
}
}
}
}
代码有两个作用:为加载的属性注明来源和加载已有资源。由于此处不满足加载条件props != null,因此会跳出代码。接下来的流程里还会调用这个方法,我们还会再来分析,主要是分析loadResources()这个加载资源的方法。

conf.addResource(new Path(“/Users/didi/hdfs-site.xml”));同样的方法,不再赘述。

综上,addResource()将资源加入资源列表,如果之前已加载过资源(properties不为空),则加载除了新加资源外的所有资源。

  1. conf.set(“aaa”, “bbb”)
    这个方法的作用为,将key为aaa,value为bbb的属性加载到properties中去。

其中,properties是Configuration的属性,以key-value形式存放已经加载的配置,如dfs.namenode.rpc-address ——> hadoop01:8020。这里写aaa和bbb,纯粹是为了告诉读者,这个key-value是可以随便写的,但是加载过后能否起到作用,就看内部程序有没有定义相关逻辑了。

(1). 再来看conf.set():

public void set(String name, String value, String source) {
Preconditions.checkArgument(
name != null,
“Property name must not be null”);
Preconditions.checkArgument(
value != null,
“The value of property %s must not be null”, name);
name = name.trim();
DeprecationContext deprecations = deprecationContext.get();
if (deprecations.getDeprecatedKeyMap().isEmpty()) {
getProps();
}
getOverlay().setProperty(name, value);
getProps().setProperty(name, value);
String newSource = (source ==

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值