hive笔记(六):分区表和分桶表-分区/二级分区/抽样查询/动态分区调整

目录

分区表和分桶表

分区表

分区表基本操作

二级分区

动态分区调整

分桶表

抽样查询


分区表和分桶表

分区表

分区表实际上就是对应一个HDFS文件系统上的独立的文件夹,该文件夹下是该分区所有的数据文件。

Hive中的分区就是分目录,把一个大的数据集根据业务需要分割成小的数据集。在查询时通过WHERE子句中的表达式选择查询所需要的指定分区,这样的查询效率会提高很多。

分区表基本操作

(1)创建分区表

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

(2)准备数据

1)dept_20220826.log

10 ACCOUNTING 1700
20 RESEARCH 1800

2)dept_20220827.log

30 SALES 1900
40 OPERATIONS 1700

3)dept_20220828.log

50 TEST 2000
60 DEV 1900

(3)导入数据

load data local inpath '/root/datas/partition/dept_20220826.log'
into table dept_partition partition(day='20220826');


load data local inpath '/root/datas/partition/dept_20220827.log'
into table dept_partition partition(day='20220827')

load data local inpath '/root/datas/partition/dept_20220828.log'
into table dept_partition partition(day='20220828');

 注:分区表加载数据时必须指定分区。

(4)查看数据

(5)增加分区

#创建单个分区
alter table dept_partition add partition(day='20220829');

#创建多个分区
alter table dept_partition add partition(day='20220830') 
partition(day='20220831');

 (6)删除分区

#删除单个分区
alter table dept_partition drop partition(day='20220829');

#删除多个分区
alter table dept_partition drop partition(day='20220830'),
partition(day='20220831');

(7)查看分区

(8)查看分区表结构

二级分区

 (1)创建二级分区表

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

(2)加载数据

load data local inpath '/root/datas/partition/dept_20220826.log'
into table dept_partition2 partition(day='20220826',hour='2');

(3)查看数据

 (4)数据上传到分区目录,分区表和数据产生关联的方式

1)一:上传数据后恢复

上传数据

dfs -mkdir -p /user/hive/warehouse/test.db/dept_partition2/day=20220826/hour=2;

dfs -put /root/datas/partition/dept_20220826.log
/user/hive/warehouse/test.db/dept_partition2/day=20220826/hour=2

修复

msck repair table dept_partition2;

查询

select * from dept_partition2 where day='20220826' and hour='2';

2)二:上传数据后添加分区

上传数据

dfs -mkdir -p /user/hive/warehouse/test.db/dept_partition2/day=20220826/hour=2;

dfs -put /root/datas/partition/dept_20220826.log
/user/hive/warehouse/test.db/dept_partition2/day=20220826/hour=2

添加分区

alter table dept_partition2 add partition(day='20220826',hour='2')

查询

select * from dept_partition2 where day='20220826' and hour='2';

3)三:创建文件夹后添加分区

创建目录

dfs -mkdir -p /user/hive/warehouse/test.db/dept_partition2/day=20220826/hour=2;

上传数据

load data local inpath '/root/datas/partition/dept_20220826.log'
into table dept_partition2 partition(day='20220826',hour='2');

查询

select * from dept_partition2 where day='20220826' and hour='2';

动态分区调整

关系型数据库中,对分区表Insert数据时候,数据库自动会根据分区字段的值,将数据插入到相应的分区中。

Hive中也提供了类似的机制,即动态分区(Dynamic Partition);使用Hive的动态分区需要进行相应的配置。

(1)开启动态分区参数设置

1)开启动态分区参数功能(默认为TRUE)

hive.exec.dynamic.partition=true

 2)设置为非严格模式

动态分区的模式,默认为strict,表示必须指定至少一个分区为静态分区;nonstrict 模式表示允许所有的分区字段都可以使用动态分区。 

hive.exec.dynamic.partition.mode=nonstrict

3)设置在所有执行MR的节点上最大一共可以创建多少个动态分区(默认:1000)

hive.exec.max.dynamic.partitions=1000

4)设置在每个执行MR的节点上,最大可以创建多少个动态分区

该参数需要根据实际的数据来设定。

hive.exec.max.dynamic.partitions.pernode=100

5)设置整个MR Job中最大可以创建多少个HDFS文件(默认为100000)

hive.exec.max.created.files=100000

6)当有空分区生成时是否抛出异常(默认为FALSE)

hive.error.on.empty.partition=false

例子:将dept表中的数据按照地区(loc 字段),插入到目标表dept_partition_dy的相应分区中

#创建目标分区表
create table dept_partition_dy(id int, name string) 
partitioned by (loc int) row format delimited fields terminated by ' ';

#设置动态分区
set hive.exec.dynamic.partition.mode = nonstrict;
insert into table dept_partition_dy partition(loc) select 
deptno, dname, loc from dept;

#查看目标分区表的分区情况
show partitions dept_partition;

分桶表

1)分区提供一个隔离数据和优化查询的便利方式。并非所有的数据集都可形成合理的分区,对于一张表或者分区,Hive可以进一步组织成桶,也就是更为细粒度的数据范围划分。

2)分桶是将数据集分解成更容易管理的若干部分的另一个技术。

3)分区针对的是数据的存储路径;分桶针对的是数据文件。

(1)创建分桶表

1)准备数据

1001 ss1
1002 ss2
1003 ss3
1004 ss4
1005 ss5
1006 ss6
1007 ss7
1008 ss8
1009 ss9
1010 ss10
1011 ss11
1012 ss12
1013 ss13
1014 ss14
1015 ss15
1016 ss16

2)创建分桶表

create table buck(
id int,
name string)
clustered by (id)
into 4 buckets
row format delimited fields terminated by ' ';

3)导入数据

load data inpath '/root/datas/partition/bucket.txt' into table buck;

4)查询

 (2)注意事项

1reduce的个数设置为-1,Job自行决定需要用多少个reduce或者将reduce的个数设置为大于等于分桶表的桶数;

2)从hdfsload数据到分桶表中,避免本地文件找不到问题;

3)不要使用本地模式;

(3)insert导入数据

insert into table buck select * from buck_insert;

抽样查询

有时用户需要使用的是一个具有代表性的查询结果而不是全部结果。Hive可以通过对表进行抽样来满足这个需求。

语法: TABLE SAMPLE(BUCKET x OUT OF y)

例子:查询表buck中的数据

select * from buck tablesample(bucket 1 out of 4 on id);

主:x的值必须小于等于y的值。

本文为学习笔记!!!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值