hive启动的参数提取+日志设置

当我们启动cli环境后,其实是启动了一个java程序,只不过这个程序是从console里读取命令进行交互,就跟shell一样的。

好,那么当我们启动之后,敲入命令"CREATE DATABASE financials;"之后,到底发生了什么?程序背后又是怎么做的呢?

就让我们跟着源码一起来分析下。

-------------------------------------------------------------------------------------

首先代码CliDriver.java的main函数很简单,如下:

  public static void main(String[] argsthrows Exception {

    int ret = run(args);

    System.exit(ret);

  }

所以需要继续跟踪run函数。

-------------------------------------------------------------------------------------

OptionsProcessor oproc = new OptionsProcessor();---生成了这么一个对象,

 

  private final Options options = new Options();---内部变量,属于commons-cli类

然后紧接着就加入了若干对象

 

 

// -e 'quoted-query-string'

    options.addOption(OptionBuilder

        .hasArg()

        .withArgName("quoted-query-string")

        .withDescription("SQL from command line")

        .create('e'));

 

    // -f <query-file>

    options.addOption(OptionBuilder

        .hasArg()

        .withArgName("filename")

        .withDescription("SQL from files")

        .create('f'));

 

    // -i <init-query-file>

    options.addOption(OptionBuilder

        .hasArg()

        .withArgName("filename")

        .withDescription("Initialization SQL file")

        .create('i'));

 

    // -hiveconf x=y

    options.addOption(OptionBuilder

        .withValueSeparator()

        .hasArgs(2)

        .withArgName("property=value")

        .withLongOpt("hiveconf")

        .withDescription("Use value for given property")

        .create());

 

    // -h hostname/ippaddress

    options.addOption(OptionBuilder

        .hasArg()

        .withArgName("hostname")

        .withDescription("connecting to Hive Server on remote host")

        .create('h'));

 

    // -p port

    options.addOption(OptionBuilder

        .hasArg()

        .withArgName("port")

        .withDescription("connecting to Hive Server on port number")

        .create('p'));

 

    // Substitution option -d, --define

    options.addOption(OptionBuilder

        .withValueSeparator()

        .hasArgs(2)

        .withArgName("key=value")

        .withLongOpt("define")

        .withDescription("Variable subsitution to apply to hive commands. e.g. -d A=B or --define A=B")

        .create('d'));

 

    // Substitution option --hivevar

    options.addOption(OptionBuilder

        .withValueSeparator()

        .hasArgs(2)

        .withArgName("key=value")

        .withLongOpt("hivevar")

        .withDescription("Variable subsitution to apply to hive commands. e.g. --hivevar A=B")

        .create());

 

    // [-S|--silent]

    options.addOption(new Option("S""silent"false"Silent mode in interactive shell"));

 

    // [-v|--verbose]

    options.addOption(new Option("v""verbose"false"Verbose mode (echo executed SQL to the console)"));

 

    // [-H|--help]

    options.addOption(new Option("H""help"false"Print help information"));

---这个没啥好说的,继续,初始化完之后,就开始使用了

 

if (!oproc.process_stage1(args)) {

      return 1;

    }

那么process_stage1内部究竟做了什么事情呢?--

 

public boolean process_stage1(String[] argv) {

// 看到这里了

try {

commandLine = new GnuParser().parse(optionsargv);// 依赖commons-cli第三方的包来解析参数

Properties confProps = commandLine.getOptionProperties("hiveconf");//---解析hiveconf的属性

for (String propKey : confProps.stringPropertyNames()) {---解析hiveconf的属性

System.setProperty(propKeyconfProps.getProperty(propKey));

}

 

Properties hiveVars = commandLine.getOptionProperties("define");---解析define 的属性

for (String propKey : hiveVars.stringPropertyNames()) {

hiveVariables.put(propKeyhiveVars.getProperty(propKey));

}

Properties hiveVars2 = commandLine.getOptionProperties("hivevar");---解析 hivevar的属性

for (String propKey : hiveVars2.stringPropertyNames()) {

hiveVariables.put(propKeyhiveVars2.getProperty(propKey));

}

catch (ParseException e) {

System.err.println(e.getMessage());

printUsage();

return false;

}

return true;---返回true.

}

 到目前为止,一切都很简单,就是解析了args 里的几个参数而已。

接下来是设置log4j

 

boolean logInitFailed = false;// ---设置日志属性,hive-log4j.properties

String logInitDetailMessage;

try {

logInitDetailMessage = LogUtils.initHiveLog4j();

catch (LogInitializationException e) {

logInitFailed = true;

logInitDetailMessage = e.getMessage();

}

很简单

------

 

 

 

 

转载于:https://my.oschina.net/qiangzigege/blog/636278

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值