Review Hive 概念、意義、特性、缺點、內部組成架構、安裝部署、訪問方式、數據庫的基本操作、數據表基本操作、自定義函數、UDF、修改表信息、表數據加載、數據導出等等

Hive基本概念

是一個基於hadoop的數據倉庫工具,可以將結構化數據映射成一張數據表,並提供類SQL的查詢功能。

 

Hive的意義是什麼

背景:hadoop是個好東西,但是學習難度大,成本高,坡度陡。
意義(目的):降低程序員使用hadoop的難度。降低學習成本。
 

Hive可以對數據進行存儲與計算

存儲使用HDFS存儲
計算使用MapReduce進行計算。

 

Hive的特性

1、擴展性 : Hive的擴展性與集羣的擴展性相同
2、延展性:Hive支持用戶自定義函數,編寫符合自己業務需求的函數。
3、容錯性:節點出現問題(宕機/斷電)SQL仍可完成執行

 

Hive缺點

每次的執行開銷較大,任務運行時間較長,延時較高。

 

Hive的內部組成架構

             

元數據:描述數據的數據(屬性)
表名稱、字段名,字段的數據類型。
內部執行流程
解釋器 -> 編譯器 -> 優化器 -> 執行器

 

數據格式:Hive中沒有定義專門的數據格式 數據格式部分自定義:

列分隔符(通常爲空格、”\t”、”\x001″)
行分隔符(”\n”)
讀取文件數據的方法(Hive 中默認有三個文件格式 TextFile,SequenceFile 以及 RCFile)。

Hive在加載數據的過程就是拷貝數據的過程,不需要特定的轉化。不會對數據本身進行任何修改,甚至不會對數據 進行掃描。

Hive 中不支持對數據的改寫和添加(對文本內數據的添加)

Hive 在加載數據的過程中不會對數據中的某些 Key 建立索引。所以Hive不適合在線數據查詢(要求相應速度快)

 

總結:hive具有sql數據庫的外表,但應用場景完全不同,hive只適合用來做批量數據統計分析。

 

hive支持的數據格式

可支持Text,
SequenceFile,
ParquetFile,
ORC格式
RCFILE等

 

Hive元數據

DB、數據庫
Table,內部表
External Table,外部表
Partition,分區
Bucket,分桶
 

Hive安裝部署

1、derby版hive直接使用
每個節點自己維護自己的元數據庫(derby),導致多個節點之間元數據不共享。
2、mysql共享hive元數據
2.1安裝mysql
mysq不推薦使用RPM安裝,建議使用yum進行安裝
yum install -y mysql mysql-server mysql-devel
/etc/init.d/mysqld start
配置遠程連接(root 只用123456密碼)
grant all privileges on . to 'root'@'%' identified by '123456' with grant option;
配置mysql用戶 root 密碼是123456
update user set password=password('123456') where user='root';
2.2 安裝HIve
現在第一個節點解壓hive的安裝包。
在解壓後的目錄conf中修改配置文件
hive-env.sh
配置HADOOP_HOME
配置HIVE_CONF_DIR
hive-site.xml
添加連接Mysql的配置
將解壓後的文件夾拷貝到其他節點
配置環境變量
將mysql-connector-java-5.1.38.jar 上傳到HIVE的lib目錄下。
 

Hive的訪問方式

1、在Hive客戶端,配置hive到環境變量的前提下,在節點的任意位置 直接數據hive + 回車

2、
啓動hiveserver2 服務
hive --service hiveserver2
進行連接
進入beelin的shell窗口
連接hiveserver2服務
!connect jdbc:hive2://node001:10000
輸入root 和密碼 123456
0: jdbc:hive2://node001:10000> 0: jdbc:hive2://node001:10000>
0: jdbc:hive2://node001:10000> show databases;
+----------------+--+ | database_name | +----------------+--+ | default | | myhive | +----------------+--+

 

Hive傳選項

hive -e '操作命令'
hive -e 'show databases;'
hive -f 文件名(文件內是操作命令)
hive -f test.sql
 

數據庫 的基本操作

