溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

hive分區(qū)和分桶的示例分析

發(fā)布時(shí)間:2021-12-10 14:54:19 來(lái)源:億速云 閱讀:264 作者:小新 欄目:大數(shù)據(jù)

這篇文章主要為大家展示了“hive分區(qū)和分桶的示例分析”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“hive分區(qū)和分桶的示例分析”這篇文章吧。

1.為什么要分區(qū)??

當(dāng)單個(gè)表數(shù)據(jù)量越來(lái)越大的時(shí)候,hive查詢通常會(huì)全表掃描,這將會(huì)浪費(fèi)我們不關(guān)心數(shù)據(jù)的掃描,浪費(fèi)大量時(shí)間。從而hive引出分區(qū)概念partition

2.怎么分區(qū)??

看具體業(yè)務(wù),能把一堆數(shù)據(jù)拆分成多個(gè)堆的數(shù)據(jù)就可以。 通常使用id 、 年 、 月 、天 、區(qū)域 、省份、 hive分區(qū)和mysql分區(qū)的區(qū)別?? mysql的分區(qū)字段采用的表內(nèi)字段。 hive的分區(qū)字段使用的是表外字段。

3.hive分區(qū)細(xì)節(jié)??

1、分區(qū)本質(zhì)是在該表下創(chuàng)建對(duì)應(yīng)的目錄。 2、分區(qū)名大小寫(xiě)不區(qū)分,建議不要使用中文。 3、可以查詢分區(qū)信息。但是我們的分區(qū)字段相當(dāng)于是一個(gè)偽字段,在元數(shù)據(jù)中存在,但是不真實(shí)存在數(shù)據(jù)內(nèi)容中。

4、加載數(shù)據(jù)時(shí)要指定分區(qū)

4.分區(qū)操作

創(chuàng)建一級(jí)分區(qū)表:

create table if not exists day_part(
uid int,
uname string
)
partitioned by(year int)
row format delimited fields terminated by '\t'
;
load data local inpath '/root/Desktop/student.txt' into table day_part partition(year=2017);
load data local inpath '/root/Desktop/score.txt' into table day_part partition(year=2016);
show partitions day_part;

二級(jí)分區(qū)

create table if not exists day_part1(
uid int,
uname string
)
partitioned by(year int,month int)
row format delimited fields terminated by '\t'
;
load data local inpath '/root/Desktop/student.txt' into table day_part1 partition(year=2017,month=04);
load data local inpath '/root/Desktop/score.txt' into table day_part1 partition(year=2017,month=03);

三級(jí)分區(qū):

create table if not exists day_part2(
uid int,
uname string
)
partitioned by(year int,month int,day int)
row format delimited fields terminated by '\t'
;

對(duì)分區(qū)進(jìn)行操作: 顯示分區(qū):

show partitions day_part;

新增分區(qū):空的

alter table day_part1 add partition(year=2017,month=2);
alter table day_part1 add partition(year=2017,month=1) partition(year=2016,month=12);

新增分區(qū)并加載數(shù)據(jù):

alter table day_part1 add partition(year=2016,month=11) location "/user/hive/warehouse/qf1603.db/day_part1/year=2017/month=2";

修改分區(qū)所對(duì)應(yīng)的存儲(chǔ)路徑:

##路徑必須從hdfs寫(xiě)起
alter table day_part1 partition(year=2016,month=11) set location "hdfs://linux1:9000/user/hive/warehouse/qf1603.db/day_part1/year=2017/month=3";

刪除分區(qū):刪除分區(qū)將會(huì)刪除對(duì)應(yīng)的分區(qū)目錄(數(shù)據(jù))

##刪除某個(gè)分區(qū)
alter table day_part1 drop partition(year=2017,month=2);
##刪除多個(gè)
alter table day_part1 drop partition(year=2017,month=3),partition(year=2017,month=4);

靜態(tài)分區(qū)、動(dòng)態(tài)分區(qū)、混合分區(qū) 靜態(tài)分區(qū):新增分區(qū)或者是加載分區(qū)數(shù)據(jù)時(shí),已經(jīng)指定分區(qū)名。 動(dòng)態(tài)分區(qū):新增分區(qū)或者是加載分區(qū)數(shù)據(jù)時(shí),分區(qū)名未知。 混合分區(qū):靜態(tài)分區(qū)和動(dòng)態(tài)分區(qū)同時(shí)存在。

