Hive動態分區和分桶
1、Hive動態分區
1、hive的動態分區介紹
hive的靜態分區需要用戶在插入數據的時候必須手動指定hive的分區字段值,但是這樣的話會導致用戶的操作復雜度提高,而且在使用的時候會導致數據只能插入到某一個指定分區,無法讓數據散列分布,因此更好的方式是當數據在進行插入的時候,根據數據的某一個字段或某幾個字段值動態的將數據插入到不同的目錄中,此時,引入動態分區。
2、hive的動態分區配置
--hive設置hive動態分區開啟
set hive.exec.dynamic.partition=true;
默認:true
--hive的動態分區模式
set hive.exec.dynamic.partition.mode=nostrict;
默認:strict(至少有一個分區列是靜態分區)
--每一個執行mr節點上,允許創建的動態分區的最大數量(100)
set hive.exec.max.dynamic.partitions.pernode;
--所有執行mr節點上,允許創建的所有動態分區的最大數量(1000)
set hive.exec.max.dynamic.partitions;
--所有的mr job允許創建的文件的最大數量(100000)
set hive.exec.max.created.files;
3、hive動態分區語法
--Hive extension (dynamic partition inserts):
INSERT OVERWRITE TABLE tablename PARTITION (partcol1[=val1], partcol2[=val2] ...) select_statement FROM from_statement;
INSERT INTO TABLE tablename PARTITION (partcol1[=val1], partcol2[=val2] ...) select_statement FROM from_statement;
2、Hive分桶
1、Hive分桶的介紹
Bucketed tables are fantastic in that they allow much more efficient sampling than do non-bucketed tables, and they may later allow for time saving operations such as mapside joins. However, the bucketing specified at table creation is not enforced when the table is written to, and so it is possible for the table's metadata to advertise properties which are not upheld by the table's actual layout. This should obviously be avoided. Here's how to do it right.
注意:
1、Hive分桶表是對列值取hash值得方式,將不同數據放到不同文件中存儲
2、對於hive中每一個表、分區都可以進一步進行分桶
3、由列的hash值除以桶的個數來決定每條數據划分在哪個桶中
2、Hive分桶的配置
--設置hive支持分桶
set hive.enforce.bucketing=true;
3、Hive分桶的抽樣查詢
--案例
select * from bucket_table tablesample(bucket 1 out of 4 on columns)
--TABLESAMPLE語法:
TABLESAMPLE(BUCKET x OUT OF y)
x:表示從哪個bucket開始抽取數據
y:必須為該表總bucket數的倍數或因子