简单看下源码:
org.apache.hadoop.hive.conf.HiveConf
HiveConf中有静态代码块,内容就是调用findConfigFile方法,尝试读取hive-default.xml,hive-site.xml,hivemetastore-site.xml,hiveserver2-site.xml。四个文件的内容。
static {ClassLoader classLoader = Thread.currentThread().getContextClassLoader();if (classLoader == null) {classLoader = HiveConf.class.getClassLoader();}hiveDefaultURL = classLoader.getResource("hive-default.xml");hiveSiteURL = findConfigFile(classLoader, "hive-site.xml", true);hivemetastoreSiteUrl = findConfigFile(classLoader, "hivemetastore-site.xml", false);hiveServer2SiteUrl = findConfigFile(classLoader, "hiveserver2-site.xml", false);
省略...
}
findConfigFile方法就更简单了
System.getenv("...")就是查找系统环境变量HIVE_CONF_DIR和HIVE_HOME,显然HIVE_CONF_DIR比HIVE_HOME优先级更高。然后组装成路径,并读取文件内容。
private static URL findConfigFile(ClassLoader classLoader, String name, boolean doLog) {URL result = classLoader.getResource(name);if (result == null) {String confPath = System.getenv("HIVE_CONF_DIR");result = checkConfigFile(new File(confPath, name));if (result == null) {String homePath = System.getenv("HIVE_HOME");String nameInConf = "conf" + File.separator + name;result = checkConfigFile(new File(homePath, nameInConf));省略...
总结:
使用HiveConf特别简单额,设置好环境变量HIVE_CONF_DIR和HIVE_HOME后就可以在代码中用了。
注意:是环境变量,不是java系统变量,即System.setProperty()和-D是不行的,这两项都是设置java变量,不是操作系统变量