Hive 删除表

  • Hive 删除表

    本章介绍如何在Hive中删除表。当您从Hive Metastore中删除表时,它将删除表/列数据及其元数据。它可以是普通表(存储在Metastore中)或外部表(存储在本地文件系统中);无论它们的类型如何,Hive都以相同的方式对待它们。
  • 删除表声明

    语法如下:
    
    DROP TABLE [IF EXISTS] table_name;
    
    以下查询删除一个名为employee的表:
    
    hive> DROP TABLE IF EXISTS employee;
    
    成功执行查询后,您将看到以下响应:
    
    OK
    Time taken: 5.3 seconds
    hive>
    
    JDBC程序
    以下JDBC程序删除了employee表。
    
    import java.sql.SQLException;
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import java.sql.DriverManager;
    
    public class HiveDropTable {
    
       private static String driverName = "org.apache.hive.jdbc.HiveDriver";
       
       public static void main(String[] args) throws SQLException {
          try {
             // Register driver and create driver instance
             Class.forName(driverName);
    
             // get connection
             Connection con = DriverManager.getConnection("jdbc:hive2://localhost:10000/userdb", "", "");
    
             // create statement
             Statement stmt = con.createStatement();
    
             // execute statement
             stmt.execute("DROP TABLE IF EXISTS employee");
             System.out.println("Drop table successful.");
             
             con.close();
          } catch (Exception e) {
             System.out.println(e.getMessage());
          }      
       }
    }
    
    将程序保存在名为HiveDropTable.java的文件中。使用以下命令来编译和执行该程序。
    
    $ javac HiveDropTable.java
    $ java HiveDropTable
    
    输出:
    
    Drop table successful
    
    以下查询用于验证表列表:
    
    hive> SHOW TABLES;
    emp
    ok
    Time taken: 2.1 seconds
    hive>