Hive刪除表
阿新 • • 發佈:2019-01-10
本章介紹瞭如何在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>