hive05: hive的分区表及分桶表

1.分区表

 

分区表实际上就是对应一个HDFS文件系统上的独立的文件夹,该文件夹下是该分区所有的数据文件。Hive中的分区就是分目录,把一个大的数据集根据业务需要分割成小的数据集

分区表是将数据分文件夹管理 , 减少数据扫描的文件范围  直接从对应文件夹中读取数据  

1.静态分区

文件中存储的指定规则的数据

创建静态分区步骤:

1)前提有静态数据

20201128.log                      20201129.log                 20191011.log                              
1,url1,20201128                1,url1,20201129              1,url1,20191011                   
2,url2,20201128                2,url2,20201129               2,url2,20191011 
3,url3,20201128                3,url3,20201129               3,url3,20191011
4,url4,20201128                4,url4,20201129               4,url4,20191011
5,url5,20201128                5,url5,20201129               5,url5,20191011
6,url6,20201128                6,url6,20201129               6,url6,20191011
7,url7,20201128                7,url7,20201129               7,url7,20191011

drop  table tb_log ;
create table tb_log(
log_id string ,
url string ,
ct string
)
row format delimited fields terminated by ',' ;
load  data  local  inpath "/data/log/" into table tb_log ;  //将数据导入到一个普通表格中

 

2)创建分区表

-------------------一级分区-------------------------------

create table tb_partition_log (

id string,

url string,

tb_time string

)

partitioned by(tb_time string)    ----指定分区字段

row format delimited fields terminated by ',' ;



--------------------多级分区--------------------------

create table tb_partition_log2 (

id string,

url string,

tb_time string

)

partitioned by(y string , m string , d string)   ----此处为多级分区

row format delimited fields terminated by ',' ;

3)将静态数据导入到分区表中

-----------------创建一级分区表-------------------------------------

load data local inpath "/data/log/20201128.log" into table tb_partition_log partition(tb_time='20201128') ;

load data local inpath "/data/log/20201129.log" into table tb_partition_log partition(tb_time='20201129') ;

load data local inpath "/data/log/20191011.log" into table tb_partition_log partition(tb_time='20191011');

------------------创建多级分区表--------------------------------

load data local inpath "/data/log/20201128.log" into table tb_partition_log partition(y='2020' , m='11' , d='28') ;

load data local inpath "/data/log/20201129.log" into table tb_partition_log partition(y='2020' , m='11' , d='29') ;

load data local inpath "/data/log/20191011.log" into table tb_partition_log partition(y='2019' , m='10' , d='11');

 

2.动态分区

数据

userlog

1,zs,shenzheng
2,ls,beijing
3,ww,shanghai
4,laoliu,shanghai

1)创建普通表

create table user_log1(
id int,
name string,
city string
)
row format delimited fields  terminated by ',';
load  data  local  inpath  "/data/userlog/user_log" into  table user_log1;

2)创建动态分区表

create table tb_partition_user_log(

id string,

name string,

city string

)

partitioned by(P_city string);

3)开启动态权限(最基本要开启红色的部分,其他看情况开启)

set hive.exec.dynamic.partition=true //使用动态分区

set hive.exec.dynamic.partition.mode=nonstrick;//无限制模式,如果模式是strict,则必须有一个静态分区且放在最前面

set hive.exec.max.dynamic.partitions.pernode=10000;//每个节点生成动态分区的最大个数

set hive.exec.max.dynamic.partitions=100000;//生成动态分区的最大个数

set hive.exec.max.created.files=150000;//一个任务最多可以创建的文件数目

set dfs.datanode.max.xcievers=8192;//限定一次最多打开的文件数

set hive.merge.mapfiles=true; //map端的结果进行合并

set mapred.reduce.tasks =2;  //设置reduce task个数

4)把数据加载到分区表中

insert into table tb_partition_user_log partition(P_city)    --- 此处参数名字P_city, 必须和动态分区表的分区参数名字相同

select id ,name , city ,city as P_city from user_log1; ---此处的P_city不和上面的相同的话也会对应上,此处会默认第四个参数作为了分区值(最好名字对应上)

只能添加4行数据,第四位默认为分区值,不能指定其他行

 

3.修改分区表

1)重命名表

alter table tb_name rename new_tb_name ;

2)增加分区

alter table tb_name add partition(str='分区值');
alter table tb_name add partition(str='分区值') partition(str1='分区值');

3)删除分区

alter table tb_name drop partition(str='分区值');

alter table tb_name drop partition(str='分区值') partition(str='分区值');

4)查看表中有多少分区

show partitions tb_name;

5)查看分区表结构

desc foramtted tb_name

2.分桶表

对Hive(Inceptor)表分桶可以将表中记录按分桶键(字段)的哈希值分散进多个文件中,这些小文件称为桶。

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

优点:

1,更快,桶为表加上额外结构,链接相同列划分了桶的表,可以使用map-side join更加高效。

2,取样sampling更高效。没有分区的话需要扫描整个数据集。

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 tb_stu(
id int,
name string
)
row format delimited fields terminated by '\t';
load data local inpath '/data/stu' into table tb_stu;

3.开启分桶权限

set hive.enforce.bucketing=true;     -- 开启分桶
set mapreduce.job.reduces=-1;        --设置reduce为默认值

4.创建分桶表

create table tb_buck_stu(
id int,
name string
)
clustered by(id)  ---分区排序
into 3 buckets;  ----分桶

5.导入数据

insert into tb_buck_stu
select id,name from tb_stu;

2.分桶表的抽样查询

select * from tb_buck_stu1 tablesample(bucket 4 out of 8 on id); ---按id取1/3

语法:TABLESAMPLE(BUCKET x OUT OF y) 。
y必须是table总bucket数的倍数或者因子。hive根据y的大小,决定抽样的比例。
例如,table总共分了4份,当y=2时,抽取(4/2=)2个bucket的数据,当y=8时,抽取(4/8=)1/2个bucket的数据。
x表示从哪个bucket开始抽取,如果需要取多个分区,以后的分区号为当前分区号加上y。
例如,table总bucket数为4,tablesample(bucket 1 out of 2),表示总共抽取(4/2=)2个bucket的数据,抽取第1(x)个和第3(x+y)个bucket的数据。
注意:x的值必须小于等于y的值

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值