Hive - Create Table&Drop Table & ALTER Table(中)

译注:书接上篇,了解过创建表以及load data后,假如发现需要更改表字段类型或者添加表字段,怎么办?这篇文章将进一步了解具体细节。

This chapter explains how to alter the attributes of a table such as changing its table name, changing column names, adding columns, and deleting or replacing columns.

Alter Table Statement

It is used to alter a table in Hive.

Syntax

The statement takes any of the following syntaxes based on what attributes we wish to modify in a table.

ALTER TABLE name RENAME TO new_name
ALTER TABLE name ADD COLUMNS (col_spec[, col_spec ...])
ALTER TABLE name DROP [COLUMN] column_name ------------ 译注:这个drop命令,我在工作中,并没有生效,而是利用表级别的REPLACE进行替换式删除字段的
ALTER TABLE name CHANGE column_name new_name new_type
ALTER TABLE name REPLACE COLUMNS (col_spec[, col_spec ...])

Rename To… Statement

The following query renames the table from employee to emp.

hive> ALTER TABLE employee RENAME TO emp;

JDBC Program

The JDBC program to rename a table is as follows.

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();
   }
}

Save the program in a file named HiveAlterRenameTo.java. Use the following commands to compile and execute this program.

$ javac HiveAlterRenameTo.java
$ java HiveAlterRenameTo

Output:

Table renamed successfully.

Change Statement

The following table contains the fields of employee table and it shows the fields to be changed (in bold).

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

The following queries rename the column name and column data type using the above data:

hive> ALTER TABLE employee CHANGE name ename String;
hive> ALTER TABLE employee CHANGE salary salary Double;

JDBC Program

Given below is the JDBC program to change a column.

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();
   }
}

Save the program in a file named HiveAlterChangeColumn.java. Use the following commands to compile and execute this program.

$ javac HiveAlterChangeColumn.java
$ java HiveAlterChangeColumn

Output:

Change column successful.

Add Columns Statement

The following query adds a column named dept to the employee table.

hive> ALTER TABLE employee ADD COLUMNS (dept STRING COMMENT ‘Department name‘);

-- 有一些文章里说 add columns dept STRING COMMENT ‘comment‘ ;但是在一些高版本里,需要把你需要加的列放到括号里()

JDBC Program

The JDBC program to add a column to a table is given below.

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();
   }
}

Save the program in a file named HiveAlterAddColumn.java. Use the following commands to compile and execute this program.

$ javac HiveAlterAddColumn.java
$ java HiveAlterAddColumn

Output:

Add column successful.

Replace Statement

The following query deletes all the columns from the employee table and replaces it with emp and name columns:

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

JDBC Program

Given below is the JDBC program to replace eid column with empid and ename column with 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();
   }
}

Save the program in a file named HiveAlterReplaceColumn.java. Use the following commands to compile and execute this program.

$ javac HiveAlterReplaceColumn.java
$ java HiveAlterReplaceColumn

Output:

Replace column successful.

Drop Table主要就一个命令,就不单独再搞一篇了,在此一并搞了。--------------------------------------------------

Drop Table Statement

The syntax is as follows:

DROP TABLE [IF EXISTS] table_name;

The following query drops a table named employee:

hive> DROP TABLE IF EXISTS employee;

On successful execution of the query, you get to see the following response:

OK
Time taken: 5.3 seconds
hive>

---------------------------------------------------

译注:本章中一些操作,我个人并没有一一验证。后续使用到并验证后,如果有问题,会备注进去。目前里面的内容基本都是英文文章里的内容。

------------英文地址:https://www.tutorialspoint.com/hive/hive_alter_table.htm
时间: 2024-08-24 05:28:34

Hive - Create Table&Drop Table & ALTER Table(中)的相关文章

数据定义: CREATE、DROP、ALTER

