如何通过在配置单元的分区 table 中选择另一列来覆盖列值

How to overwrite columns value by selecting another columns in partition table in hive

您好,如何通过 selecting 相同分区 table 在配置单元中覆盖列值。

我通过执行以下查询

创建了table
CREATE TABLE user (fname string,lname string) partitioned By (day int);

在将数据插入 table 之后,我插入数据。 我执行了 select 查询,如下所示:

AA  AA  20170201
BB  BB  20170201
CC  CC  20170201
DD  DD  20170202
EE  EE  20170203

根据我的要求,我想在我添加的以下查询的帮助下向我的 table(user) 添加一列。

ALTER TABLE user ADD COLUMNS ( day2 int);

添加列后,我的 table 如下所示

AA  AA  NULL    20170201
BB  BB  NULL    20170201
CC  CC  NULL    20170201
DD  DD  NULL    20170202
EE  EE  NULL    20170203

但我想要table喜欢。

AA  AA  20170201    20170202
BB  BB  20170201    20170202
CC  CC  20170201    20170202
DD  DD  20170202    20170202
EE  EE  20170203    20170203

所以我执行了下面的查询

insert overwrite table user partition (day)
select
    fname,
    lname,
    day as day2,
    case 
        when day <= 20170202 then 20170202
        when day > 20170202 then day
    end as day
from user;

然后我执行了 select 如下查询

select * from user;

结果是:

AA  AA  NULL    20170201
BB  BB  NULL    20170201
CC  CC  NULL    20170201
AA  AA  NULL    20170202
BB  BB  NULL    20170202
CC  CC  NULL    20170202
DD  DD  NULL    20170202
EE  EE  NULL    20170203

为什么我得到空 values.Can 请让我知道我错过了什么,让我知道如何实现 this.i.e

   AA   AA  20170201    20170202
    BB  BB  20170201    20170202
    CC  CC  20170201    20170202
    DD  DD  20170202    20170202
    EE  EE  20170203    20170203

Following the behaviour of partitioned Hive Table, HiVE's schema definitions are maintained at partition level.So older partition may not have received schema updates.

详细查询

hive> drop table test_insert;
OK
hive> CREATE TABLE test_insert (fname string,lname string) partitioned By (day int);
OK
hive> insert into test_insert partition (day=20170202) values('f','l');
OK
hive> insert into test_insert partition (day=20170203) values('f','l');
OK
hive> select * from test_insert;
OK
f   l   20170202
f   l   20170203
Time taken: 0.148 seconds, Fetched: 2 row(s)
hive> ALTER TABLE test_insert ADD COLUMNS ( day2 int);
OK
Time taken: 0.193 seconds
hive> select * from test_insert;
OK
   f    l   NULL    20170202
   f    l   NULL    20170203

此时,请注意分区 table 的 架构在分区级别维护 并且它应该遵循分区架构而不是 table架构。

请注意此处各列中的差异。 ALTER 没有更改现有分区的列定义。

  hive> describe formatted test_insert;
    OK
    # col_name              data_type               comment             

    fname                   string                                      
    lname                   string                                      
    day2                    int                                         

    # Partition Information      
    # col_name              data_type               comment             

    day                     int    

    hive> describe formatted test_insert partition (day=20170202);
    OK
    # col_name              data_type               comment             

    fname                   string                                      
    lname                   string                                      

    # Partition Information      
    # col_name              data_type               comment             

    day                     int                           

已调整您的上述查询以创建新分区,以便模式将从 table 复制到新创建的分区。

 hive> insert overwrite table test_insert partition (day)
        > select
        >     fname,
        >     lname,
        >     day as day2,
        >     case 
        >         when day <= 20170202 then 19999999
        >         when day > 20170202 then day
        >     end as day
        > from test_insert;


    hive> select * from test_insert;
    OK
    f   l   20170202    19999999
    f   l   NULL    20170202
    f   l   NULL    20170203
    Time taken: 0.224 seconds, Fetched: 3 row(s)

希望这是清楚的。! 如果您想更改所有现有分区的架构,请尝试使用以下命令,然后尝试插入。

ALTER TABLE test_insert  partition(day) ADD COLUMNS ( day2 string);