本章将介绍如何修改表的属性,如,修改表名,修改列名,添加列,并删除或替换列。
它是在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.