作者:手机用户2602914827 | 来源:互联网 | 2024-12-04 14:16
Hive中的表创建语句允许用户定义数据结构、存储位置、文件格式等关键信息。以下是创建表的基本语法及其详细说明:
1 CREATE [TEMPORARY] [EXTERNAL] TABLE [IF NOT EXISTS] [database_name.]table_name
2 [(column_name data_type [column_constraint_specification] [COMMENT column_comment], ... [constraint_specification])]
3 [COMMENT table_comment]
4 [PARTITIONED BY (column_name data_type [COMMENT column_comment], ...)]
5 [CLUSTERED BY (column_name, column_name, ...) [SORTED BY (column_name [ASC|DESC], ...)] INTO number_of_buckets BUCKETS]
6 [SKEWED BY (column_name, column_name, ...) ON ((column_value, column_value, ...), (column_value, column_value, ...), ...)
7 [STORED AS DIRECTORIES]]
8 [
9 [ROW FORMAT row_format]
10 [STORED AS file_format]
11 | STORED BY 'storage.handler.class.name' [WITH SERDEPROPERTIES (...)]
12 ]
13 [LOCATION 'hdfs_path']
14 [TBLPROPERTIES (property_name=property_value, ...)]
15 [AS select_statement];
16
17 CREATE [TEMPORARY] [EXTERNAL] TABLE [IF NOT EXISTS] [database_name.]table_name
18 LIKE existing_table_or_view_name
19 [LOCATION 'hdfs_path'];
其中,data_type 可以是基本类型或复杂类型,具体如下:
- Primitive Types: 包括 TINYINT, SMALLINT, INT, BIGINT, BOOLEAN, FLOAT, DOUBLE, STRING, BINARY, TIMESTAMP, DECIMAL, DATE, VARCHAR, CHAR。
- Complex Types: 包括 ARRAY, MAP, STRUCT, UNIONTYPE。
row_format 定义了行的格式,支持 DELIMITED 和 SERDE 两种方式。DELIMITED 方式用于指定字段分隔符、集合项分隔符、映射键分隔符、行分隔符等;SERDE 方式则通过自定义序列化/反序列化类来处理数据格式。
file_format 指定了表的数据存储格式,包括 SEQUENCEFILE, TEXTFILE, RCFILE, ORC, PARQUET, AVRO, JSONFILE 等。不同的文件格式适用于不同类型的数据处理需求。
column_constraint_specification 和 constraint_specification 允许为列定义约束,如主键、唯一性、非空、默认值等,这些约束有助于确保数据的一致性和完整性。