hive建立不同的表(內部,外部,分割槽,分桶)

2021-10-10 17:29:59 字數 2174 閱讀 3705

hive本身並不儲存資料,而是將資料儲存在hadoop的hdfs中,表名對應hdfs中的目錄/檔案。根據資料的不同儲存方式,將hive表分為外部表、內部表、分割槽表和分桶表四種資料模型。每種資料模型各有優缺點。通過create user命令建立user表時,會在hdfs中生成乙個user目錄/檔案。

資料不由hive管理,使用drop命令刪除乙個表時,只是把錶的元資料給刪除了,而表的資料不會刪除。 建立外部表的sql語句:

create external table bigdata17_user(

userid int,

username string,

fullname string)

row format delimited fields terminated by ','

lines terminated by '\n'

;

內部表(有些人會翻譯成管理表)的資料由hive管理,當使用drop刪除表時,會把表的元資料和資料一起刪除,資料無法恢復,因此一定要慎用drop刪除內部表。

建立內部表的sql語句:

create table bigdata17_user( userid int, username string, fullname string)

row format delimited fields terminated by ','

lines terminated by '\n'

;

和外部表建立的語法基本一樣,只是建立外部表需要使用external關鍵字。沒有external關鍵字則是建立內部表。

內部表和外部表都可以使用分割槽的功能,使用分割槽的內部或外部表稱為分割槽表。 建立分割槽表的語句:

create external table bigdata17_user_partition(

username string,

fullname string)

partitioned by(userid string)

row format delimited fields terminated by ','

lines terminated by '\n'

;

往分割槽表匯入資料分為靜態分割槽匯入和動態分割槽匯入,靜態分割槽是在匯入語句中指定分割槽值,例如:

insert overwrite table bigdata17-user_parttion

partition(userid=1)

select username ,fullname from bigdata17_user;

該語句的分割槽值預設是1,如果有多個分割槽值,必須寫多個sql語句,效率低下。

一般情況在我們都是使用動態分割槽匯入資料,

在匯入資料之前必須執行下面的兩條語句讓hive支援動態分割槽功能,預設是不支援動態分割槽的。

set hive.exec.dynamic.partition=true;

set hive.exec.dynamic.partition.mode=nonstrict;

動態分割槽匯入資料的sql語句:

insert overwrite table bigdata17_user_partition

partition(userid)

select username ,fullname,userid from bigdata17_user;

分桶是將某個欄位取雜湊值,值相同的資料分發到乙個桶中。在建立分桶表的時候必須指定分桶的字段,並且指定要分桶的數量。 建立分桶表對sql語句如下:

create table bigdata17_user_bucket( userid int, username string, fullname string)

clustered by(userid) into 2 buckets

row format delimited fields terminated by ','

lines terminated by '\n'

;

注意:分割槽和分桶都是按字段來組織資料的存放,分割槽是相同的字段值存放在乙個檔案中,而分桶是字段雜湊值相同的資料存放在乙個檔案中。

hive建立表 內部表和外部表)

1 建表語法 create external table if not exists table name col name data type comment col comment comment table comment partitioned by col name data type c...

Hive內部表 外部表

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

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

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