cloudera公司發行的CDH中
hive的有三種角色:gateway、hiveserver二、hive metastore三種角色
其中hive metastore主要用於客戶端鏈接 默認端口
hive server2 主要用於jdbc鏈接(不少bi產品都靠jdbc鏈接,好比hue,帆軟等軟件)
gateway 是client的一個代理,主要用於更新hive的客戶端配置
apache社區的hive中也分爲客戶端,hiveserver2和hive metastore
啓動 metastore : hive --service metastore
啓動hive :hive --service hiveserver2
理論:chrome
什麼是hive:apache
1. Hive旨在實現輕鬆的數據彙總,即時查詢和分析大量數據。bash
2. 它提供了SQL,使用戶能夠輕鬆地進行臨時查詢,彙總和數據分析。網絡
3. Hive可使用用戶定義函數(UDF)。app
4. 使用率:實際開發中,80%操做使用Hive完成,20%使用MapReduce。負載均衡
5. 與MapReduce相比,Hive特色:穩定、代碼精簡、易於維護。函數
6. HiveQL:未嚴格實現SQL-92標準。chrome-extension
7. 本質:將HiveQL轉化爲一個或多個MapReduce做業並在集羣上運行,但並非全部HiveQL都會轉爲MapReduce做業。url
常見問題:spa
一、hive 內部表:加載數據到 hive 所在的 hdfs 目錄,刪除時,元數據和數據文件都刪除
hive外部表:不加載數據到 hive 所在的 hdfs 目錄,刪除時,只刪除表結構
二、分區做用:防止數據傾斜
三、sort by和order by之間的區別?distribute by?cluster by? group by?
sort by 是單個reduce內部的排序; order by 是全局排序,只觸發一個reduce distribute by :按照指定的字段對數據進行劃分輸出到不一樣的reduce中,一般與sort by 連用 cluster by :除了具備distribute by 的功能外還兼具sort by 的功能 group by 一般用於作聚合函數操做 count(*) sum(xx)
4.Hive中追加導入數據的4種方式是什麼
#hdfs
load data inpath 'hdfs://xxx/xxx' into (overwrite) table a #本地
load data local inpath '/xxx/xx' into (overwrite) table a
#查詢導入
create table student as select * from student1;
#查詢結果導入
insert (overwrite) into table student select user_id,user_name from student2;
5.hive導出數據
#導出到hdfs 會遞歸建立文件夾 (注意文件夾若是存在,數據會被清空)高危操做 insert overwrite directory '/liuzhimin/test2/a' row format delimited fields terminated by '\t' select * from u_data_new; #導出到本地(高危操做) insert overwrite local directory '/home/dip/a' row format delimited fields terminated by '\t' select * from u_data_new; #bash hive -e "use cslc; select * from u_data_new ;"> a.txt
6.數據傾斜怎麼辦?
解決辦法:①參數調節: set hive.map.aggr=true set hive.groupby.skewindata=true 有數據傾斜的時候進行負載均衡,當選項設定爲true,生成的查詢計劃會有兩個MR Job。第一個MR Job中,Map的輸出結果集合會隨機分佈到Reduce中,每一個Reduce作部分聚合操做,
並輸出結果,這樣處理的結果是相同Group By Key有可能被分發到不一樣的Reduce中,從而達到負載均衡的目的;
第二個MR Job在根據預處理的數據結果按照 Group By Key 分佈到Reduce中(這個過程能夠保證相同的 Group By Key 被分佈到同一個Reduce中),最後完成最終的聚合操做。 ②SQL語句調節: 大小表join,小表進內存; 大表Join大表:把空值的Key變成一個字符串加上一個隨機數,把傾斜的數據分到不一樣的reduce上,因爲null值關聯不上,處理後並不影響最終的結果 count distinct大量相同特殊值:count distinct時,將值爲空的狀況單獨處理,若是是計算count distinct,能夠不用處理,直接過濾,在作後結果中加1。
若是還有其餘計算,須要進行group by,能夠先將值爲空的記錄單獨處理,再和其餘計算結果進行union.