本文介绍了“如果存在表t1重命名为t2,则更改表";在HiveQL中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想重命名Hive表(如果存在),并且不生成错误.

I want to rename a Hive table if it exists, and not generate an error if it doesn't.

我需要类似的东西

如果存在t1,则将其更改为t2;

ALTER TABLE IF EXISTS t1 RENAME TO t2;

但是,这不会运行(无法识别出alter table语句中'if''exists''rename'附近的输入"),我尝试过的变体也没有.文档( https://cwiki .apache.org/confluence/display/Hive/LanguageManual + DDL#LanguageManualDDL-RenameTable ),也许是因为不可能.

but this doesn't run ("cannot recognize input near 'if' 'exists' 'rename' in alter table statement"), and neither do the variations that I've tried. This isn't covered in the docs (https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL#LanguageManualDDL-RenameTable), maybe because it's not possible.

有人知道如何执行此操作或解决方法(例如,尝试/捕获,如果它在Hive中存在)?

Does anyone know how to do this, or a workaround (e.g. try/catch, if it existed in Hive)?

我正在使用Hive 1.2.

I'm on Hive 1.2.

推荐答案

IF EXIST子句目前在Hive CLI中不起作用.您可以编写以下类似的程序来进行条件检查.

IF EXIST clause does not work in Hive CLI as of now. You can write program something like below for condition checking.

公共类HiveAlterRenameTo { 私有静态字符串driverName ="org.apache.hadoop.hive.jdbc.HiveDriver";

public class HiveAlterRenameTo { private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";

公共静态void main(String [] args)抛出SQLException {

public static void main(String[] args) throws SQLException {

  // Register driver and create driver instance
  Class.forName(driverName);

  // get connection
  Connection con = DriverManager.getConnection("jdbc:hive://localhost:10000/userdb", "", "");

  // create statement
  Statement stmt = con.createStatement();

  // execute statement
  Resultset res = stmt.executeQuery("SELECT count(*) FROM <Table_name> ;");


  if (res > 0) {
    // execute statement
  stmt.executeQuery("ALTER TABLE employee RENAME TO emp;");
    System.out.println("Table Renamed Successfully");

  }
  else {
        System.out.println("Table Not exist");
  }
  con.close();

}

这篇关于“如果存在表t1重命名为t2,则更改表";在HiveQL中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 15:56