kylin配置文件获取
public class KylinConfig extends KylinConfigBase KylinConfig继承自KylinConfigBase
public static KylinConfig getInstanceFromEnv() {
//同步锁
synchronized (KylinConfig.class) {
//获取本地实例的config
KylinConfig config = THREAD_ENV_INSTANCE.get();
//判断实例中的config是否存在,如果存在则直接返回config
if (config != null) {
return config;
}
//如果不存在则读本地配置文件
if (SYS_ENV_INSTANCE == null) {
try {
config = new KylinConfig();
config.reloadKylinConfig(buildSiteProperties());
logger.info("Initialized a new KylinConfig from getInstanceFromEnv : "
+ System.identityHashCode(config));
SYS_ENV_INSTANCE = config;
} catch (IllegalArgumentException e) {
throw new IllegalStateException("Failed to find KylinConfig ", e);
}
}
return SYS_ENV_INSTANCE;
}
}
getInstanceFromEnv方法的具体实现如下:首先获取从本地实例的THREAD_ENV_INSTANCE中获取kylinConfig,如果不为null则直接返回实例中config,如果为null,则读取本地配置问题,然后调用kylinConfig自身的bulidSiteProperties()方法返回一个Properties 最后通过config.reloadKylinConfig()方法把配置项加载到config中。赋值给SYS_ENV_INSTANCE 返回。
bulidSiteProperties()方法实现如下:
private static Properties buildSiteProperties() {
Properties conf = new Properties();
OrderedProperties orderedProperties = buildSiteOrderedProps();
for (Map.Entry each : orderedProperties.entrySet()) {
conf.put(each.getKey(), each.getValue());
}
return conf;
}
bulidSiteOrderedProps()方法实现如下:
// build kylin properties from site deployment, a.k.a KYLIN_HOME/conf/kylin.properties
private static OrderedProperties buildSiteOrderedProps() {
try {
// 1. load default configurations from classpath.
// we have a kylin-defaults.properties in kylin/core-common/src/main/resources
URL resource = Thread.currentThread().getContextClassLoader().getResource("kylin-defaults.properties");
Preconditions.checkNotNull(resource);
logger.info("Loading kylin-defaults.properties from {}", resource.getPath());
OrderedProperties orderedProperties = new OrderedProperties();
loadPropertiesFromInputStream(resource.openStream(), orderedProperties);
for (int i = 0; i < 10; i++) {
String fileName = "kylin-defaults" + (i) + ".properties";
URL additionalResource = Thread.currentThread().getContextClassLoader().getResource(fileName);
if (additionalResource != null) {
logger.info("Loading {} from {} ", fileName, additionalResource.getPath());
loadPropertiesFromInputStream(additionalResource.openStream(), orderedProperties);
}
}
// 2. load site conf, to keep backward compatibility it's still named kylin.properties
// actually it's better to be named kylin-site.properties
File propFile = getSitePropertiesFile();
if (propFile == null || !propFile.exists()) {
logger.error("fail to locate " + KYLIN_CONF_PROPERTIES_FILE);
throw new RuntimeException("fail to locate " + KYLIN_CONF_PROPERTIES_FILE);
}
loadPropertiesFromInputStream(new FileInputStream(propFile), orderedProperties);
// 3. still support kylin.properties.override as secondary override
// not suggest to use it anymore
File propOverrideFile = new File(propFile.getParentFile(), propFile.getName() + ".override");
if (propOverrideFile.exists()) {
loadPropertiesFromInputStream(new FileInputStream(propOverrideFile), orderedProperties);
}
return orderedProperties;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
bulidSiteProperties() 方法读取 KYLIN_HOME/conf/kylin.properties 文件,具体逻辑:
1.首先从classpath中读取默认kylin-defaults.properties文件,文件位置:kylin/core-common/src/main/resources
生成URL路径,Preconditions.checkNotNull(resource)判断是否为NULL,如果为null,则抛出空指针异常,不为null则使用url.openStream()返回一个InputSteam对象。调用kylinConfig.loadPropertiesFromInputStream()方法,把配置文件中的配置项通过kv形式赋值给orderedProperties对象,接着循环10次判断是否有额外新增的配置文件命名格式以kylin-defaults" + (i) + ".properties,如果有,则重复上述步骤调用kylinConfig.loadPropertiesFromInputStream()方法获取配置项
2.为了保持向后兼容性,调用kylin.getSitePropertiesFile() 返回一个文件对象,然后判断是否为null,接着同上调用kylin.getSitePropertiesFile() 返回一个文件对象重复上述步骤调用kylinConfig.loadPropertiesFromInputStream()方法获取配置项
3.还支持kylin.properties 的重写这里不再鳌述。
kylinConfig.reloadKylinConfig() 方法主要是通过调用
BackwardCompatibilityConfig.check()方法把key,value强制转化为String类型,类似于格式化