温馨提示×

hive create怎样实现

小樊
81
2024-12-19 22:40:41
栏目: 大数据

Hive中的CREATE语句用于创建表、分区表、外部表等

  1. 创建普通表:
CREATE TABLE table_name (
    column1 data_type,
    column2 data_type,
    ...
)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY 'delimiter'
STORED AS file_format;

示例:

CREATE TABLE employees (
    id INT,
    name STRING,
    age INT
)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','
STORED AS TEXTFILE;
  1. 创建分区表:
CREATE TABLE table_name (
    column1 data_type,
    column2 data_type,
    ...
)
PARTITIONED BY (partition_column1 data_type, partition_column2 data_type, ...)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY 'delimiter'
STORED AS file_format;

示例:

CREATE TABLE sales (
    order_id INT,
    product_id INT,
    customer_id INT,
    quantity INT,
    price FLOAT
)
PARTITIONED BY (order_date STRING)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','
STORED AS TEXTFILE;
  1. 创建外部表:
CREATE EXTERNAL TABLE table_name (
    column1 data_type,
    column2 data_type,
    ...
)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY 'delimiter'
STORED AS file_format
LOCATION 'hdfs_path';

示例:

CREATE EXTERNAL TABLE customer_data (
    id INT,
    name STRING,
    age INT
)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','
STORED AS TEXTFILE
LOCATION 'hdfs://localhost:9000/user/customer_data';

请注意,这些示例中的data_typedelimiterfile_format需要根据实际需求进行替换。在实际使用中,还需要考虑Hive的元数据存储(通常是HDFS)的可用性和配置。

0