全国好的深圳网站设计,建筑人才网首页,网站建设公司如何开拓客户,网站系统的设计与实现结论
alter table xxx add columns 时加上 cascade 时#xff0c;会把所有的分区都加上此字段。如果不加则只有新的分区会加上此字段#xff0c;旧的分区没有此字段#xff0c;即便数据文件里有对应的数据#xff0c;也不能显示内容。
如果分区都是 insert overwrite 生成…结论
alter table xxx add columns 时加上 cascade 时会把所有的分区都加上此字段。如果不加则只有新的分区会加上此字段旧的分区没有此字段即便数据文件里有对应的数据也不能显示内容。
如果分区都是 insert overwrite 生成的并且旧分区的数据不再重新生成可以在 add columns 不用cascade这样旧的分区对应的列显示 null。新的分区正常显示新增的列。如果分区都是 insert overwrite 生成的并且旧分区的数据需要重新生成。两种方案1. 可以在 add columns 不用 cascade。然后每个分区先执行 drop partition然后再执行 insert overwrite。2.可以在 add columns 使用 cascade然后再执行 insert overwrite。如果方案2报错则只能使用方案1.如果文件是从外部生成然后放到对应分区位置上并且文件里已经有要加的字段数据。需要使用 cascade。如果使用 cascade 报错那么看表是否是外部表。如果不是外部表则先转成外部表。如果是外部表则直接 drop partition 然后再 add partition location 增加分区。如果文件是从外部生成然后放到对应分区位置上并且文件里没有要加的字段数据。不需要使用 cascade。旧的分区对应的列显示 null。新的分区正常显示新增的列。
注
判断表是否是外部表使用 ‘show create table xxx’, 如果生成的是 ‘CREATE TABLE’ 是内部表如果是 CREATE EXTERNAL TABLE 是外部表。把表从外部表转成内部表 ALTER TABLE xxx SET TBLPROPERTIES(EXTERNALFALSE);把表从内部表转成外部表 ALTER TABLE table SET TBLPROPERTIES(EXTERNALTRUE);
测试 cascade 的作用
准备文件 data.txt
key1,value1
key2,value2测试 no cascade
create table t_no_cascade(c1 string) partitioned by (pt string) row format delimited
FIELDS TERMINATED BY , stored as textfile;增加分区 pt1
load data local inpath data.txt overwrite into table t_no_cascade partition(pt1);检索结果显示 c1 和 pt 字段。
select * from t_no_cascade where pt1;
OK
t_no_cascade.c1 t_no_cascade.pt
key1 1
key2 1增加字段
alter table t_no_cascade add columns(c2 string) ;再次检索分区pt1因为分区没有定义 c2, 所以 c2 为 null。
select * from t_no_cascade where pt1;
OK
t_no_cascade.c1 t_no_cascade.c2 t_no_cascade.pt
key1 NULL 1
key2 NULL 1增加新分区 pt2 新增的字段对新增的分区有效。
load data local inpath data.txt overwrite into table t_no_cascade partition(pt2);检索分区 因为新分区是表
select * from t_no_cascade where pt2;
OK
t_no_cascade.c1 t_no_cascade.c2 t_no_cascade.pt
key1 value1 2
key2 value2 2重新复写老分区 重新复写老分区不能看到新的列数据。如果需要可以先删除老分区再使用 insert overwrite。
insert overwrite table t_no_cascade partition(pt1) select c1,c2 from t_no_cascade where pt2;insert overwrite table xxx partition 还是使用之前的 partition id所以此分区还是没有新的字段。
select * from t_no_cascade where pt1;
OK
t_no_cascade.c1 t_no_cascade.c2 t_no_cascade.pt
key1 NULL 1
key2 NULL 1drop 老分区再使用 insert overwrite drop 老分区再使用 insert overwrite可以看到新的字段。
alter table t_no_cascade drop partition(pt1);
insert overwrite table t_no_cascade partition(pt1) select c1,c2 from t_no_cascade where pt2;这时的 partition(pt1) 是新的分区id这时可以看到新的数据。
select * from t_no_cascade where pt1;
OK
t_no_cascade.c1 t_no_cascade.c2 t_no_cascade.pt
key1 value1 1
key2 value2 12. 测试 cascade
create table t_cascade(c1 string) partitioned by (pt string) row format delimited
FIELDS TERMINATED BY , stored as textfile;增加分区 pt1
load data local inpath data.txt overwrite into table t_cascade partition(pt1);检索结果显示 c1 和 pt 字段。
select * from t_cascade where pt1;
OK
t_cascade.c1 t_cascade.pt
key1 1
key2 1增加字段 使用 cascade 递归的给各分区增加上字段。
alter table t_cascade add columns(c2 string) cascade;再次检索分区pt1。 cascade 后老的分区也加上了字段。 select * from t_cascade where pt1;
OK
t_cascade.c1 t_cascade.c2 t_cascade.pt
key1 value1 1
key2 value2 1