2017-02-09 6 views
1

ハイブで同じパーティションテーブルを選択して列値を上書きする方法。ハイブでパーティションテーブル内の別の列を選択して列値を上書きする方法

私は、クエリ

CREATE TABLE user (fname string,lname string) partitioned By (day int); 

の下に実行してテーブルを作成していると私は、テーブルにデータを挿入した後、データを挿入します。 私はそれが以下のように見える選択クエリを実行する:私の要件ごとに

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

を、私は追加のクエリ以下の助けを借りて、私のテーブルに1つの以上の列(ユーザー)を追加します。列を追加した後

ALTER TABLE user ADD COLUMNS (day2 int); 

、私のテーブルは

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

以下のように見える。しかし、私は次のようにテーブルをしたいです。

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 

私はnull値を取得しています理由:

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

は、だから私は、私は結果がある

select * from user; 

の下のような選択クエリを実行したクエリの下

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; 

を実行しました私に教えてください。私は逃した何かを知って、パーティションハイブ表の振る舞い後this.i.e

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

答えて

0

を達成するためにどのようにしましょう、ハイブのスキーマ 定義は、パーティションで維持されているlevel.So古いパーティションは、スキーマの更新を受け取っていない可能性があります。この時点で

詳細なクエリ

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 

、パーティションテーブルのスキーマは、パーティションレベルに維持され、それが名誉パーティションスキーマではなく、テーブルのスキーマ予想だという点に注意してください。

ここの列の相違点に注目してください。 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       

上記のクエリを調整して、新しいパーティションを作成し、スキーマをテーブルから新しく作成されたパーティションにコピーします。

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) 

これは明らかです。! 既存のすべてのパーティションのスキーマを変更するには、以下のコマンドを試してから、insertを試してください。

ALTER TABLE test_insert partition(day) ADD COLUMNS (day2 string); 
関連する問題