1. 程式人生 > >Hive動態分割槽與建表、插入資料操作

Hive動態分割槽與建表、插入資料操作

1、定義

動態分割槽指不需要為不同的分割槽新增不同的插入語句,分割槽不確定,需要從資料中獲取

set hive.exec.dynamic.partition=true;//使用動態分割槽

(可通過這個語句檢視:set hive.exec.dynamic.partition;) 

set hive.exec.dynamic.partition.mode=nonstrict;//無限制模式

如果模式是strict,則必須有一個靜態分割槽,且放在最前面。 

SET hive.exec.max.dynamic.partitions.pernode=10000;每個節點生成動態分割槽最大個數 

set hive.exec.max.dynamic.partitions=100000;,生成動態分割槽最大個數,如果自動分割槽數大於這個引數,將會報錯 

set hive.exec.max.created.files=150000; //一個任務最多可以建立的檔案數目 

set dfs.datanode.max.xcievers=8192;//限定一次最多開啟的檔案數set dfs.datanode.max.xcievers=8192;//限定一次最多開啟的檔案數

2、建立靜態分割槽表與動態分割槽表在hql語句上沒有本質區別,主要區別在於hive.exec.dynamic.partition.mode設定

例:

CREATE TABLE order (

    nameSTRING,

    id STRING

)

PARTITIONED BY (monthstring,info string)

ROW FORMAT DELIMITED FIELDS TERMINATED BY'\t';

3、動態分割槽表插入資料

Strict模式,需要有一個靜態分割槽,且放在最前面

Insert overwritetable order(month=’2016-06’,info)

Select name,id,info(或者 substr(describtion,2,10) as info)from order;

info(或者 substr(describtion,2,10) as info):

動態分割槽的欄位info可以是表中的Field,也可以是(alias)別名。

實際操作:

建立分割槽表:

creat tableTest_Info(

  name string,

  id string

  )

  Partitioned by(year string,month string,daystring,info string)

向分割槽表中插入資料:

set mapred.reduce.tasks = 10;

set mapred.job.priority=VERY_HIGH;

insert overwrite table test_infopartition(year="2016",month="06",day="13",info)

select deviceid as name,uuid as id,ip asinfo from ad_install limit 100;

向test_info插入資料時,ad_install的欄位不允許錯位,否則資料有誤,但是名稱可以不一致。

插入結果:

在/usr/deployer/warehouse/tmp.db/test_info下會出現year=2016資料夾,其下會有month=06資料夾,再其下會有day=13資料夾下的內容如下圖:


清除表的資料:

truncate table test_info;

檢視建立表的語句:

show create table test_info;

刪除表:

drop table test_info;

建立資料庫,指定資料庫儲存路徑,預設為hive的metastore的路徑:

create database if not exists lin 
location '/tmp/data';

檢視資料庫:

describe database lin;

hive建立外部表和內表的區別就是表文件夾的位置,外部表可以設定location,並且在刪除表的時候內部表能夠將資料夾刪除,而外部表只能刪除資料,無法刪除資料夾