Hive操作表分割槽

2021-07-25 04:59:54 字數 2023 閱讀 6141

建立分割槽表語句,使用關鍵字partition

a、單分割槽建表語句:

create

table table_name (id int, content string) partitioned by (dt string);

單分割槽表,按天分割槽,在表結構增加了dt列。

以dt為資料夾區分

b、 雙分割槽建表語句:

create

table table_name (id int, content string) partitioned by (dt string, hour string);

雙分割槽表,按天和小時分割槽,在表結構中新增加了dt和hour兩列。

先以dt為資料夾,再以hour子資料夾區分

新增分割槽表語法

如果表已建立,在此基礎上新增分割槽):

alter table table_name add

partition_spec [ location 'location1' ]

partition_spec [ location 'location2' ] ...

如:alter table table_name add

partition (dt='2016-12-06', hour='16')

location '/usr/resource/aa.txt'

刪除分割槽語法:

alter table table_name drop

partition_spec, partition_spec,...

使用者可以用 alter table drop partition 來刪除分割槽。分割槽的元資料和資料將被一併刪除。

如:

alter

table table_name drop partition (dt='2016-12-06', hour='16');

資料載入進分割槽表中語法:

load data [local] inpath 'filepath' [overwrite] into

table tablename [partition (partcol1=val1, partcol2=val2 ...)]

如:方式一:load data inpath '/usr/resource/aa.txt'

into

table table_name partition(dt='2016-12-06', hour='16');

方式二:load data local inpath '/usr/resource/aa.txt'

into

table table_name partition(dt='2016-12-06');

方式三:load data local inpath '/usr/resource/aa.txt' **overwrite** into

table table_name partition(dt='2016-12-06');

基於分割槽的查詢的語句:

select table_name.* from table_name where table_name.dt>= '2016-12-06';

select * from table_name where dt='2016-12-06'

and cycle= 'active_data';

檢視表有多少分割槽的語句(partitions 關鍵字):

hive> show partitions table_name; 

ok dt=2016-12-06/hour=16

dt=2016-12-06/hour=17

dt=2016-12-06/hour=18

hive表分割槽

hive對錶有分割槽的功能。hive根據表的列屬性值 諸如日期,城市 部門對錶進行分割槽 使用分割槽,能夠很容易查詢 到資料的一部分。表或分割槽被分為 buckets 桶 以便為資料 提供額外的資料結構,該資料結構被 用於提公升 查詢的有效性。buckets 桶 的工作依賴於表中某個列的 雜湊函式值...

Hive 表分割槽

基本知識 hadoop fs rmr hdfs hdfs hadoop fs put user hive warehouse data.txt hdfs hdfs data.txt 建立分割槽表 外部表 create external table if not exists employee id ...

Hive 表分割槽

基本知識 hadoop fs rmr hdfs hdfs hadoop fs put user hive warehouse data.txt hdfs hdfs data.txt 建立分割槽表 外部表 create external table if not exists employee id ...