Spark SQL----ALTER TABLE

一、描述

ALTER TABLE语句修改表的schema或属性。

二、RENAME

ALTER TABLE RENAME TO语句更改数据库中现有表的表名。表重命名命令不能用于在数据库之间移动表,只能用于重命名同一数据库中的表。
如果表已缓存,则命令会清除表的缓存数据。下次访问表时,缓存将被延迟填充。此外:

  • table rename命令取消缓存所有表的从属项(dependents),例如引用该表的视图。应再次显式缓存从属项。
  • partition rename命令清除所有表从属项的缓存,同时保持它们处于缓存状态。因此,当下次访问它们时,它们的缓存将被延迟填充。

2.1 语法

ALTER TABLE table_identifier RENAME TO table_identifier

ALTER TABLE table_identifier partition_spec RENAME TO partition_spec

2.2 参数

  • table_identifier
    指定一个表名称,可以选择使用数据库名称对其进行限定。
    语法:[ database_name. ] table_name
  • partition_spec
    要重命名的分区。请注意,可以在分区规范中使用类型化的文字(例如,date’2019-01-02’)。
    语法:PARTITION ( partition_col_name = partition_col_val [ , … ] )

三、ADD COLUMNS

ALTER TABLE ADD COLUMNS语句将指定的列添加到现有表中。

3.1 语法

ALTER TABLE table_identifier ADD COLUMNS ( col_spec [ , ... ] )

3.2 参数

  • table_identifier
    指定表名,可以选择使用数据库名进行限定。
    语法:[ database_name. ] table_name
  • COLUMNS ( col_spec )
    指定要添加的列。

四、DROP COLUMNS

ALTER TABLE DROP COLUMNS语句从已存在的表中删除所提到的列。注意,这个语句只支持v2表。

4.1 语法

ALTER TABLE table_identifier DROP { COLUMN | COLUMNS } [ ( ] col_name [ , ... ] [ ) ]

4.2 参数

  • table_identifier
    指定一个表名称,可以选择使用数据库名称对其进行限定。
    语法:[ database_name. ] table_name
  • col_name
    指定列的名称。

五、ALTER OR CHANGE COLUMN

ALTER TABLE ALTER COLUMN 或ALTER TABLE CHANGE COLUMN语句修改列的定义。

5.1 语法

ALTER TABLE table_identifier { ALTER | CHANGE } [ COLUMN ] col_name alterColumnAction

5.2 参数

  • table_identifier
    指定一个表名称,可以选择使用数据库名称对其进行限定。
    语法:[ database_name. ] table_name
  • col_name
    指定列的名称。
  • alterColumnAction
    更改列的定义。

六、REPLACE COLUMNS

ALTER TABLE REPLACE COLUMNS语句删除所有现有列,并添加一组新的列。注意,这个语句只由v2表支持。

6.1 语法

ALTER TABLE table_identifier [ partition_spec ] REPLACE COLUMNS  
  [ ( ] qualified_col_type_with_position_list [ ) ]

6.2 参数

  • table_identifier
    指定一个表名称,可以选择使用数据库名称对其进行限定。
    语法:[ database_name. ] table_name
  • partition_spec
    要更换的分区。请注意,可以在分区规范中使用类型化的文字(例如,date’2019-01-02’)。
    语法:PARTITION ( partition_col_name = partition_col_val [ , … ] )
  • qualified_col_type_with_position_list
    要添加的列的列表
    语法:col_name col_type [ col_comment ] [ col_position ] [ , … ]

七、ADD PARTITION

ALTER TABLE ADD语句将分区添加到已分区的表中。
如果表是缓存的,则该命令将清除表的缓存数据及其引用它的所有从属项(dependents)。下次访问表或从属项时,缓存将被延迟填充。

7.1 语法

ALTER TABLE table_identifier ADD [IF NOT EXISTS] 
    ( partition_spec [ partition_spec ... ] )

7.2 参数

  • table_identifier
    指定一个表名称,可以选择使用数据库名称对其进行限定。
    语法:[ database_name. ] table_name
  • partition_spec
    要添加的分区。请注意,可以在分区规范中使用类型化的文字(例如,date’2019-01-02’)。
    语法:PARTITION ( partition_col_name = partition_col_val [ , … ] )

