今天在數(shù)據(jù)脫敏的工作中碰到一個需求:
將數(shù)據(jù)倉庫中的用戶畫像的phone字段以及該字段下的數(shù)據(jù)全部刪除
1. 錯誤示范:impala里面直接刪除該列
ALTER TABLE name DROP [COLUMN] column_name
如
ALTER TABLE dmr.edu_user_profile DROP COLUMN user_phone
然后查看表,發(fā)現(xiàn)該列的字段確實(shí)被刪除了,但是該列數(shù)據(jù)還在,相當(dāng)于整個表名發(fā)生了左移,數(shù)據(jù)錯位了
2. 重新添加該列,并移動該列到原先的位置
首先,
-
impala和hive并不支持直接在指定列前增加新的列(mysql可以) -
impala并不支持列移動。
即,只能用hive先添加列,然后將列移動到指定位置,
1)添加列
ALTER TABLE name ADD COLUMNS (col_spec[, col_spec ...])
ALTER TABLE dmr.edu_user_profile ADD COLUMNS(user_phone STRING);
2)修改指定列
ALTER TABLE name CHANGE column_name new_name new_type
ALTER TABLE dmr.edu_user_profile CHANGE user_phone user_phone STRING AFTER user_name;
注意在impala中after clolumn這部分是執(zhí)行不了的。
查看表,發(fā)現(xiàn)在hive中數(shù)據(jù)的內(nèi)容和字段確實(shí)添加了,也對齊了,但是在impala中數(shù)據(jù)新添加的列仍然在末尾,該字段并沒有在應(yīng)該出現(xiàn)的位置
3. 另辟蹊徑
在hive中利用原表創(chuàng)建一個新的tmp表,然后取數(shù)據(jù)的時候不select phone這一字段,再將原表drop掉,用類似的方法select來創(chuàng)建原表即可
1)創(chuàng)建臨時表并用原表數(shù)據(jù)填充,不添加select字段
create edu_user_profile_tmp as
select id,name,dt
from edu_user_profile
但是這樣創(chuàng)建的表是沒有分區(qū)的,所以需要在插入回原表的時候按照動態(tài)分區(qū)的方式添加。
又因?yàn)閑du_user_profile 是以dt為分區(qū)的分區(qū)表,所以重新創(chuàng)建
2)drop掉原表
drop table edu_user_profile
3)重新創(chuàng)建edu_user_profile分區(qū)表,但是phone字段取消
create edu_user_profile (
id bigint,
name string
)
partitioned by (dt string)
4) 按照動態(tài)分區(qū)的方式從tmp表插入數(shù)據(jù)
insert into table edu_user_profile partition(dt)
select id,name,dt
from edu_user_profile_tmp
最后,感謝驕姐救我,捂臉哭~!