码迷,mamicode.com
首页 > 其他好文 > 详细

hiverc文件的加载实现

时间:2014-08-20 10:38:26      阅读:295      评论:0      收藏:0      [点我收藏+]

标签:hiverc加载


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

具体的方法调用顺序:

main--->run--->executeDriver----->processInitFiles---->processFile---->processReader---->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)其中在 processReader定义了.hiverc文件中注释的写法(以--开头)

 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指定或者  

$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);
  }

本文出自 “菜光光的博客” 博客,请务必保留此出处http://caiguangguang.blog.51cto.com/1652935/1542269

hiverc文件的加载实现,布布扣,bubuko.com

hiverc文件的加载实现

标签:hiverc加载

原文地址:http://caiguangguang.blog.51cto.com/1652935/1542269

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!