本章將介紹如何修改表的屬性,如,修改表名,修改列名,新增列,並刪除或替換列。
它是在Hive中用來修改的表。
宣告接受任意屬性,我們希望在一個表中修改以下語法。
ALTER TABLE name RENAME TO new_name ALTER TABLE name ADD COLUMNS (col_spec[, col_spec ...]) ALTER TABLE name DROP [COLUMN] column_name ALTER TABLE name CHANGE column_name new_name new_type ALTER TABLE name REPLACE COLUMNS (col_spec[, col_spec ...])
下面是查詢重新命名表,把 employee 修改為 emp。
hive> ALTER TABLE employee RENAME TO emp;
在JDBC程式重新命名表如下。
import java.sql.SQLException; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import java.sql.DriverManager; public class HiveAlterRenameTo { 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("ALTER TABLE employee RENAME TO emp;"); System.out.println("Table Renamed Successfully"); con.close(); } }
將該程式儲存在一個名為HiveAlterRenameTo.java檔案。使用下面的命令來編譯和執行這個程式。
$ javac HiveAlterRenameTo.java $ java HiveAlterRenameTo
Table renamed successfully.
下表包含employee表的欄位,它顯示的欄位要被更改(粗體)。
欄位名 | 從資料型別轉換 | 更改欄位名稱 | 轉換為資料型別 |
---|---|---|---|
eid | int | eid | int |
name | String | ename | String |
salary | Float | salary | Double |
designation | String | designation | String |
下面查詢重新命名使用上述資料的列名和列資料型別:
hive> ALTER TABLE employee CHANGE name ename String; hive> ALTER TABLE employee CHANGE salary salary Double;
下面給出的是使用JDBC程式來更改列。
import java.sql.SQLException; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import java.sql.DriverManager; public class HiveAlterChangeColumn { 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("ALTER TABLE employee CHANGE name ename String;"); stmt.executeQuery("ALTER TABLE employee CHANGE salary salary Double;"); System.out.println("Change column successful."); con.close(); } }
將該程式儲存在一個名為HiveAlterChangeColumn.java檔案。使用下面的命令來編譯和執行這個程式。
$ javac HiveAlterChangeColumn.java $ java HiveAlterChangeColumn
Change column successful.
下面的查詢增加了一個列名dept在employee表。
hive> ALTER TABLE employee ADD COLUMNS ( > dept STRING COMMENT 'Department name');
JDBC程式新增列到表如下。
import java.sql.SQLException; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import java.sql.DriverManager; public class HiveAlterAddColumn { 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("ALTER TABLE employee ADD COLUMNS " + " (dept STRING COMMENT 'Department name');"); System.out.prinln("Add column successful."); con.close(); } }
將該程式儲存在一個名為HiveAlterAddColumn.java檔案。使用下面的命令來編譯和執行這個程式。
$ javac HiveAlterAddColumn.java $ java HiveAlterAddColumn
Add column successful.
以下從employee表中查詢刪除的所有列,並使用emp替換列:
hive> ALTER TABLE employee REPLACE COLUMNS ( > eid INT empid Int, > ename STRING name String);
下面給出的是JDBC程式使用empid代替eid列,name代替ename列。
import java.sql.SQLException; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import java.sql.DriverManager; public class HiveAlterReplaceColumn { 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("ALTER TABLE employee REPLACE COLUMNS " +" (eid INT empid Int," +" ename STRING name String);"); System.out.println(" Replace column successful"); con.close(); } }
將該程式儲存在一個名為HiveAlterReplaceColumn.java檔案。使用下面的命令來編譯和執行這個程式。
$ javac HiveAlterReplaceColumn.java $ java HiveAlterReplaceColumn
Replace column successful.