【问题标题】:Create hive table using "as select" or "like" and also specify delimiter使用“as select”或“like”创建配置单元表并指定分隔符
【发布时间】:2020-10-19 10:59:19
【问题描述】:

是否可以做一个

create table <mytable> as select <query statement>

使用

row format delimited fields terminated by '|';

或者做一个

create table <mytable> like <other_table> row format delimited fields terminated by '|';

语言手册似乎没有表明......但我过去已经做到了这一点。

【问题讨论】:

    标签: hive sql-like create-table


    【解决方案1】:

    在 Hive 中可以选择 Create Table as select (CTAS)。

    你可以试试下面的命令:

    CREATE TABLE new_test 
        row format delimited 
        fields terminated by '|' 
        STORED AS RCFile 
    AS select * from source where col=1
    
    1. 目标不能是分区表。
    2. 目标不能是外部表。
    3. 它复制结构和数据

    在 Hive 中也可以创建类似的表。

    1. 它只是复制源表定义。

    【讨论】:

    • 很好的答案!解决 (a) 特定语法和 (b) 限制。正是我想要的。
    • @Venkatesh,如果我在CREATE TABLE <table_name> AS SELECT <some query from target table> 的情况下没有提及,默认字段终止符/分隔符将是什么。它会使用目标表的字段分隔符还是一些默认的字段分隔符?
    • @Manindar 默认配置单元分隔符是 ^A(ctrl-a)
    【解决方案2】:

    假设我们有一个名为employee的外部表

    hive> SHOW CREATE TABLE employee;
    OK
    CREATE EXTERNAL TABLE employee(
      id string,
      fname string,
      lname string, 
      salary double)
    ROW FORMAT SERDE
      'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe'
    WITH SERDEPROPERTIES (
      'colelction.delim'=':',
      'field.delim'=',',
      'line.delim'='\n',
      'serialization.format'=',')
    STORED AS INPUTFORMAT
      'org.apache.hadoop.mapred.TextInputFormat'
    OUTPUTFORMAT
      'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
    LOCATION
      'maprfs:/user/hadoop/data/employee'
    TBLPROPERTIES (
      'COLUMN_STATS_ACCURATE'='false',
      'numFiles'='0',
      'numRows'='-1',
      'rawDataSize'='-1',
      'totalSize'='0',
      'transient_lastDdlTime'='1487884795')
    
    1. 创建person 表,如employee

      CREATE TABLE person LIKE employee;

    2. 创建person 外部表,如employee

      CREATE TABLE person LIKE employee LOCATION 'maprfs:/user/hadoop/data/person';

    3. 然后使用 DESC person; 查看新创建的表架构。

    【讨论】:

    • 我试过了,但 TBLPROPERTIES 没有被复制。无论如何都要修复它。
    【解决方案3】:

    上面提供的两个答案都可以正常工作。

    1. CREATE TABLE person AS select * from employee;
    2. CREATE TABLE person LIKE employee;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-26
      • 1970-01-01
      • 1970-01-01
      • 2014-12-08
      • 2013-07-12
      • 1970-01-01
      • 2011-12-24
      • 1970-01-01
      相关资源
      最近更新 更多