Hive分区表

分区表实际上就是对应一个 HDFS 文件系统上的独立的文件夹,该文件夹下是该分区 所有的数据文件。Hive 中的分区就是分目录,把一个大的数据集根据业务需要分割成小的 数据集。在查询时通过 WHERE 子句中的表达式选择查询所需要的指定的分区,这样的查 询效率会提高很多。

创建分区表语法

查询分区表中数据

增加分区

删除分区

同时删除多个分区

查看分区表有多少分区

查看分区表结构

创建二级分区表

加载数据到二级分区表中

查询分区数据

把数据直接上传到分区目录上,让分区表和数据产生关联的三种方式

方式一:上传数据后修复

上传数据

dfs -mkdir -p /user/hive/warehouse/dept_partition2/month=201709/day=12;
dfs -put /home/hadoop/data/dept.txt /user/hive/warehouse/dept_partition2/month=201709/day=12;

查询数据(查询不到刚上传的数据)

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

执行修复命令

msck repair table dept_partition2;

再次查询数据

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

方式二:上传数据后添加分区

上传数据

dfs -mkdir -p /user/hive/warehouse/dept_partition2/month=201709/day=11;

dfs -put /home/hadoop/data/dept.txt /user/hive/warehouse/dept_partition2/month=201709/day=11;

执行添加分区

alter table dept_partition2 add partition(month='201709',day='11');

查询数据

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

方式三:创建文件夹后 load 数据到分区

创建目录

dfs -mkdir -p /user/hive/warehouse/dept_partition2/month=201709/day=10;

上传数据

load data local inpath '/home/hadoop/data/dept.txt' into table dept_partition2 partition(month='201709',day='10');

查询数据

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

修改表

重命名表

ALTER TABLE table_name RENAME TO new_table_name

实操案例

alter table dept_partition2 rename to dept_partition3;

增加、修改和删除表分区

添加列

alter table dept_partition add columns(deptdesc string);

更新列

alter table dept_partition change column deptdesc desc int;

替换列

alter table dept_partition replace columns(deptno string, dname string, loc string);

删除表

drop table dept_partition;

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值