📜  Hive-修改表

📅  最后修改于: 2020-11-30 04:52:42             🧑  作者: Mango


本章说明如何更改表的属性,例如更改表名称,更改列名称,添加列以及删除或替换列。

修改表语句

它用于更改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程序

重命名表的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表的字段,并显示要更改的字段(粗体)。

Field Name Convert from Data Type Change Field Name Convert to Data Type
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程序

下面给出的是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程序

下面给出了将列添加到表的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表中删除所有列,并将其替换为empname列:

hive> ALTER TABLE employee REPLACE COLUMNS ( 
eid INT empid Int, 
ename STRING name String);

JDBC程序

下面给出的是JDBC程序,用于将eid列替换为empid并将ename列替换为name

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.