当前位置: 首页 > news >正文

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

目录

一、背景

二、准备测试数据

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语法适用于分区表及无分区表 

http://www.lryc.cn/news/18066.html

相关文章:

  • 如何将Linux的NIC 名称更改为 eth0 而不是 enps33 或 enp0s25,只要几秒钟
  • 位运算笔记
  • 2023全国首个区块链平台发布,区块链绿色消费积分系统玩法悄然上市
  • 【异常】因为忘加了租户查询条件,导致重复ID导入失败Duplicate entry ‘XXX‘ for key ‘PRIMARY‘
  • 证明CPU指令是乱序执行的
  • css 属性和属性值的定义
  • Python获取中国大学MOOC某课程评论及其参与人数
  • 【C++】类和对象(完结篇)
  • 低代码开发可以解决哪些问题?
  • Linux 中使用 docker-compose 部署 MongoDB 6 以上版本副本集及配置 SSL / TLS 协议
  • JavaWeb--Mybatis练习
  • Springer-MTA期刊上传Latex要求
  • Graph Embedding基础 图表示学习 什么是Graph Embedding
  • 某直聘tp_token解析
  • 替代启攀微8按键触控八通道触摸芯片-GTC08L
  • Zabbix“专家坐诊”第182期问答汇总
  • PHP、Nginx、openssl ECC证书搭建
  • 秒杀服务------技术点及亮点
  • 【Python数据挖掘入门】一、数据挖掘概况
  • 【python】anaconda 管理 python 环境
  • 线上插画培训班有用吗,教你选靠谱的插画课程
  • 吃鸡用什么蓝牙耳机效果好?手游吃鸡公认最好的几款蓝牙耳机
  • 四个步骤在CRM系统中设置游戏化机制
  • 2023年TikTok营销如何破局?品牌应做好这6点
  • 2023年CDGA考试-第5章-数据建模和设计(含答案)
  • 蓝桥杯入门即劝退(二十)快乐数(我不快乐了)
  • Aspose.Imaging for .NET V23
  • 通信算法复习题纲
  • 交叉编译 MQTT/Mosquitto
  • 无重复字符的最长子串的解法