使用过hive的都知道,可以通过指定 -i参数或者配置.hiverc来设置hive启动时初始执行的一些命令,比如可以把udf的定义写到.hiverc文件中。加载.hiverc的过程是在CliDriver类中定义的。
具体的方法调用顺序:
main--->run--->executeDriver----->processInitFiles---->processFile---->proce***eader---->processLine--->processCmd,processCmd里面的具体实现就比较多了。
1)其中executeDriver中,处理 初始化文件的调用部分:
// Execute -i init files (always in silent mode)
cli.processInitFiles (ss); // 加载初始文件
if (ss. execString != null ) {
int cmdProcessStatus = cli.processLine(ss. execString); // -e的情况 ss.execString = commandLine.getOptionValue( 'e');
return cmdProcessStatus;
}
try {
if (ss. fileName != null) {
return cli.processFile(ss.fileName ); // -f的情况 ss.fileName = commandLine.getOptionValue('f' );
}
} catch (FileNotFoundException e) {
System. err.println("Could not open input file for reading. (" + e.getMessage() + ")" );
return 3;
}
2)其中在 proce***eader定义了.hiverc文件中注释的写法(以--开头)
public int proce***eader(BufferedReader r) throws IOException {
String line;
StringBuilder qsb = new StringBuilder();
while ((line = r.readLine()) != null) {
// Skipping through comments
if (! line.startsWith( "--")) {
qsb.append(line + "\n");
}
}
return (processLine(qsb.toString()));
}
3)processInitFiles定义了加载的顺序:
-i指定或者
$HIVE_HOME/bin/.hiverc--->$HIVE_CONF_DIR/.hiverc--->${user.home}/.hiverc
代码实现:
public void processInitFiles(CliSessionState ss) throws IOException {
boolean saveSilent = ss. getIsSilent();
ss.setIsSilent(true);
for (String initFile : ss. initFiles) { // ss. initFiles 指由 hive -i参数传入的文件路径
int rc = processFile(initFile);
if (rc != 0) {
System. exit(rc);
}
}
if (ss. initFiles.size() == 0) { // 如果 没有指定 -i参数,则按如下路径加载
if (System.getenv("HIVE_HOME" ) != null) { // $HIVE_HOME/bin/.hiverc
String hivercDefault = System. getenv("HIVE_HOME") + File. separator +
"bin" + File.separator + HIVERCFILE;
if (new File(hivercDefault).exists()) {
int rc = processFile(hivercDefault);
if (rc != 0) {
System. exit(rc);
}
console.printError( "Putting the global hiverc in " +
"$HIVE_HOME/bin/.hiverc is deprecated. Please " +
"use $HIVE_CONF_DIR/.hiverc instead." );
}
}
if (System.getenv("HIVE_CONF_DIR" ) != null) { // $HIVE_CONF_DIR/.hiverc
String hivercDefault = System. getenv("HIVE_CONF_DIR") + File. separator
+ HIVERCFILE;
if (new File(hivercDefault).exists()) {
int rc = processFile(hivercDefault);
if (rc != 0) {
System. exit(rc);
}
}
}
if (System.getProperty("user.home" ) != null) { //${user.home}/.hiverc
String hivercUser = System. getProperty("user.home") + File. separator +
HIVERCFILE;
if (new File(hivercUser).exists()) {
int rc = processFile(hivercUser);
if (rc != 0) {
System. exit(rc);
}
}
}
}
ss.setIsSilent(saveSilent);
}
转载于:https://blog.51cto.com/caiguangguang/1542269