1 CREATE DATABASE 句法 2 3 CREATE DATABASE [IF NOT EXISTS] db_name 4 5 CREATE DATABASE 以给定名字创建一个数据库.允许的数据库名规则在章节 6.1.2 数据库.表.索引.列和别名 中被给出. 如果数据库已经存在,并且你没有指定 IF NOT EXISTS,这时会产生一个错误. 6 7 在 MySQL 中,数据库以包含数据库表对应文件的目录实现的.因为数据库在初始创建时没有表,所以 CREATE DATABASE 语

[Hive - LanguageManual] Alter Table/Partition/Column

Alter Table/Partition/Column Alter Table Rename Table Alter Table Properties Alter Table Comment Add SerDe Properties Alter Table Storage Properties Additional Alter Table Statements Alter Partition Add Partitions Dynamic Partitions Rename Partition

[转]ALTER TABLE的用法 增多列、删除列、改列名、改列约束、改表名

[转]ALTER TABLE的用法 增加列.删除列.改列名.改列约束.改表名 ALTER TABLE 名称ALTER TABLE - 更改表属性语法ALTER TABLE table [ * ]      ADD [ COLUMN ] column typeALTER TABLE table [ * ]      ALTER [ COLUMN ] column { SET DEFAULT value | DROP DEFAULT }ALTER TABLE table [ * ]      REN

alter table table_name enable row movement

Row movement 从字面意思解释为行移动.默认情况下,oracle数据块中的一行其生命周期内是不会发生移动的,即其rowid不会发生改变. 但是在某些情景下,我们希望行的rowid可以发生变化,这时候我们就需要启动表的row movement特性. 启用row movement特性,使用如下语句: Alter table table_name enable row movement; 通查在三种情景下,需要启用row movement. 1:分区表 当我们允许分区表的分区键是可更新的时候

ALTER TABLE - 修改表的定义

SYNOPSIS ALTER TABLE [ ONLY ] name [ * ] ADD [ COLUMN ] column type [ column_constraint [ ... ] ] ALTER TABLE [ ONLY ] name [ * ] DROP [ COLUMN ] column [ RESTRICT | CASCADE ] ALTER TABLE [ ONLY ] name [ * ] ALTER [ COLUMN ] column { SET DEFAULT expr

改表 alter table

ALTER TABLE [ IF EXISTS ] [ ONLY ] name [ * ] action [, ... ] ALTER TABLE [ IF EXISTS ] [ ONLY ] name [ * ] RENAME [ COLUMN ] column_name TO new_column_name ALTER TABLE [ IF EXISTS ] [ ONLY ] name [ * ] RENAME CONSTRAINT constraint_name TO new_constr

SQL高级应用(DROP、ALTER)

SQL 撤销索引.表以及数据库 通过使用 DROP 语句,可以轻松的删除索引.表和数据库 SQL DROP INDEX  使用DROP INDEX 命令删除表格中的索引 用于 Microsoft SQLJet(以及 Microsoft Access)的语法 DROP INDEX index_name ON table_name 用于 MS SQL Server 的语法 DROP INDEX table_name.index_name 用于 IBM DB2 和 Oracle的语法 DROP IND

Hive - Create Table&Drop Table & ALTER Table(上)

写在前面:本来想着把表的创建,删除,以及修改一篇搞定的.结果看了一下,东西还是蛮多的,而且也是很多经常使用的操作.所以,就暂且分开处理吧. 特别提醒:在日常不管是创建库.表还是修改字段,删除等操作,建议都加上 [IF NOT EXISTS] | [IF EXISTS] 选项:虽然是可选项,但是还是小心为上,万一你在操作时没有加库名,又操作错了,那你哭都找不到地方. This chapter explains how to create a table and how to insert data

SQL ALTER TABLE 语句在项目中的使用

1.在实际的项目开发过程中,之前已经创建好的实体类可能需要增加/删除字段,亦或是更改已有字段的属性,比如主键的增长策略从自增型改为UUID型,那么就会涉及到 SQL 中 alter table 语句的使用. ALTER TABLE table_name ADD column_name datatype 增加表中的列 ALTER TABLE table_name DROP COLUMN column_name 删除表中的列 ALTER TABLE table_name ALTER COLUMN c