beeline源码分析

本文基于hive-1.2.2源码
beeline模块在beeline目录下

入口文件

beeline/src/java/org/apache/hive/beeline/BeeLine.java

hive驱动

beeline有2个hive driver:
org.apache.hive.jdbc.HiveDriver,用于连接hiveserver2
org.apache.hadoop.hive.jdbc.HiveDriver,用于连接hiveserver

static final SortedSet KNOWN_DRIVERS = new TreeSet(Arrays.asList(
  new String[] {
      "org.apache.hive.jdbc.HiveDriver",
      "org.apache.hadoop.hive.jdbc.HiveDriver",
  }));

beeline还支持连接mysql和postgresql:

private List supportedLocalDriver =
new ArrayList(Arrays.asList("com.mysql.jdbc.Driver", "org.postgresql.Driver"));
执行流程

1.由main函数开始,真正执行是在begin(args, inputStream)函数

public static void main(String[] args) throws IOException {
  mainWithInputRedirection(args, null);
}
...
public static void mainWithInputRedirection(String[] args, InputStream 
inputStream) throws IOException {
  BeeLine beeLine = new BeeLine();
  int status = beeLine.begin(args, inputStream);
  if (!Boolean.getBoolean(BeeLineOpts.PROPERTY_NAME_EXIT)) {
      System.exit(status);
  }
}

2.begin(args, inputStream)函数会分2步
2.1.加载beeline.properties
beeline会到当前用户目录下(linux是 HOME/.beeline,windows是 HOME/beeline)查找beeline.properties,如果找到,则加载其中以beeline.开头的配置项

try {
  // load the options first, so we can override on the command line
  getOpts().load();
 } catch (Exception e) {
  // nothing
}

2.2.解析命令行参数并执行
beeline从命令行中解析出driver,user,pass,url,auth等参数,并调用dispatch(command)函数

int code = initArgs(args);
  if (code != 0) {
    return code;
}

3.dispatch函数中,会区分beeline命令(以!开头)和sql,分别执行

boolean dispatch(String line) {
...
if (line.startsWith(COMMAND_PREFIX)) {
...
  if (cmdMap.size() > 1) {
    // any exact match?
    CommandHandler handler = cmdMap.get(line);
    if (handler == null) {
      return error(loc("multiple-matches", cmdMap.keySet().toString()));
    }
    return handler.execute(line);
  }
  return cmdMap.values().iterator().next()
      .execute(line);
} else {
  return commands.sql(line, getOpts().getEntireLineAsCommand());
}
}

4.具体执行逻辑在Commands.java,都是一些jdbc操作

apache-hive-1.2.2-src/beeline/src/java/org/apache/hive/beeline/Commands.java

你可能感兴趣的:(beeline源码分析)