1. 程式人生 > >一文搞定hive之insert into 和 insert overwrite與資料分割槽

一文搞定hive之insert into 和 insert overwrite與資料分割槽

資料分割槽

        資料庫分割槽的主要目的是為了在特定的SQL操作中減少資料讀寫的總量以縮減響應時間,主要包括兩種分割槽形式:水平分割槽與垂直分割槽。水平分割槽是對錶進行行分割槽。而垂直分割槽是對列進行分割槽,一般是通過對錶的垂直劃分來減少目標表的寬度,常用的是水平分割槽。

        hive建立分割槽語法:       

create external table if not exists tablename(

        a string,

        b string)

 partitioned by (year string,month string)

 row format delimited fields terminated by ',';

        hive通常有三種方式對包含分割槽欄位的表進行資料插入:

        1)靜態插入資料:要求插入資料時指定與建表時相同的分割槽欄位,如:

insert overwrite tablename (year='2017', month='03') select a, b from tablename2;

        2)動靜混合分割槽插入:要求指定部分分割槽欄位的值,如:

insert overwrite tablename (year='2017', month) select a, b from tablename2;

        3)動態分割槽插入:只指定分割槽欄位,不用指定值,如:

insert overwrite tablename (year, month) select a, b from tablename2;

        hive動態分割槽設定相關引數:

Hive.exec.dynamic.partition  是否啟動動態分割槽。false(不開啟) true(開啟)預設是 false

hive.exec.dynamic.partition.mode  開啟動態分割槽後,動態分割槽的模式,有 strict和 nonstrict 兩個值可選,strict 要求至少包含一個靜態分割槽列,nonstrict則無此要求。各自的好處,大家自己檢視哈。

hive.exec.max.dynamic.partitions 允許的最大的動態分割槽的個數。可以手動增加分割槽。預設1000

hive.exec.max.dynamic.partitions.pernode 一個 mapreduce job所允許的最大的動態分割槽的個數。預設是100

資料插入之insert into 和 insert overwrite

hive是基於Hadoop的一個數據倉庫工具,可以將結構化的資料檔案對映為一張資料庫表,並提供簡單的sql查詢功能,可以將sql語句轉換為MapReduce任務進行執行。通常hive包括以下四種資料匯入方式:

(1)、從本地檔案系統中匯入資料到Hive表;

(2)、從HDFS上匯入資料到Hive表;

(3)、在建立表的時候通過從別的表中查詢出相應的記錄並插入到所建立的表中;

(4)、從別的表中查詢出相應的資料並匯入到Hive表中。

  • INSERT INTO

  1. 使用樣例

                insert into table tablename1 select a, b, c from tablename2;

  • INSERT OVERWRITE

  1. 使用樣例

insert overwrite table tablename1 select a, b, c from tablename2;

  • 兩者的異同

        insert into 與 insert overwrite 都可以向hive表中插入資料,但是insert into直接追加到表中資料的尾部,而insert overwrite會重寫資料,既先進行刪除,再寫入。如果存在分割槽的情況,insert overwrite會只重寫當前分割槽資料。