remote:
hive> use default;
local:
CliDriver.java中CliDriver cli = new CliDriver(); 打断点
入口类-CliDriver
解析流程如图:
部分源码批注
publicintrun(String[] args)throws Exception {
OptionsProcessor oproc =newOptionsProcessor();if(!oproc.process_stage1(args)){return1;}// NOTE: It is critical to do this here so that log4j is reinitialized// before any of the other core hive classes are loadedboolean logInitFailed =false;
String logInitDetailMessage;try{
logInitDetailMessage = LogUtils.initHiveLog4j();}catch(LogInitializationException e){
logInitFailed =true;
logInitDetailMessage = e.getMessage();}
CliSessionState ss =newCliSessionState(newHiveConf(SessionState.class));
ss.in = System.in;try{
ss.out =newSessionStream(System.out,true, StandardCharsets.UTF_8.name());
ss.info =newSessionStream(System.err,true, StandardCharsets.UTF_8.name());
ss.err =newCachingPrintStream(System.err,true,
StandardCharsets.UTF_8.name());}catch(UnsupportedEncodingException e){return3;}if(!oproc.process_stage2(ss)){return2;}// 当前会话是否在 silent 模式运行。如果不是 silent 模式,所以 info 级打在日志中的消息,都将以标准错误流的形式输出到控制台。// 当前集群配置应该是false 默认值if(!ss.getIsSilent()){if(logInitFailed){
System.err.println(logInitDetailMessage);}else{
SessionState.getConsole().printInfo(logInitDetailMessage);}}// set all properties specified via command line// 处理用户级的配置命令 如:hive> set hive.session.silent=true;
HiveConf conf = ss.getConf();for(Map.Entry<Object, Object> item : ss.cmdProperties.entrySet()){
conf.set((String) item.getKey(),(String) item.getValue());
ss.getOverriddenConfigurations().put((String) item.getKey(),(String) item.getValue());}// read prompt configuration and substitute variables.// 读取变量并替换变量
prompt = conf.getVar(HiveConf.ConfVars.CLIPROMPT);
prompt =newVariableSubstitution(newHiveVariableSource(){@Overridepublic Map<String, String>getHiveVariable(){return SessionState.get().getHiveVariables();}}).substitute(conf, prompt);
prompt2 =spacesForString(prompt);// Tez相关if(HiveConf.getBoolVar(conf, ConfVars.HIVE_CLI_TEZ_SESSION_ASYNC)){// Start the session in a fire-and-forget manner. When the asynchronously initialized parts of// the session are needed, the corresponding getters and other methods will wait as needed.
SessionState.beginStart(ss, console);}else{
SessionState.start(ss);}
ss.updateThreadName();// Initialize metadata provider class and trimmer
CalcitePlanner.warmup();// Create views registry
HiveMaterializedViewsRegistry.get().init();// execute cli driver worktry{//执行executeDriver(ss, conf, oproc);return0;}catch(CommandProcessorException e){return e.getResponseCode();}finally{
ss.resetThreadName();
ss.close();}}