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 程序删除员工表。
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.hadoop.hive.jdbc.HiveDriver";
   
   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
      stmt.executeQuery("DROP TABLE if EXISTS employee;");
      System.out.println("Drop table successful.");
      
      con.close();
   }
}
将程序保存在名为 HiveDropTable.java 的文件中。使用以下命令编译并执行该程序。
$ javac HiveDropTable.java
$ java HiveDropTable

输出:

Drop table successful
以下查询用于验证表列表:
hive> SHOW TABLES;
emp
ok
Time taken: 2.1 seconds
hive>
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4