好程序員大數據開發之掌握Hive的靜態分區與動態分區

  分區是hive存放數據的一種方式。將列值做爲目錄來存放數據,就是一個分區。這樣查詢時使用分區列進行過濾,只需根據列值直接掃描對應目錄下的數據,不掃描其餘不關心的分區,快速定位,提升查詢效率。分動態和靜態分區兩種:node

  1.靜態分區:若分區的值是肯定的,那麼稱爲靜態分區。新增分區或者是加載分區數據時,已經指定分區名。服務器

  createtableifnotexistsday_part1(app

  uidint,ui

  unamestringorm

  )string

  partitionedby(yearint,monthint)it

  rowformatdelimitedfieldsterminatedby''io

  ;table

  ##加載數據指定分區form

  loaddatalocalinpath'/root/Desktop/student.txt'intotableday_part1partition(year=2017,month=04);

  ##新增分區指定分區名

  altertableday_part1addpartition(year=2017,month=1)partition(year=2016,month=12);

  2.動態分區:分區的值是非肯定的,由輸入數據來肯定

  2.1動態分區的相關屬性:

  hive.exec.dynamic.partition=true:是否容許動態分區

  hive.exec.dynamic.partition.mode=strict:分區模式設置

  strict:最少須要有一個是靜態分區

  nostrict:能夠所有是動態分區

  hive.exec.max.dynamic.partitions=1000:容許動態分區的最大數量

  hive.exec.max.dynamic.partitions.pernode=100:單個節點上的mapper/reducer容許建立的最大分區

  2.2動態分區的操做

  ##建立臨時表

  createtableifnotexiststmp(

  uidint,

  commentidbigint,

  recommentidbigint,

  yearint,

  monthint,

  dayint

  )

  rowformatdelimitedfieldsterminatedby'';

  ##加載數據

  loaddatalocalinpath'/root/Desktop/comm'intotabletmp;

  ##建立動態分區表

  createtableifnotexistsdyp1(

  uidint,

  commentidbigint,

  recommentidbigint

  )

  partitionedby(yearint,monthint,dayint)

  rowformatdelimitedfieldsterminatedby''

  ;

  ##嚴格模式

  insertintotabledyp1partition(year=2016,month,day)

  selectuid,commentid,recommentid,month,dayfromtmp;

  ##非嚴格模式

  ##設置非嚴格模式動態分區

  sethive.exec.dynamic.partition.mode=nostrict;

  ##建立動態分區表

  createtableifnotexistsdyp2(

  uidint,

  commentidbigint,

  recommentidbigint

  )

  partitionedby(yearint,monthint,dayint)

  rowformatdelimitedfieldsterminatedby'';

  ##爲非嚴格模式動態分區加載數據

  insertintotabledyp2partition(year,month,day)

  selectuid,commentid,recommentid,year,month,dayfromtmp;

  3.分區注意細節

  (1)、儘可能不要是用動態分區,由於動態分區的時候,將會爲每個分區分配reducer數量,當分區數量多的時候,reducer數量將會增長,對服務器是一種災難。

  (2)、動態分區和靜態分區的區別,靜態分區無論有沒有數據都將會建立該分區,動態分區是有結果集將建立,不然不建立。

  (3)、hive動態分區的嚴格模式和hive提供的hive.mapred.mode的嚴格模式。

  hive提供咱們一個嚴格模式:爲了阻止用戶不當心提交惡意hql

  hive.mapred.mode=nostrict:strict

  若是該模式值爲strict,將會阻止如下三種查詢:

  (1)、對分區表查詢,where中過濾字段不是分區字段。

  (2)、笛卡爾積join查詢,join查詢語句,不帶on條件或者where條件。

  (3)、對orderby查詢,有orderby的查詢不帶limit語句。

相關文章
相關標籤/搜索