hive只复制表结构不复制表数据

11 篇文章 0 订阅

目录

一、背景

二、准备测试数据

1.建表

2.造测试数据

三、操作

1.CTAS

(1).无分区表测试

(2).分区表测试

2.LIKE

(1).无分区表测试

(2).分区表测试


一、背景

有一张ori_table,想新建一张表new_table,保持跟ori_table一样的表结构,但是不复制ori_table的数据。

二、准备测试数据

1.建表

create table ori_table (
id   bigint,
col1 string,
col2 double
)comment '无分区原始表'
stored as orc;


create table ori_table_partition (
id   bigint,
col1 string,
col2 double
)comment '有分区原始表'
partitioned by (dt string)
stored as orc;

2.造测试数据

insert into ori_table values (1,'哈哈',10.02);
insert into ori_table values (2,'嘿嘿',20.06);
insert into ori_table values (3,'嘻嘻',30.08);

insert into ori_table_partition partition(dt='1') values (1,'哈哈',10.02);
insert into ori_table_partition partition(dt='2') values (2,'嘿嘿',20.06);
insert into ori_table_partition partition(dt='3') values (3,'嘻嘻',30.08);

三、操作

有2种办法

1.CTAS

(1).无分区表测试

--复制与ori_table表结构及表数据都一致的语句

create table new_table as select * from ori_table;


--只复制与ori_table表结构的语句
create table new_table as select * from ori_table where 1=0;

show create table new_table;   --执行看一下效果

 

select * from new_table;  --执行看一下数据量

 

由此得出结论,无分区表使用该语句可以满足需求!

(2).分区表测试

--复制与ori_table_partition表结构及表数据都一致的语句

create table new_table_partition as select * from ori_table_partition;


--只复制与ori_table表结构的语句
create table new_table_partition as select * from ori_table_partition where 1=0;

show create table new_table_partition;     --执行看一下效果

 

 这里看到表结构还是有些差异的,执行后将分区表建成了无分区表,分区字段创建成了普通字段。

select * from new_table_partition;   --执行看一下数据量

结论:

CTAS语法不适用于分区表

2.LIKE

(1).无分区表测试

--只复制与ori_table表结构的语句
create table new_table_like like ori_table;

show create table new_table_like;  --执行看一下效果

select * from new_table_like;   --执行看一下数据量

 

 

(2).分区表测试

--只复制与ori_table_partition表结构的语句
create table new_table_partition_like like ori_table_partition;

show create table new_table_partition_like; --执行看一下效果

 select * from new_table_partition_like;   --执行看一下数据量

结论:

LIKE语法适用于分区表及无分区表 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值