0
点赞
收藏
分享

微信扫一扫

Hive3.1.2源码阅读--处理sql语句 processCmd

Python芸芸 2022-03-24 阅读 59
  1. 前文提及进入processCmd真正执行单条sql语句
  2. 进入该方法中查看相关的执行逻辑
    2.1 刷新打印流,防止上一个命令输出
    2.2 将传入的单句sql去除注释
    2.3 按照空格、制表符切割,存入token数组中
    2.4 判断切割出来的第一个字段是什么来决定相应的处理方式
    2.4.1 如果输入的sql是quit,则退出
    2.4.2 如果是source,则是执行跟在后面的文件,最后也是交给processFile去执行
    2.4.3 如果是shell脚本,则调用ShellCmdExecutor
    2.4.4 其他情况则是sql,通过processLocalCmd去执行
  public int processCmd(String cmd) {
    CliSessionState ss = (CliSessionState) SessionState.get();
    ss.setLastCommand(cmd);

    ss.updateThreadName();

    // 刷新打印流,防止上个命令输出
    ss.err.flush();
    String cmd_trimmed = HiveStringUtils.removeComments(cmd).trim();//删除注释
    // tokenizeCmd将用户输入的指令从空格,制表符等出断开,得到token数组
    String[] tokens = tokenizeCmd(cmd_trimmed);
    int ret = 0;

    // 如果命令是`quit`或`exit`,则退出
    if (cmd_trimmed.toLowerCase().equals("quit") || cmd_trimmed.toLowerCase().equals("exit")) {

      // if we have come this far - either the previous commands
      // are all successful or this is command line. in either case
      // this counts as a successful run
      ss.close();
      System.exit(0);

      // 如果命令是`source`开头,则校验`source`命令后面的文件是否存在,存在则执行`processFile`
    } else if (tokens[0].equalsIgnoreCase("source")) {
      String cmd_1 = getFirstCmd(cmd_trimmed, tokens[0].length());
      cmd_1 = new VariableSubstitution(new HiveVariableSource() {
        @Override
        public Map<String, String> getHiveVariable() {
          return SessionState.get().getHiveVariables();
        }
      }).substitute(ss.getConf(), cmd_1);

      File sourceFile = new File(cmd_1);
      // 如果source后跟的参数不是文件,就报错
      if (! sourceFile.isFile()){
        console.printError("File: "+ cmd_1 + " is not a file.");
        ret = 1;
      } else {
        try {
          // 不管再多操作,只要他是文件,最后也是交给processFile去执行
          ret = processFile(cmd_1);
        } catch (IOException e) {
          // 处理失败了会输出报错的原因
          console.printError("Failed processing file "+ cmd_1 +" "+ e.getLocalizedMessage(),
            stringifyException(e));
          ret = 1;
        }
      }
      // 如果命令是以感叹号!开头,表明是`shell`命令,这时候直接调用`shell`命令执行器执行
    } else if (cmd_trimmed.startsWith("!")) {//shell脚本
      // for shell commands, use unstripped command
      String shell_cmd = cmd.trim().substring(1);
      shell_cmd = new VariableSubstitution(new HiveVariableSource() {
        @Override
        public Map<String, String> getHiveVariable() {
          return SessionState.get().getHiveVariables();
        }
      }).substitute(ss.getConf(), shell_cmd);

      // shell_cmd = "/bin/bash -c \'" + shell_cmd + "\'";
      try {
        ShellCmdExecutor executor = new ShellCmdExecutor(shell_cmd, ss.out, ss.err);
        ret = executor.execute();
        if (ret != 0) {
          console.printError("Command failed with exit code = " + ret);
        }
      } catch (Exception e) {
        console.printError("Exception raised from Shell command " + e.getLocalizedMessage(),
            stringifyException(e));
        ret = 1;
      }
    }  else { // local mode
      // 去除上面各种类型,其他sql的主要执行方法为processLocalCmd
      try {
        //直接解析输入的sql
        try (CommandProcessor proc = CommandProcessorFactory.get(tokens, (HiveConf) conf)) {
          if (proc instanceof IDriver) {
            // Let Driver strip comments using sql parser
            ret = processLocalCmd(cmd, proc, ss);
          } else {
            ret = processLocalCmd(cmd_trimmed, proc, ss);
          }
        }
      } catch (SQLException e) {
        console.printError("Failed processing command " + tokens[0] + " " + e.getLocalizedMessage(),
          org.apache.hadoop.util.StringUtils.stringifyException(e));
        ret = 1;
      }
      catch (Exception e) {
        throw new RuntimeException(e);
      }
    }

    ss.resetThreadName();
    // 如果最后返回的ret为1,那么就表示执行失败。
    return ret;
  }
举报

相关推荐

0 条评论