1、數據庫的增刪改查
增 : create database [ if not exists ] myhive ;
刪 : drop database myhive ; (數據庫內沒有表可以刪除,有表不能刪除)
改 :數據庫不允許修改
查 :show databases;
查看詳細信息:
desc database myhive2;
desc database extended myhive2;
數據庫的切換:
use myhive(數據庫名);

 

hive的數據庫、表、分區在HDFS的表現形式是文件夾

數據庫的默認路徑:/user/hive/warehouse

自定義hive數據庫的路徑:create database myhive2 location '/myhive2

 

數據 的基本操作(增刪改查)

 

創建基本數據表(內部表):


create table tableName(字段名稱 字段類型,字段名稱 字段類型)


create table tableName(字段名稱 字段類型,字段名稱 字段類型)
ROW FORMAT DELIMITED IELDS TERMINATED BY char (char分隔符)
指定數據中字段與字段的分隔符 ‘\t’ 或 ',' 或 '|' 或其他


創建外部數據表:


create EXTERNAL table tableName(字段名稱 字段類型,字段名稱 字段類型)


建外部表需要指定數據的存儲路徑。通過LOCATION進行指定。

 

內部表與外部表的區別:

在刪除內部表時:內部表刪除將表的元數據和數據同時刪除。
在刪除外部表時:外部表的元數據被刪除,數據本身不刪除。

 

刪除表

drop table tablename;

修改表

alter tablename ***

查詢表

show tables;

desc tablename;

 

Hive的數據類型

基本數據類型
INT BIGINT FLOAT DOUBLE DEICIMAL STRING VARCHAR CHAR BINARY TIMESTAMP DATE INTERVAL ARRAY
複雜數據類型
MAP STRUCT UNION

 

create table stu3 as select * from stu2; 複製數據複試表結構

create table stu4 like stu2; 不復制數據複試表結構。

 

加載數據

從linux中加載數據到hive
load data local inpath ‘數據路徑’ into table 表名;
從hdfs中加載數據到hive,並覆蓋
load data inpath ‘數據路徑’ overwrite into table 表名;

 

外部表

create external table techer (t_id string,t_name string) row format delimited fields terminated by '\t';
加載數據
load data local inpath '/export/servers/hivedatas/techer .csv' into table techer ;
在hdfs查看錶中的數據
hadoop fs -ls /user/hive/warehouse/myhive.db/techer
在hive中查詢
select * from techer
刪除數據表techer
drop table techer;
再次查看
hadoop fs -ls /user/hive/warehouse/myhive.db/techer(數據依然存在)

 

內部表

create table student(t_id string,t_name string) row format delimited fields terminated by '\t';
加載數據
load data local inpath '/export/servers/hivedatas/student .csv' into table student;
在hdfs查看錶中的數據
hadoop fs -ls /user/hive/warehouse/myhive.db/student
在hive中查詢
select * from student
刪除數據表techer
drop table student;
再次查看
hadoop fs -ls /user/hive/warehouse/myhive.db/student(數據不存在)

 

分區表

常見的分區規則:按天進行分區(一天一個分區)


創建分區表的語句
create table score(s_id string,c_id string,s_score int) partitioned by (month string) row format delimited
fieldsterminated by '\t';
create table score2 (s_id string,c_id string,s_score int) partitioned by (year string,month string,day string)
row formatdelimited fields terminated by '\t';
數據加載
load data local inpath '/opt/hive/score.csv' into table score partition (month='201806');
load data local inpath '/opt/hive/score.csv' into table score2 partition(year='2018',month='06',day='02');

 

特別強調:

分區字段絕對不能出現在數據表以有的字段中。

作用:

將數據按區域劃分開,查詢時不用掃描無關的數據,加快查詢速度。

 

分桶表