八、DROP PARTITION

ALTER TABLE DROP语句删除表的分区。
如果表是缓存的,则该命令将清除表的缓存数据及其引用它的所有从属项(dependents)。下次访问表或从属项时,缓存将被延迟填充。

8.1 语法

ALTER TABLE table_identifier DROP [ IF EXISTS ] partition_spec [PURGE]

8.2 参数

  • table_identifier
    指定一个表名称,可以选择使用数据库名称对其进行限定。
    语法:[ database_name. ] table_name
  • partition_spec
    要删除的分区。请注意,可以在分区规范中使用类型化的文字(例如,date’2019-01-02’)。
    语法:PARTITION ( partition_col_name = partition_col_val [ , … ] )

九、SET TABLE PROPERTIES

ALTER TABLE SET命令用于设置表的属性。如果已经设置了特定的属性,则会用新值覆盖旧值。ALTER TABLE UNSET用于删除TABLE属性。

9.1 语法

-- Set Table Properties 
ALTER TABLE table_identifier SET TBLPROPERTIES ( key1 = val1, key2 = val2, ... )

-- Unset Table Properties
ALTER TABLE table_identifier UNSET TBLPROPERTIES [ IF EXISTS ] ( key1, key2, ... )

十、SET SERDE

ALTER TABLE SET命令用于设置Hive表中的SERDE或SERDE属性。如果已经设置了特定的属性,则会用新值覆盖旧值。

10.1 语法

-- Set SERDE Properties
ALTER TABLE table_identifier [ partition_spec ]
    SET SERDEPROPERTIES ( key1 = val1, key2 = val2, ... )

ALTER TABLE table_identifier [ partition_spec ] SET SERDE serde_class_name
    [ WITH SERDEPROPERTIES ( key1 = val1, key2 = val2, ... ) ]

十一、SET LOCATION And SET FILE FORMAT

ALTER TABLE SET命令还可用于更改现有表的文件位置和文件格式。如果表是缓存的,那么ALTER TABLE … SET LOCATION命令清除表的缓存数据及其所有引用表的从属项(dependents)。当下一次访问表或从属项时,缓存将被延迟填充。

11.1 语法

-- Changing File Format
ALTER TABLE table_identifier [ partition_spec ] SET FILEFORMAT file_format

-- Changing File Location
ALTER TABLE table_identifier [ partition_spec ] SET LOCATION 'new_location'

11.2 参数

  • table_identifier
    指定一个表名称,可以选择使用数据库名称对其进行限定。
    语法:[ database_name. ] table_name
  • partition_spec
    指定必须在其上设置属性的分区。请注意,可以在分区规范中使用类型化的文字(例如,date’2019-01-02’)。
    语法:PARTITION ( partition_col_name = partition_col_val [ , … ] )
  • SERDEPROPERTIES ( key1 = val1, key2 = val2, … )
    指定要设置的SERDE特性。

十二、RECOVER PARTITIONS

ALTER TABLE RECOVER PARTITIONS语句恢复表目录中的所有分区并更新Hive metastore。恢复分区的另一种方法是使用 MSCK REPAIR TABLE。

12.1 语法

ALTER TABLE table_identifier RECOVER PARTITIONS

12.2 参数

  • table_identifier
    指定表名,可以选择使用数据库名进行限定。
    语法:[ database_name. ] table_name

十三、例子

-- RENAME table 
DESC student;
+-----------------------+---------+-------+
|               col_name|data_type|comment|
+-----------------------+---------+-------+
|                   name|   string|   NULL|
|                 rollno|      int|   NULL|
|                    age|      int|   NULL|
|# Partition Information|         |       |
|             # col_name|data_type|comment|
|                    age|      int|   NULL|
+-----------------------+---------+-------+

ALTER TABLE Student RENAME TO StudentInfo;

-- After Renaming the table
DESC StudentInfo;
+-----------------------+---------+-------+
|               col_name|data_type|comment|
+-----------------------+---------+-------+
|                   name|   string|   NULL|
|                 rollno|      int|   NULL|
|                    age|      int|   NULL|
|# Partition Information|         |       |
|             # col_name|data_type|comment|
|                    age|      int|   NULL|
+-----------------------+---------+-------+