動(dòng)態(tài)分區(qū)的相關(guān)屬性: hive.exec.dynamic.partition=true :是否允許動(dòng)態(tài)分區(qū) hive.exec.dynamic.partition.mode=strict :分區(qū)模式設(shè)置nostrict strict:最少需要有一個(gè)是靜態(tài)分區(qū) nostrict:可以全部是動(dòng)態(tài)分區(qū) hive.exec.max.dynamic.partitions=1000 :允許動(dòng)態(tài)分區(qū)的最大數(shù)量 hive.exec.max.dynamic.partitions.pernode =100 :?jiǎn)蝹€(gè)節(jié)點(diǎn)上的mapper/reducer允許創(chuàng)建的最大分區(qū)

創(chuàng)建臨時(shí)表:

##創(chuàng)建臨時(shí)表
create table if not exists tmp(
uid int,
commentid bigint,
recommentid bigint,
year int,
month int,
day int
)
row format delimited fields terminated by '\t';
##加載數(shù)據(jù)
load data local inpath '/root/Desktop/comm' into table tmp;

創(chuàng)建動(dòng)態(tài)分區(qū):

##創(chuàng)建動(dòng)態(tài)分區(qū)表
create table if not exists dyp1(
uid int,
commentid bigint,
recommentid bigint
)
partitioned by(year int,month int,day int)
row format delimited fields terminated by '\t'
;

為動(dòng)態(tài)分區(qū)加載數(shù)據(jù):

##嚴(yán)格模式
insert into table dyp1 partition(year=2016,month,day)
select uid,commentid,recommentid,month,day from tmp;
##非嚴(yán)格模式
##設(shè)置非嚴(yán)格模式動(dòng)態(tài)分區(qū)
set hive.exec.dynamic.partition.mode=nostrict;
##創(chuàng)建動(dòng)態(tài)分區(qū)表
create table if not exists dyp2(
uid int,
commentid bigint,
recommentid bigint
)
partitioned by(year int,month int,day int)
row format delimited fields terminated by '\t';
##為非嚴(yán)格模式動(dòng)態(tài)分區(qū)加載數(shù)據(jù)
insert into table dyp2 partition(year,month,day)
select uid,commentid,recommentid,year,month,day from tmp;

hive提供我們一個(gè)嚴(yán)格模式:為了阻止用戶不小心提交惡意hql hive.mapred.mode=nostrict : strict
如果該模式值為strict,將會(huì)阻止以下三種查詢: 1、對(duì)分區(qū)表查詢,where中過(guò)濾字段不是分區(qū)字段。 2、笛卡爾積join查詢,join查詢語(yǔ)句,不帶on條件 或者 where條件。

select
stu.id,
stu.name,
score.grade
from student stu
join score
;

可以:

select
stu.id,
stu.name,
score.grade
from student stu
join score
where stu.id = score.uid
;

3、對(duì)order by查詢,有order by的查詢不帶limit語(yǔ)句。

select
student.*
from student
order by student.id desc
;

注意: 1、盡量不要是用動(dòng)態(tài)分區(qū),因?yàn)閯?dòng)態(tài)分區(qū)的時(shí)候,將會(huì)為每一個(gè)分區(qū)分配reducer數(shù)量,當(dāng)分區(qū)數(shù)量多的時(shí)候,reducer數(shù)量將會(huì)增加,對(duì)服務(wù)器是一種災(zāi)難。 2、動(dòng)態(tài)分區(qū)和靜態(tài)分區(qū)的區(qū)別,靜態(tài)分區(qū)不管有沒(méi)有數(shù)據(jù)都將會(huì)創(chuàng)建該分區(qū),動(dòng)態(tài)分區(qū)是有結(jié)果集將創(chuàng)建,否則不創(chuàng)建。 3、hive動(dòng)態(tài)分區(qū)的嚴(yán)格模式和hive提供的hive.mapred.mode的嚴(yán)格模式。

