在run
方法最后调用了executeDriver
方法,同时传入三个参数:
- ss hiveconf用户配置
- conf hiveconf默认配置
- oproc 用户环境配置
private int executeDriver(CliSessionState ss, HiveConf conf, OptionsProcessor oproc) throws Exception {
// 1.创建cli对象
CliDriver cli = new CliDriver();
// 将 -e 后的参数封装进cli(命令行执行的sql语句)
cli.setHiveVariables(oproc.getHiveVariables());
// 将 -database 后的参数封装进cli(数据库选项)
cli.processSelectDatabase(ss);
// 将 -i 后的参数封装进,如果未指定则加载.hiverc文件
cli.processInitFiles(ss);
// 2.如果hive -e后有参数(sql语句),则执行sql语句
if (ss.execString != null) {
int cmdProcessStatus = cli.processLine(ss.execString);
return cmdProcessStatus;
} else {
try {
// 如果hive -h后有参数(sql脚本文件),则执行脚本文件
if (ss.fileName != null) {
return cli.processFile(ss.fileName);
}
} catch (FileNotFoundException var11) {
System.err.println("Could not open input file for reading. (" + var11.getMessage() + ")");
return 3;
}
// 3.如果是设定了执行引擎是mr,则告警提示
if ("mr".equals(HiveConf.getVar(conf, ConfVars.HIVE_EXECUTION_ENGINE))) {
this.console.printInfo(HiveConf.generateMrDeprecationWarning());
}
// 初始化控制台用于接收语句
this.setupConsoleReader();
int ret = 0;
String prefix = "";
// 4.进入默认的库中
String curDB = getFormattedDb(conf, ss);
String curPrompt = prompt + curDB;
// 将字符串替换为空格
String dbSpaces = spacesForString(curDB);
// 5.这里是一个while循环,直到遇到分号的时候才执行命令。
while(true) {
while(true) {
String line;
// 开始从命令行获取sql 语句,如果是以 ; 结尾,则开始执行sql
do {
if ((line = this.reader.readLine(curPrompt + "> ")) == null) {
return ret;
}
// 换行
if (!prefix.equals("")) {
prefix = prefix + '\n';
}
}
// 忽略使用 -- 开头的行
while(line.trim().startsWith("--"));
// 判断line是否以分号结尾,同时不以\\;(被注释)结尾
if (line.trim().endsWith(";") && !line.trim().endsWith("\\;")) {
line = prefix + line;
// 提交sql语句,开始执行hive命令
ret = cli.processLine(line, true);
prefix = "";
curDB = getFormattedDb(conf, ss);
curPrompt = prompt + curDB;
dbSpaces = dbSpaces.length() == curDB.length() ? dbSpaces : spacesForString(curDB);
} else {
prefix = prefix + line;
curPrompt = prompt2 + dbSpaces;
}
}
}
}
}