Hcatalog 简明教程

HCatalog - Alter Table

本章介绍如何修改表的属性,例如更改表名、更改列名、添加列以及删除或替换列。

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

您可以使用 ALTER TABLE 语句来修改 Hive 中的表。

You can use the ALTER TABLE statement 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
ALTER TABLE name CHANGE column_name new_name new_type
ALTER TABLE name REPLACE COLUMNS (col_spec[, col_spec ...])

下面介绍一些场景。

Some of the scenarios are explained below.

Rename To… Statement

以下查询将表 employee 重命名为 emp

The following query renames a table from employee to emp.

./hcat –e "ALTER TABLE employee RENAME TO emp;"

Change Statement

下表包含 employee 表的字段,并显示要更改的字段(以粗体显示)。

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 −

./hcat –e "ALTER TABLE employee CHANGE name ename String;"
./hcat –e "ALTER TABLE employee CHANGE salary salary Double;"

Add Columns Statement

以下查询向 employee 表中添加一个名为 dept 的列。

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

./hcat –e "ALTER TABLE employee ADD COLUMNS (dept STRING COMMENT 'Department name');"

Replace Statement

以下查询从 employee 表中删除所有列,并用 empname 列替换它们 −

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

./hcat – e "ALTER TABLE employee REPLACE COLUMNS ( eid INT empid Int, ename STRING name String);"

Drop Table Statement

本章介绍如何在 HCatalog 中删除表。当您从元存储中删除表时,它会删除表/列数据及其元数据。它可以是普通表(存储在元存储中)或外部表(存储在本地文件系统中);无论类型如何,HCatalog 都以相同的方式处理它们。

This chapter describes how to drop a table in HCatalog. When you drop a table from the metastore, it removes the table/column data and their metadata. It can be a normal table (stored in metastore) or an external table (stored in local file system); HCatalog treats both in the same manner, irrespective of their types.

其语法如下:

The syntax is as follows −

DROP TABLE [IF EXISTS] table_name;

以下查询删除名为 employee 的表格−

The following query drops a table named employee

./hcat –e "DROP TABLE IF EXISTS employee;"

成功执行查询后,你会看到以下响应−

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

OK
Time taken: 5.3 seconds