使用过hive的都知道,可以通过指定 -i参数或者配置.hiverc来设置hive启动时初始执行的一些命令,比如可以把udf的定义写到.hiverc文件中。加载.hiverc的过程是在CliDriver类中定义的。

具体的方法调用顺序:

1
main--->run--->executeDriver----->processInitFiles---->processFile---->processReader---->processLine--->processCmd,processCmd里面的具体实现就比较多了。

1)其中executeDriver中,处理 初始化文件的调用部分:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  // 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)其中在 processReader定义了.hiverc文件中注释的写法(以--开头)

1
2
3
4
5
6
7
8
9
10
11
  public  int  processReader(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指定或者  

1
$HIVE_HOME/bin/.hiverc--->$HIVE_CONF_DIR/.hiverc--->${user.home}/.hiverc

代码实现:  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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);
   }