好程序員大數據培訓分享Hive的靜態分區與動態分區

  好程序員大數據培訓分享Hive的靜態分區與動態分區:分區是hive存放數據的一種方式。將列值作爲目錄來存放數據,就是一個分區。這樣查詢時使用分區列進行過濾,只需根據列值直接掃描對應目錄下的數據,不掃描其他不關心的分區,快速定位,提高查詢效率。分動態和靜態分區兩種:
  1. 靜態分區:若分區的值是確定的,那麼稱爲靜態分區。新增分區或者是加載分區數據時,已經指定分區名。
  create table if not exists day_part1(
  uid int,
  uname string
  )
  partitioned by(year int,month int)
  row format delimited fields terminated by 't';
  ##加載數據指定分區
  load data local inpath '/root/Desktop/student.txt' into table day_part1
partition(year=2017,month=04);
  ##新增分區指定分區名
  alter table day_part1 add partition(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 動態分區的操作
  ##創建臨時表
  create table if not exists tmp
  (uid int,
  commentid bigint,
  recommentid bigint,
  year int,
  month int,
  day int)
  row format delimited fields terminated by 't';
  ##加載數據
  load data local inpath '/root/Desktop/comm' into table tmp;
  ##創建動態分區表
  create table if not exists dyp1
  (uid int,
  commentid bigint,
  recommentid bigint)
  partitioned by(year int,month int,day int)
  row format delimited fields terminated by 't';
  ##嚴格模式
  insert into table dyp1 partition(year=2016,month,day)
  select uid,commentid,recommentid,month,day from tmp;
  ##非嚴格模式
  ##設置非嚴格模式動態分區
  set hive.exec.dynamic.partition.mode=nostrict;
  ##創建動態分區表
  create table if not exists dyp2
  (uid int,
  commentid bigint,
  recommentid bigint)
  partitioned by(year int,month int,day int)
  row format delimited fields terminated by 't';
  ##爲非嚴格模式動態分區加載數據
  insert into table dyp2 partition(year,month,day)
  select uid,commentid,recommentid,year,month,day from tmp;
  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)、對order by查詢,有order by的查詢不帶limit語句。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章