本章介紹了如何在Hive中刪除表。當(dāng)從Hive Metastore刪除表,它刪除了表/列的數(shù)據(jù)及其元數(shù)據(jù)。它可以是一個正常的表(存儲在Metastore)或外部表(存儲在本地文件系統(tǒng)); 不論什么類型Hive對待的方式相同。
語法如下:
DROP TABLE [IF EXISTS] table_name;
以下查詢刪除一個名為 employee 的表:
hive> DROP TABLE IF EXISTS employee;
對于成功執(zhí)行查詢,能看到以下回應(yīng):
OK Time taken: 5.3 seconds hive>
下面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文件。使用下面的命令來編譯和執(zhí)行這個程序。
$ javac HiveDropTable.java $ java HiveDropTable
Drop table successful
以下查詢被用來驗證表的列表:
hive> SHOW TABLES; emp ok Time taken: 2.1 seconds hive>
更多建議: