Hive刪除表

本章介紹瞭如何在Hive中刪除表。當從Hive Metastore刪除表,它刪除了表/列的數據及其元數據。它可以是一個正常的表(存儲在Metastore)或外部表(存儲在本地文件系統); 不論什麼類型Hive對待的方式相同。

Drop Table語句

語法如下:

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.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>