📅  最后修改于: 2023-12-03 15:15:32.039000             🧑  作者: Mango
在Hive中,表是数据的逻辑表示。表可以通过定义列、表名、分隔符等参数进行创建。本文将详细介绍如何在Hive中创建表。
CREATE [TEMPORARY] [EXTERNAL] TABLE [IF NOT EXISTS] table_name
[(col_name data_type [COMMENT col_comment], ...)]
[COMMENT table_comment]
[PARTITIONED BY (col_name data_type [COMMENT col_comment], ...)]
[CLUSTERED BY (col_name, col_name, ...) [SORTED BY (col_name [ASC|DESC]), ...]]
[INTO num_buckets BUCKETS]
[ROW FORMAT row_format]
[STORED AS file_format]
[LOCATION hdfs_path]
[TBLPROPERTIES (property_name=property_value, ...)]
下面是创建一个简单的表的示例:
CREATE TABLE IF NOT EXISTS employee (
employee_id INT,
name STRING,
salary FLOAT,
birthday TIMESTAMP
) COMMENT 'Employee details'
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
STORED AS TEXTFILE;
此代码片段创建名为employee
的表,该表具有employee_id
、name
、salary
、birthday
等四个字段,还定义行格式、分隔符等其他参数。
下面是创建分区表的示例:
CREATE EXTERNAL TABLE IF NOT EXISTS partitioned_employee (
employee_id INT,
name STRING,
salary FLOAT,
birthday TIMESTAMP
) COMMENT 'Employee details'
PARTITIONED BY (department STRING, city STRING)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
STORED AS TEXTFILE
LOCATION '/user/hive/warehouse/partitioned_employee';
此代码片段创建名为partitioned_employee
的表,该表还具有department
和city
两个分区字段,并且以外部表的形式存在。
本文介绍了在Hive中创建表的语法和参数。使用Hive,您可以轻松地创建适合您的数据需求的表。