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

2021-10-07 08:04:29 字數 3890 閱讀 1540

內部表與資料庫中的table在概念上是類似的,每乙個內部table在hive中都有乙個相應目錄儲存資料,所有的table資料(不包括external table)都儲存在這個目錄中。刪除表時,元資料與資料都會被刪除。

在建立表的時候可以指定external關鍵字建立外部表,外部表對應的檔案儲存在location指定的目錄下,向該目錄新增新檔案的同時,該錶也會讀取到該檔案(當然檔案格式必須跟表定義的一致),刪除外部表的同時並不會刪除location指定目錄下的檔案。在刪除內部表的時候,內部表的元資料和資料會被一起刪除。

刪除表,保留表中的資料- 推薦external

1

,zhangsan,

true,18

,15000

,tv|game,

001>建設|

002>招商,china|bj

2,lisi,

true,28

,15000

,tv|game,

001>建設|

002>招商,china|bj

3,wangwu,

false,38

,5000

,tv|game,

001>建設|

002>招商,china|sh

------

create external table t_user_c(

id int

, name string,

*** boolean

, age int

, salary double

, hobbies array

, card map

, address struct

)row format delimited

fields

terminated

by','

collection items terminated

by'|'

map keys

terminated

by'>'

lines

terminated

by'\n'

;-- 指定路徑資訊

create external table t_user_a(

id int

, name string,

*** boolean

, age int

, salary double

, hobbies array

, card map

, address struct

)row format delimited

fields

terminated

by','

collection items terminated

by'|'

map keys

terminated

by'>'

lines

terminated

by'\n'

location '/user/hive/warehouse/baizhi.db/t_user_c'

;

龐大的資料集可能需要耗費大量的時間去處理。在許多場景下,可以通過分割槽或切片的方法減少每一次掃瞄總資料量,這種做法可以顯著地改善效能。資料會依照單個或多個列進行分割槽,通常按照時間、地域或者是商業維度進行分割槽。比如電影表,分割槽的依據可以是電影的種類和評級,另外,按照拍攝時間劃分可能會得到更一致的結果。為了達到效能表現的一致性,對不同列的劃分應該讓資料盡可能均勻分布。最好的情況下,分割槽的劃分條件總是能夠對應where語句的部分查詢條件。

為了對錶進行合理的管理以及提高查詢效率,hive可以將表組織成「分割槽」。分割槽是表的部分列的集合,可以為頻繁使用的資料建立分割槽,這樣查詢分割槽中的資料時就不需要掃瞄全表,這對於提高查詢效率很有幫助。分割槽表是一種根據「分割槽列」(partition column)的值對錶進行粗略劃分的機制。hive中的每個分割槽表對應資料庫中相應分割槽列的乙個索引,每個分割槽表對應著表下的乙個目錄,在hdfs上的表現形式與表在hdfs上的表現形式相同,都是以子目錄的形式存在。但是由於hdfs並不支援大量的子目錄,這也給分割槽的使用帶來了限制。我們有必要對錶中的分割槽數量進行預估,從而避免因為分割槽數量過大帶來一系列問題。hive查詢通常使用分割槽的列作為查詢條件。這樣的做法可以指定mapreduce任務在hdfs中指定的子目錄下完成掃瞄的工作。hdfs的檔案目錄結構可以像索引一樣高效利用。

create external table t_user(

id int

, name string,

*** boolean

, age int

, salary double

, country string,

city string

)row format delimited

fields

terminated

by','

collection items terminated

by'|'

map keys

terminated

by'>'

lines

terminated

by'\n'

--------------------

create external table t_user_p(

id int

, name string,

*** boolean

, age int

, salary double

) partitioned by

(country string,city string)

row format delimited

fields

terminated

by','

collection items terminated

by'|'

map keys

terminated

by'>'

lines

terminated

by'\n'

0: jdbc:hive2://centos:10000> load data local inpath '/root/t_user_sh' overwrite into table t_user_p partition(country='china',city='sh');

0: jdbc:hive2://centos:10000> show partitions t_user_p;

+------------------------+--+

|partition|+

------------------------+--+

| country=china/city=bj |

| country=china/city=sh |

+------------------------+--+

0: jdbc:hive2://centos:10000> alter table t_user_p drop partition(country='china',city='bj');

info : dropped the partition country=china/city=bj

norows affected (

0.496 seconds)

0: jdbc:hive2://centos:10000> alter table t_user_p add partition(country='china',city='bj');

0: jdbc:hive2://centos:10000> alter table t_user_p add partition(country='china',city='sh') location '/logs';

hive內部表 外部表 分割槽

hive內部表 外部表 分割槽 內部表 managed table 外部表 external table 表分割槽 partitioned table create table table name id int,dtdontquery string,name string partitioned ...

Hive內部表 外部表

內部表 外部表 未被external修飾的是內部表 managed table 被external修飾的為外部表 external table 區別 內部表資料由hive自身管理,外部表資料由hdfs管理 內部表資料儲存的位置是hive.metastore.warehouse.dir 預設 user...

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

create external table if not exists 表名 列名資料型別 comment 本列注釋 comment 表注釋 partitioned by 列名資料型別 comment 本列注釋 clustered by 列名,列名,sorted by 列名 asc desc inf...