-- RENAME partition

SHOW PARTITIONS StudentInfo;
+---------+
|partition|
+---------+
|   age=10|
|   age=11|
|   age=12|
+---------+

ALTER TABLE default.StudentInfo PARTITION (age='10') RENAME TO PARTITION (age='15');

-- After renaming Partition
SHOW PARTITIONS StudentInfo;
+---------+
|partition|
+---------+
|   age=11|
|   age=12|
|   age=15|
+---------+

-- Add new columns to a table
DESC StudentInfo;
+-----------------------+---------+-------+
|               col_name|data_type|comment|
+-----------------------+---------+-------+
|                   name|   string|   NULL|
|                 rollno|      int|   NULL|
|                    age|      int|   NULL|
|# Partition Information|         |       |
|             # col_name|data_type|comment|
|                    age|      int|   NULL|
+-----------------------+---------+-------+

ALTER TABLE StudentInfo ADD columns (LastName string, DOB timestamp);

-- After Adding New columns to the table
DESC StudentInfo;
+-----------------------+---------+-------+
|               col_name|data_type|comment|
+-----------------------+---------+-------+
|                   name|   string|   NULL|
|                 rollno|      int|   NULL|
|               LastName|   string|   NULL|
|                    DOB|timestamp|   NULL|
|                    age|      int|   NULL|
|# Partition Information|         |       |
|             # col_name|data_type|comment|
|                    age|      int|   NULL|
+-----------------------+---------+-------+

-- Drop columns of a table
DESC StudentInfo;
+-----------------------+---------+-------+
|               col_name|data_type|comment|
+-----------------------+---------+-------+
|                   name|   string|   NULL|
|                 rollno|      int|   NULL|
|               LastName|   string|   NULL|
|                    DOB|timestamp|   NULL|
|                    age|      int|   NULL|
|# Partition Information|         |       |
|             # col_name|data_type|comment|
|                    age|      int|   NULL|
+-----------------------+---------+-------+

ALTER TABLE StudentInfo DROP columns (LastName, DOB);

-- After dropping columns of the table
DESC StudentInfo;
+-----------------------+---------+-------+
|               col_name|data_type|comment|
+-----------------------+---------+-------+
|                   name|   string|   NULL|
|                 rollno|      int|   NULL|
|                    age|      int|   NULL|
|# Partition Information|         |       |
|             # col_name|data_type|comment|
|                    age|      int|   NULL|
+-----------------------+---------+-------+

-- Rename a column of a table
DESC StudentInfo;
+-----------------------+---------+-------+
|               col_name|data_type|comment|
+-----------------------+---------+-------+
|                   name|   string|   NULL|
|                 rollno|      int|   NULL|
|                    age|      int|   NULL|
|# Partition Information|         |       |
|             # col_name|data_type|comment|
|                    age|      int|   NULL|
+-----------------------+---------+-------+

ALTER TABLE StudentInfo RENAME COLUMN name TO FirstName;

-- After renaming a column of the table
DESC StudentInfo;
+-----------------------+---------+-------+
|               col_name|data_type|comment|
+-----------------------+---------+-------+
|              FirstName|   string|   NULL|
|                 rollno|      int|   NULL|
|                    age|      int|   NULL|
|# Partition Information|         |       |
|             # col_name|data_type|comment|
|                    age|      int|   NULL|
+-----------------------+---------+-------+

-- ALTER OR CHANGE COLUMNS
DESC StudentInfo;
+-----------------------+---------+-------+
|               col_name|data_type|comment|
+-----------------------+---------+-------+
|              FirstName|   string|   NULL|
|                 rollno|      int|   NULL|
|                    age|      int|   NULL|
|# Partition Information|         |       |
|             # col_name|data_type|comment|
|                    age|      int|   NULL|
+-----------------------+---------+-------+

ALTER TABLE StudentInfo ALTER COLUMN FirstName COMMENT "new comment";

