建立分割槽表的原因:單錶資料量隨著時間越來越大。為了避免全表掃瞄,引入分割槽。
hive分割槽和mysql分割槽表的區別??
hive分割槽使用表外字段,mysql使用表內字段。
hive分割槽表細節?
1.hive分割槽的字段是乙個偽欄位,它不會在表中真實存在,可以用來過濾查詢等
2.乙個表或者乙個分割槽可以有多個分割槽,而每乙個分割槽都是以目錄的形式存在。
怎麼分割槽?
根據業務,地域,年,月,日,性別等。
關鍵字:partitioned by
一級分割槽:
首先使用use databasename; 轉到相應的資料庫。
//檢視分割槽資訊:
show partitions comm;
//建立分割槽表
create table if not exists comm(
id int,
comment string,
dt string
)partitioned by(year string)
row format delimited fields terminated by '\t'
;//從本地檔案載入資料到comm表的year=2019的分割槽當中
load data local inpath 'youfilepath' into comm partition(year='2019');
//檢視分割槽內容
select * from comm where year = 2019;
分割槽操作
//增加分割槽
//建立單個分割槽:
alter table comm add partition(year='2018');
//同時建立多個分割槽
alter table comm add partition(year='2020') partition(year='2017');
//修改分割槽名字
alter table comm partition(year='2020') rename to partition(year='2016');
//指定分割槽對應到已有資料:
alter table comm partition(year='2016') set location 'hdfs://***/user/hive/warehouse/***.db/***'
(這是在hdfs中想對應的檔案路徑,分割槽表其實就是hdfs的某個檔案)
//檢視分割槽
show partitions comm ;
//刪除分割槽
刪除單個分割槽:alter table comm drop partition(year='2018');
同時刪除多個分割槽:alter table comm drop partition(year='2018'),partition(year='2019');
//檢視分割槽表的結構
desc formatted comm;
二級分割槽:
//建立
create table if not exists comm(
id int,
comment string,
dt string
)partitioned by(year string,month string)
row format delimited fields terminated by '\t'
;
靜態分割槽、動態分割槽、混合分割槽
靜態分割槽:對分割槽已經知道,並可以使用load方式載入
動態分割槽:對於分割槽未知,同時不能使用load方式載入
混合分割槽:靜態和動態同時有
動態分割槽案例:
建立資料表:
create table if not exists comm_tmpl(
id int,
comment string,
year string,
month string
)row format delimited fields terminated by '\t'
;//向分割槽表加入資料
load data local inpath 『 』 into table comm_tmpl;
建立分割槽表:
create table if not comm(
id int,
comment string,
)partitioned by(year string,month int)
rot format delimited fields terminated by '\t'
;
動態向分割槽表加入資料:
首先要把hive.exec.dynamic.partition.mode=strict/nostrict 改為nostrict
命令:set hive.exec.dynamic.partition.mode=nostrict
//新增資料
insert into table comm3 partition(year,month)
select id,comment,year,month from comm_tmpl
;//命令結束後,會按照comm_tmpl中year和month來對資料進行分割槽
hive.exec.dynamic.partition=true 是否允許動態分割槽;
hive.exec.dynamic.partition.mode=strict/nostrict
hive. exec .max. dynamic. partitions=1000最大動態分割槽數量
hive. exec . max.dynamic. partitions.pernode=100 單個節點允許最大分割槽數量
嚴格模式下不讓執行的語句:
1.笛卡爾積查詢
select
c3.*,
c4.*
from comm c4
join comm1 c3
;2.分割槽不帶where條件,並且where條件中不帶分割槽欄位來過濾
(可以)
select *
from comm
where year = 2016
;(不可以)
select *
from comm
;3.排序不帶limit
select *
from comm
where year = 2019
order by id desc
limit 2
;
hive 分割槽表
partitioned by create table tb name name string partitioned by age int row format delimited fields terminated by t load data local inpath file path in...
hive分割槽表
partition對應於資料庫的 partition 列的密集索引 在 hive 中,表中的乙個 partition 對應於表下的乙個目錄,所有的 partition 的資料都儲存在對應的目錄中 例如 test表中包含 date 和 city 兩個 partition 則對應於date 201302...
Hive 分割槽表
分割槽表的查詢 讓分割槽表和資料產生關聯的三種方式 create table student id int name string partitioned by day string row format delimited fields terminated by load data local ...