分桶

1.為什么要分桶??

分區(qū)數(shù)據(jù)依然很大,對(duì)分區(qū)數(shù)據(jù)或者表數(shù)據(jù)更加細(xì)粒度的管理。 分桶關(guān)鍵字: clustered by(uid) into n buckets 、bucket 、 分桶使用表內(nèi)字段 怎么分桶?? 對(duì)分桶字段進(jìn)行hash值,然后將hash值模于總的桶數(shù),然后得到桶數(shù)

2.分桶的意義:

1、快速抽樣查詢。tablesample 2、減少查詢掃描數(shù)據(jù)量,提高查詢效率。

##創(chuàng)建分桶表,設(shè)置4個(gè)分桶
create table if not exists bucket1(
uid int,
uname String
)
clustered by(uid) into 4 buckets
row format delimited fields terminated by '\t'
;

3.分桶的操作:

為分桶表加載數(shù)據(jù): 分桶不能使用load方式來(lái)加載數(shù)據(jù),而需要iinsert into方式來(lái)加載 并且需要設(shè)置屬性:

##設(shè)置分桶啟用
hive> set hive.enforce.bucketing=true;
##錯(cuò)誤的加載數(shù)據(jù)方式
load data local inpath '/root/Desktop/student' into table bucket1;
##創(chuàng)建分桶表,設(shè)置4個(gè)分桶
create table if not exists bucket7(
uid int,
uname String
)
clustered by(uid) into 4 buckets
row format delimited fields terminated by '\t'
;
##為分桶表加載數(shù)據(jù)
insert into table bucket7
select id,name from student
;

分桶查詢:tablesample(bucket x out of y on uid) 注意:x不能大于y x:所取桶的起始位置, y:所取桶的總數(shù),y是總桶數(shù)的因子。y大于源總桶數(shù)相當(dāng)于拉伸,y小于源總桶數(shù)相當(dāng)于壓縮 1 out of 2 1 1+4/2 2 out of 2 2 2+4/2

1 out of 4 1 1+4

select * from bucket7;
select * from bucket7 tablesample(bucket 1 out of 4 on uid);
select * from bucket7 tablesample(bucket 2 out of 4 on uid);
select * from bucket7 tablesample(bucket 1 out of 2 on uid);
select * from bucket7 tablesample(bucket 2 out of 2 on uid);
select * from bucket7 tablesample(bucket 3 out of 2 on uid);
select * from bucket7 tablesample(bucket 1 out of 8 on uid);
select * from bucket7 tablesample(bucket 5 out of 8 on uid);

分區(qū)+分桶:(qfstu) uid,uname,class,master gender分區(qū) 分桶uid 基偶分桶 查詢女生中的學(xué)號(hào)為基數(shù)??

##創(chuàng)建表
create table if not exists qftmp(
uid int,
uname string,
class int,
gender int)
row format delimited fields terminated by '\t';
##加載數(shù)據(jù)
load data local inpath '/home/qf' into table qftmp;
##創(chuàng)建動(dòng)態(tài)分區(qū)分桶表
create table if not exists qf(
uid int,
uname string,
class int)
partitioned by(gender int)
clustered by(uid) into 2 buckets
row format delimited fields terminated by '\t';
##為動(dòng)態(tài)分區(qū)分桶表加載數(shù)據(jù)
insert into table qf partition(gender)
select uid,uname,class,gender from qftmp;

查詢女生中的學(xué)號(hào)為基數(shù)?????

select * from qf where gender = 2 and uid%2 != 0;
select * from qf tablesample(bucket 2 out of 2 on uid) where gender = 2;

分桶使用內(nèi)部關(guān)鍵字,分區(qū)使用的是外部字段。 兩者都是對(duì)hive的一個(gè)優(yōu)化。 分區(qū)和分桶的數(shù)量都要合理設(shè)置,不是越多越好。

抽樣:

select * from student order by rand() limit 3;
select * from student limit 3;
select * from student tablesample(3 rows);
select * from student tablesample(20B); ##最小單位是B
select * from student tablesample(20 percent);##百分比

以上是“hive分區(qū)和分桶的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI