hive的分區表在新增字段後,新增字段的數據顯示NULL。hive分區表新增字段,新分區表字段能夠顯示,老的分區表字段無法顯示,在hdfs上顯示正常。
測試具體情況:
1、創建一個測試表
2、插入一條數據
insert into table cyx_test partition(dt='20180101') values('0101','java','shenzhen');
3、新增一個表字段(在原有字段後面,注意寫入數據順序)
alter table cyx_test add columns(postcode string comment '郵編');
insert into table cyx_test partition(dt='20180101') values ('0101a','python','guangzhou','0757'); # 寫入老分區
insert into table cyx_test partition(dt='20180202') values('0202','scala','zhuhai','0766'); # 寫入新分區
理論上第二條數據會顯示4個字段的信息(hdfs文件保存正常數據,但是hive查詢無法顯示,),而新增分區是在更改表結構後創建的,所以能正常顯示。
解決方法:(修改hive庫sds表)
update sds set cd_id=63365 where LOCATION like'hdfs://mycluster/user/hive/warehouse/xdata.db/cyx_test/dt%';
再次查詢可以顯示
最新的分區表的CD_ID會很原始表的一致,因此需要把老分區表的CD_ID更新過來。