Hive创建分区表常用指令

这篇博客介绍了Hive中对分区表的操作,包括创建、加载数据、查询、多表联合查询、增加和删除分区,以及查看分区数量。还涉及到二级分区表的创建与数据加载,以及如何在HDFS上进行数据管理并与Hive元数据同步。最后讨论了在上传数据后如何修复和添加分区,确保数据可查询。
摘要由CSDN通过智能技术生成

1、创建分区表

create table dept_partition(
deptno int, dname string, loc string
)
partitioned by (month string)
row format delimited fields terminated by '\t';

2、加载数据到分区表中

load data local inpath '/opt/module/datas/dept.txt' into table dept_partition partition(month='201709');

3、查询分区表中数据

select * from dept_partition where month='201709';

4、多表联合查询

select * from dept_partition where month='201709'
union
select * from dept_partition where month='201708'
union 
select * from dept_partition where month='201707';

5、增加分区

alter table dept_partition add partition(month='201706');

6、同时增加多个分区

alter table dept_partition add partition(month='201705') partition(month='201704');

7、删除分区

alter table dept_partition drop partition(month='201704');

8、同时删除多个分区

alter table dept_partition drop partition(month='201705'),partition (month='201706');

9、查看分区表有多少分区

show partitions dept_partition;

10、创建二级分区表

create table dept_partition2(
deptno int,dname string,loc string
)
partitioned by (month string,day string)
row format delimited fields terminated by '\t';

11、加载数据到二级分区

load data local inpath '/opt/module/data/text' into table dept_partition2 partition(month='201709',day='13');

12、查询分区数据

select * from dept_partition2 where month='201709' and day='13';

13、分区表和数据管关联

如果首先用hadoop fs -mkdri 创建分区文件夹,然后-put文件,不会查询到,原因是因为mysql上没有元数据信息,alter添加分区和load data会自动创建元数据
不一定是分区表,普通的表也同理

hadoop fs -mkdir /user/hive/warehouse/stu3
hadoop fs -put /opt/data/test /user/hive/warehouse/stu3

这样查询表不会查询到数据,而且表也没有,因为在Mysql上没有元数据信息
只需要重新建立这张表就好

create table if not exists(
id int);

这就会将元数据与现在的文件夹与数据相关联

14、上传数据后修复

msck repair table dept_pattition2;

15、上传数据后添加分区

# 相当于上面的建表操作,都是为了在Mysql上有元数据
alter table dept_pattition2 add partition(month='201709',day='11');

16、创建文件夹后load数据到分区

# load data会自动关联元数据
load data local inpath '/opt/module/data/test' into table dept_partition2 partition(month='201709',day='10');
  • 2
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

海洋 之心

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值