是在已有的表結構之上新添加了特殊的結構
開啓hive的桶表功能
set hive.enforce.bucketing=true;
設置桶(reduce)的個數
set mapreduce.job.reduces=3;
建分桶表
create table course (c_id string,c_name string,t_id string) clustered by(c_id) into 3 buckets row format
delimited fields terminated by '\t';
創建基本表
create table course_common (c_id string,c_name string,t_id string) row format delimited fields terminated by
'\t';
基本表添加數據
load data local inpath '/export/servers/hivedatas/course.csv' into table course_common;
在基本表中查詢數據插入到分桶表
insert overwrite table course select * from course_common cluster by(c_id);
確認分桶內的數據
[root@node01 hive]# hadoop fs -cat /user/hive/warehouse/course/000000_0 03 英語 03 [root@node01 hive]#
hadoop fs -cat /user/hive/warehouse/course/000001_0 01 語文 02 [root@node01 hive]# hadoop fs -cat
/user/hive/warehouse/course/000002_0 02 數學 01
 

特別強調:

分桶字段必須是表中的字段。

分桶邏輯:

對分桶字段求哈希值,用哈希值與分桶的數量取餘,餘幾,這個數據就放在那個桶內。

 

分桶的作用和好處

1、對於join的需求,能夠起到優化加速的作用。(前提是,join字段設置爲分桶字段)

2、用於數據取樣(獲取/提取數據樣本)

將數據編號作爲分桶字段。這樣每個桶內各種“級別”的數據都有。

 

Hive的自定義函數

函數查看
show functions;
show function 函數名 ;
desc function extended upper;
UDF UDAF UDTF
一進一出 多進一出 一進多出

 

UDF函數

1、創建一個class 繼承UDF
2 編寫evaluate函數,在這裏編寫業務需求需要的代碼
3 打成jar包,並上傳
4 將jar包添加到hive
在hive shell 內 add jar 路徑+jar包
5 創建臨時函數(永久的函數將temporary 刪掉)
create temporary function 新的函數的名稱 as 業務代碼所在的包名-類名;
6 調用 驗證;

 

Hive通過reflect調用java方法

1、使用純java代碼編寫業務邏輯,並打包上傳
2、將jar包添加到hive
在hive shell 內 add jar 路徑+jar包
3、調用
select reflect (‘參數一’,‘參數二’,‘參數三’)
參數一 包名-類名
參數而 方法名
參數三 需要計算的數據
 

修改表信息

修改表明
alter table old_table_name rename to new_table_name;
查看錶結構
desc tablename;
添加列
alter table score5 add column (mycol string, mysco string);
 

Hive表數據加載

五種情況
1、直接向分區表中插入數據
insert into table score3 partition(month ='201807') values ('001','002','100');
2、通過查詢插入數據
(linux ) load data local inpath '/export/servers/hivedatas/score.csv' overwrite into table score
partition(month='201806');
(HDFS) load data inpath '/export/servers/hivedatas/score.csv' overwrite into table score
partition(month='201806');
3、多插入模式
from score
insert overwrite table score_first partition(month='201806') select s_id,c_id
insert overwrite table score_second partition(month = '201806') select c_id,s_score;
4、查詢語句中創建表並加載數據(as select)
create table score5 as select * from score;
5、創建表時通過location指定加載數據路徑
create external table score6 (s_id string,c_id string,s_score int) row format delimited fields terminated by '\t'
location '/myscore6';
 

Hive數據的導出

7種方法
1、將查詢的結果導出到本地
insert overwrite local directory '/export/servers/exporthive/a' select * from score;
2、將查詢的結果格式化導出到本地
insert overwrite local directory '/export/servers/exporthive' row format delimited fields terminated by '\t'
collection items terminated by '#' select * from student;
3、將查詢的結果導出到HDFS上(沒有local)
insert overwrite directory '/export/servers/exporthive' row format delimited fields terminated by '\t' collection
items terminated by '#' select * from score;
4、Hadoop命令導出到本地
dfs -get /export/servers/exporthive/000000_0 /export/servers/exporthive/local.txt;
5 、 hive shell 命令導出
bin/hive -e "select * from yhive.score;" > /export/servers/exporthive/score.txt
6、export導出到HDFS上(全表導出)
export table score to '/export/exporthive/score';
7、SQOOP導出(以後會有介紹)

 

清空hive數據表

truncate table score5

 

Hive查詢語句

