Hive 內部表 外部表 分割槽表 擴充套件命令

2022-05-24 23:57:14 字數 4543 閱讀 7620

create [external] table if not exists 表名

(列名資料型別 [comment 本列注釋],...)

[comment 表注釋]

[partitioned by (列名資料型別 [comment 本列注釋],...)]

[clustered by(列名,列名,...)]

[sorted by (列名 [asc|desc],...)] info num_buckets buckets]

[row format row_format][stored as file_format]

[location hdfs_path]

[tblproperties (property_name=property_value,...)]

[as select_statement]

說明:①external表示建立外部表;hive在建立內部表時,會將資料移動到資料倉儲指向的路徑;若建立外部表,僅記錄資料所在的路徑,不對資料的位置做任何改變

②partitioned by表示建立分割槽表

③clustered by建立分桶表

④sorted by 不常用

⑤row format delimited [fields terminated by char] [collection items terminated by char] [map keys terminated by char] [line terminated by char]

⑥stored as 指定檔案儲存型別(sequencefile二進位制檔案、textfile文字檔案、rcfile列式儲存格式)

⑦location 指定表在hdfs上的儲存位置

⑧like 允許使用者複製現有的表結構,但是不複製資料

⑨as 後跟查詢語句,根據查詢結果建立表

desc formated 表名;  //可以檢視分割槽字段(partition)資訊

desc 表名;

external:表示建立外部表,僅記錄資料所在路徑,不對資料做改變

不寫external:表示建立內部表,會將資料移動到資料倉儲指向的路徑。

1、希望做資料備份並且不經常改變的資料,存放在外部表可以減少失誤操作。

2、資料清洗轉換後的中間結果,可以存放在內部表,因為hive對內部表支援的功能比較全面,方便管理。

3、處理完成的資料由於需要共享,可以儲存在外部表,這樣能夠防止失誤操作,增加資料的安全性。

4、在生產中一般建立外部表來儲存資料。

修改表名

alter

table a_a rename to aa;

向表中新增列

alter

table a_a add

columns (

a_name string comment '

' ,a_id bigint comment '

the current session id

');

修改列名

alter

table 表名 change 原列名 新列名 新型別;

(慎用)刪表:

drop

table

ifexists table_name;

(慎用)清空表:

truncate

table 表名;

擴充套件:在hive中檢視函式功能(比如substr)

desc function extended substr;

檢視所有hive函式

show functions;

殺死程序 -比如有乙個程序為 :11245 runjar

kill-9

11245;

分割槽表:在一定程度上可以理解為分成資料夾。

hive中有分割槽表的概念,我們可以看到分割槽具有重要效能優勢,分割槽表可以將資料以一種符合邏輯的方式進行組織,比如分層儲存。

2、查詢分割槽表中的資料時,除非where語句中包含分割槽字段過濾條件來顯示資料範圍,否則不允許執行。

3、換句話說,就是使用者不允許掃瞄所有的分割槽。

4、進行這個顯示的原因是,通常分割槽表都擁有非常大的資料集,而且資料增加迅速。如果沒有進行分割槽限制的查詢可能會消耗令人不可接受的巨大資源來處理這個表。

5、分割槽是hive存放資料的一種方式。將列值作為目錄來存放資料,就是乙個分割槽。這樣查詢時使用分割槽列進行過濾,只需根據列值直接掃瞄對應目錄下的資料,不掃瞄其他不關心的分割槽,快速定位,提高查詢效率.

分割槽表分位靜態分割槽和動態分割槽

靜態分割槽:手動指定-->編譯時期。

動態分割槽:通過輸入資料來進行判斷-->sql語句。

一般按時間來分割槽:天,小時,分鐘。

create

table

test

(name string,age

int)

partitioned

by(country string)

row format delimited fields terminated by'

\t'lines terminated by'

\n'stored

as textfile;

向分割槽中插入資料:

insert

into

table test partition(country=

"china")

values("zhangsan",1

);insert

into

table test partition(country=

"usa")

values("james",34

);insert

into

table test partition(country=

"usa")

values("tom",2);

查詢分割槽表的資料:

select

*from test where country="china";

刪除分割槽:

alter

table test drop partition(country="china");

載入資料指定分割槽:

load data local inpath '

/root/desktop/student.txt

'into

table test partition(name='zs

',age=

21);

動態分割槽的相關配置屬性:

set hive.exec.dynamic.partition=

true;

(可通過這個語句檢視:

set hive.exec

.dynamic.partition;)

set hive.exec.dynamic.partition.mode=

nonstrict;

set hive.exec.max.dynamic.partitions=

100000

;(如果自動分割槽數

大於這個引數,將會報錯)

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

100000;

顯示分割槽數:

show partitions order_part;

查詢分割槽表中的資料:

select

*from user_trade limit 6; //這樣會報錯,因為沒有加分割槽條件。

檢視所有配置

set

嚴格模式:set hive.mapred.mode=strict;

set hive.mapred.mode=

strict;

select

*from user_trade limit 6

;select

*from user_trade where dt=

'2017-01-12

';

我們應該

1、對分割槽表查詢,用where過濾欄位用分割槽字段。

2、禁止用笛卡爾積join查詢,join查詢語句,不帶on條件或者where條件。

3、order by後面用

limit。

1、分桶是對列值取hash值的方式將資料放在不同的檔案儲存。

2、hive中的每乙個表、分割槽都可以進行分桶。

3、由列的hash值除以桶的個數來決定將每條資料具體劃分在哪個桶中。

應用場景:抽樣、map-join

開課吧

內部表,外部表,分割槽表

1.未被external修飾的是內部表 managed table 被external修飾的為外部表 external table 2.內部表資料由hive自身管理,外部表資料由hdfs管理。3.內部表資料儲存在hive.metastore.warehouse.dir 預設 user hive wa...

Hive內部表,外部表,分割槽表的建立

建立內部表 預設儲存在 user hive warehouse下 也可以通過location指定 刪除表時,會刪除表資料及元資料 create table if not exists db study.student id string name string row format delimite...

Hive表分類,內部表 外部表 分割槽表簡介

內部表與資料庫中的table在概念上是類似的,每乙個內部table在hive中都有乙個相應目錄儲存資料,所有的table資料 不包括external table 都儲存在這個目錄中。刪除表時,元資料與資料都會被刪除。在建立表的時候可以指定external關鍵字建立外部表,外部表對應的檔案儲存在loc...