-- After ALTER or CHANGE COLUMNS
DESC StudentInfo;
+-----------------------+---------+-----------+
|               col_name|data_type|    comment|
+-----------------------+---------+-----------+
|              FirstName|   string|new comment|
|                 rollno|      int|       NULL|
|                    age|      int|       NULL|
|# Partition Information|         |           |
|             # col_name|data_type|    comment|
|                    age|      int|       NULL|
+-----------------------+---------+-----------+

-- REPLACE COLUMNS
DESC StudentInfo;
+-----------------------+---------+-----------+
|               col_name|data_type|    comment|
+-----------------------+---------+-----------+
|              FirstName|   string|new comment|
|                 rollno|      int|       NULL|
|                    age|      int|       NULL|
|# Partition Information|         |           |
|             # col_name|data_type|    comment|
|                    age|      int|       NULL|
+-----------------------+---------+-----------+

ALTER TABLE StudentInfo REPLACE COLUMNS (name string, ID int COMMENT 'new comment');

-- After replacing COLUMNS
DESC StudentInfo;
+-----=---------+---------+-----------+
|       col_name|data_type|    comment|
+---------------+---------+-----------+
|           name|   string|       NULL|
|             ID|      int|new comment|
| # Partitioning|         |           |
|Not partitioned|         |           |
+---------------+---------+-----------+

-- Add a new partition to a table 
SHOW PARTITIONS StudentInfo;
+---------+
|partition|
+---------+
|   age=11|
|   age=12|
|   age=15|
+---------+

ALTER TABLE StudentInfo ADD IF NOT EXISTS PARTITION (age=18);

-- After adding a new partition to the table
SHOW PARTITIONS StudentInfo;
+---------+
|partition|
+---------+
|   age=11|
|   age=12|
|   age=15|
|   age=18|
+---------+

-- Drop a partition from the table 
SHOW PARTITIONS StudentInfo;
+---------+
|partition|
+---------+
|   age=11|
|   age=12|
|   age=15|
|   age=18|
+---------+

ALTER TABLE StudentInfo DROP IF EXISTS PARTITION (age=18);

-- After dropping the partition of the table
SHOW PARTITIONS StudentInfo;
+---------+
|partition|
+---------+
|   age=11|
|   age=12|
|   age=15|
+---------+

-- Adding multiple partitions to the table
SHOW PARTITIONS StudentInfo;
+---------+
|partition|
+---------+
|   age=11|
|   age=12|
|   age=15|
+---------+

ALTER TABLE StudentInfo ADD IF NOT EXISTS PARTITION (age=18) PARTITION (age=20);

-- After adding multiple partitions to the table
SHOW PARTITIONS StudentInfo;
+---------+
|partition|
+---------+
|   age=11|
|   age=12|
|   age=15|
|   age=18|
|   age=20|
+---------+

-- Change the fileformat
ALTER TABLE loc_orc SET fileformat orc;

ALTER TABLE p1 partition (month=2, day=2) SET fileformat parquet;

-- Change the file Location
ALTER TABLE dbx.tab1 PARTITION (a='1', b='2') SET LOCATION '/path/to/part/ways'

-- SET SERDE/ SERDE Properties
ALTER TABLE test_tab SET SERDE 'org.apache.hadoop.hive.serde2.columnar.LazyBinaryColumnarSerDe';

ALTER TABLE dbx.tab1 SET SERDE 'org.apache.hadoop' WITH SERDEPROPERTIES ('k' = 'v', 'kay' = 'vee')

-- SET TABLE PROPERTIES
ALTER TABLE dbx.tab1 SET TBLPROPERTIES ('winner' = 'loser');

-- SET TABLE COMMENT Using SET PROPERTIES
ALTER TABLE dbx.tab1 SET TBLPROPERTIES ('comment' = 'A table comment.');

-- Alter TABLE COMMENT Using SET PROPERTIES
ALTER TABLE dbx.tab1 SET TBLPROPERTIES ('comment' = 'This is a new comment.');

-- DROP TABLE PROPERTIES
ALTER TABLE dbx.tab1 UNSET TBLPROPERTIES ('winner');

-- RECOVER PARTITIONS
ALTER TABLE dbx.tab1 RECOVER PARTITIONS;
  • 15
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值