like
select * from tablename where name like '張%'
Rlike匹配正則
select * from tablename where name rlike '[]'
通配符
% 多個 100
_ 一個 1
 

Hive修改配置文件優先級

1、通過配置文件及逆行設置
2、hive進入shell 時,加參數 -hiveconf(命令行參數)
hive -hiveconf hive.root.logger=INFO;
3、在Hiveshell 內進行設置(參數聲明)
set mapred.reduce.tasks=100;


參數聲明 > 命令行參數 > 配置文件

 

壓縮算法

 

Hive支持的數據存儲格式

 

可支持Text,
SequenceFile,
ParquetFile,
ORC格式

RCFILE等
在實際的項目開發當中,hive表的數據存儲格式一般選擇:orc或parquet。壓縮方式一般選擇snappy。

 

Hive的優化

1、 Fetch抓取

設置屬性 set hive.fetch.task.conversion=none; 所有的查詢語句都要轉化成MR程序。
set hive.fetch.task.conversion=more; 簡單的查詢不會轉化成MR程序(select * from T,select id from T limit
N,)


什麼是本地計算

數據存儲到HDFS後,編寫分析代碼實現計算程序,程序在進行分發時,優先分發放到這個程序所使用到的數據所在
的節點上。


2、本地模式

任務在提交SQL語句的節點上“本地”執行,任務不會分配到集羣。
作用或好處:在小數據量的前提下,提高查詢效率

3、數據傾斜

當一個key數據過大時就會數據傾斜。
開啓局(Map)部聚和功能,開啓局部聚和後,hive會創建兩個MR,程序,第一個進行數據的局部聚和,第二個進行
數據的最終彙總。

4、Count(distinct)

    先去重,再求總數量
SELECT count(DISTINCT id) FROM bigtable;
替換方案 SELECT count(id) FROM (SELECT id FROM bigtable GROUP BY id) a;

儘量避免笛卡爾積,在join時加on 條件

5、分區剪裁、列剪裁

分區剪裁: 只拿需要的分區,用什麼那什麼。
列剪裁: 只拿需要的列,用什麼那什麼。

先關聯再過濾

SELECT a.id FROM bigtable a LEFT JOIN ori b ON a.id = b.id
WHERE b.id <= 10;
優化方案 先過濾再關聯
1、SELECT a.id FROM ori a LEFT JOIN bigtable b
ON (b.id <= 10 AND a.id = b.id);
2、SELECT a.id FROM bigtable a RIGHT JOIN (SELECT id FROM ori WHERE id <= 10 ) b ON a.id = b.id;

 

6、動態分區調整

以第一個表的分區規則,來對應第二個表的分區規則,將第一個表的所有分區,全部拷貝到第二個表中來,第二個 表在加載數據的時候,不需要指定分區了,直接用第一個表的分區即可

7、數據傾斜

如何解決數據傾斜問題思路:衆人拾柴火焰高(將一個大任務拆分成多個小任務,再次執行)
前提:設置reduce數量10或更多
1、distribute by (字段)
2、distribute by rand()

8、影響Map的數量

1、文件很小時,影響map數量的因素是文件的數量
2、文件很大時,影響map數量的因素是塊的數量

9、影響reduce的數量

參數1、每個Reduce處理的數據量
參數2、每個任務最大的reduce數
計算reducer數的公式
reduce數量=min(參數2,總輸入數據量/參數1)

10、並行執行

HIVE將SQL轉化成MapReduce,需要有 Map 階段,Reduce 階段,抽樣階段、合併階段、limit階段。
開啓並行執行,使得多個沒有依賴關係的任務同時執行。一起到提高查詢效率的作用。


11、嚴格模式


不允許執行以下操作
1、 不允許掃描所有分區
2、使用了order by語句的查詢,要求必須使用limit語句
3、限制笛卡爾積的查詢


12、jvm重用


允許多個task使用一個jvm
好處:降低任務啓動的開銷,提高任務的執行效率。
不足:整個任務結束前jvm 不釋放,長時間佔用。導致資源不足時,資源(佔用而不是用)浪費。


13、 推測執行


MapReduce的推測執行

 

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