Hive-DDL

作者: MR_ChanHwang | 来源:发表于2018-09-13 16:10 被阅读0次

    创建表

    ROW FORMAT

    目标文本格式

    1,xiaoming,book-tv-code,beijing:chaoyang-shanghai:pudong
    2,lilei,book-code,huoxing:xxx-shanghai:pudong
    3,hanmeimei,lilei-shop,huoxing:xxx
    

    第三列与第四列都是短线-分隔

    第三列为数组;第四列为Map

    [LINE TERMINATED by char] 行间分隔,默认换行符

    CREATE TABLE psn1 (
    id int,
    name string,
    likes ARRAY <string>,
    address MAP <string,string>
    )
    ROW FORMAT DELIMITED
    FIELDS TERMINATED BY ','
    COLLECTION ITEMS TERMINATED BY '-'
    MAP KEYS TERMINATED BY  ':';
    

    载入数据

    ~目录下新建data文件,文件中内容如下:

    1,xiaoming,book-tv-code,beijing:chaoyang-shanghai:pudong
    2,lilei,book-code,huoxing:xxx-shanghai:pudong
    3,hanmeimei,lilei-shop,huoxing:xxx
    

    将数据载入hive

    load data local inpath '/root/data' into table psn1;
    

    创建外部表

    指定表类型为外部表,同时指定存储在HDFS的目录

    CREATE EXTERNAL TABLE psn2 (
    id int,
    name string,
    likes ARRAY <string>,
    address MAP <string,string>
    )
    ROW FORMAT DELIMITED
    FIELDS TERMINATED BY ','
    COLLECTION ITEMS TERMINATED BY '-'
    MAP KEYS TERMINATED BY  ':'
    LOCATION '/user/psn2';
    

    删表

    drop table psn1;
    drop table psn2;
    

    psn1为内部表;psn2为外部表。

    外部表会指定数据存放的目,添加external关键字。

    若删除,内部表数据会丢失,外部表仅删除元数据,不会删除数据。

    CREATE TABLE AS SELECT

    初始化:创建psn1并且载入数据。

    CREATE TABLE psn1 (
    id int,
    name string,
    likes ARRAY <string>,
    address MAP <string,string>
    )
    ROW FORMAT DELIMITED
    FIELDS TERMINATED BY ','
    COLLECTION ITEMS TERMINATED BY '-'
    MAP KEYS TERMINATED BY  ':';
    
    load data local inpath '/root/data' into table psn1;
    

    创建psn3

    CREATE 
     TABLE psn3 
        AS
    SELECT id
         , name
         , likes
         , address
      FROM psn1;
    

    一共做了两件事:1.根据原有结构创建表;2. 载入原有表数据入新表。

    CRATE TABLE [t1] LIKE [t2]

    CREATE
     TABLE psn4
      LIKE psn1;
    

    复制创建表结构

    Hive建表create table的DDL

    CREATE [TEMPORARY] [EXTERNAL] TABLE [IF NOT EXISTS] [db_name.]table_name    -- (Note: TEMPORARY available in Hive 0.14.0 and later)
      [(col_name data_type [COMMENT col_comment], ... [constraint_specification])]
      [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]
      [SKEWED BY (col_name, col_name, ...)                  -- (Note: Available in Hive 0.10.0 and later)]
         ON ((col_value, col_value, ...), (col_value, col_value, ...), ...)
         [STORED AS DIRECTORIES]
      [
       [ROW FORMAT row_format] 
       [STORED AS file_format]  -- 压缩
         | STORED BY 'storage.handler.class.name' [WITH SERDEPROPERTIES (...)]  -- (Note: Available in Hive 0.6.0 and later)
      ]
      [LOCATION hdfs_path]
      [TBLPROPERTIES (property_name=property_value, ...)]   -- (Note: Available in Hive 0.6.0 and later)
      [AS select_statement];   -- (Note: Available in Hive 0.5.0 and later; not supported for external tables)
     
    CREATE [TEMPORARY] [EXTERNAL] TABLE [IF NOT EXISTS] [db_name.]table_name
      LIKE existing_table_or_view_name
      [LOCATION hdfs_path];
     
    data_type
      : primitive_type
      | array_type
      | map_type
      | struct_type
      | union_type  -- (Note: Available in Hive 0.7.0 and later)
     
    primitive_type
      : TINYINT
      | SMALLINT
      | INT
      | BIGINT
      | BOOLEAN
      | FLOAT
      | DOUBLE
      | DOUBLE PRECISION -- (Note: Available in Hive 2.2.0 and later)
      | STRING
      | BINARY      -- (Note: Available in Hive 0.8.0 and later)
      | TIMESTAMP   -- (Note: Available in Hive 0.8.0 and later)
      | DECIMAL     -- (Note: Available in Hive 0.11.0 and later)
      | DECIMAL(precision, scale)  -- (Note: Available in Hive 0.13.0 and later)
      | DATE        -- (Note: Available in Hive 0.12.0 and later)
      | VARCHAR     -- (Note: Available in Hive 0.12.0 and later)
      | CHAR        -- (Note: Available in Hive 0.13.0 and later)
     
    array_type
      : ARRAY < data_type >
     
    map_type
      : MAP < primitive_type, data_type >
     
    struct_type
      : STRUCT < col_name : data_type [COMMENT col_comment], ...>
     
    union_type
       : UNIONTYPE < data_type, data_type, ... >  -- (Note: Available in Hive 0.7.0 and later)
     
    row_format
      : DELIMITED [FIELDS TERMINATED BY char [ESCAPED BY char]] [COLLECTION ITEMS TERMINATED BY char]
            [MAP KEYS TERMINATED BY char] [LINES TERMINATED BY char]
            [NULL DEFINED AS char]   -- (Note: Available in Hive 0.13 and later)
      | SERDE serde_name [WITH SERDEPROPERTIES (property_name=property_value, property_name=property_value, ...)]
     
    file_format:
      : SEQUENCEFILE
      | TEXTFILE    -- (Default, depending on hive.default.fileformat configuration)
      | RCFILE      -- (Note: Available in Hive 0.6.0 and later)
      | ORC         -- (Note: Available in Hive 0.11.0 and later)
      | PARQUET     -- (Note: Available in Hive 0.13.0 and later)
      | AVRO        -- (Note: Available in Hive 0.14.0 and later)
      | JSONFILE    -- (Note: Available in Hive 4.0.0 and later)
      | INPUTFORMAT input_format_classname OUTPUTFORMAT output_format_classname
     
    constraint_specification:
      : [, PRIMARY KEY (col_name, ...) DISABLE NOVALIDATE ]
        [, CONSTRAINT constraint_name FOREIGN KEY (col_name, ...) REFERENCES table_name(col_name, ...) DISABLE NOVALIDATE 
    

    相关文章

      网友评论

          本文标题:Hive-DDL

          本文链接:https://www.haomeiwen.com/subject/rlawgftx.html