
一、Hive簡(jiǎn)介Hive 是一個(gè)框架可以通過(guò)編寫(xiě)sql的方式自動(dòng)的編譯為MR任務(wù)的一個(gè)工具。在這個(gè)世界上會(huì)寫(xiě)SQL的人遠(yuǎn)遠(yuǎn)大于會(huì)寫(xiě)java代碼的人所以假如可以將MR通過(guò)sql實(shí)現(xiàn)這個(gè)將是一個(gè)巨大的市場(chǎng)FaceBook就這么干。臉書(shū)在大數(shù)據(jù)中發(fā)展趨勢(shì)所有的技術(shù)全部都變?yōu)镾QL。1、Hive是一個(gè)數(shù)據(jù)倉(cāng)庫(kù)工具2、可以將數(shù)據(jù)加載到表中編寫(xiě)sql進(jìn)行分析3、底層依賴Hadoop所以每一次都需要啟動(dòng)hadoop(hdfs以及yarn)4、Hive的底層計(jì)算框架可以使用MR、也可以使用Spark、TEZ5、Hive不是數(shù)據(jù)庫(kù)而是一個(gè)將MR包了一層殼兒。類似于一個(gè)中介。Hive官網(wǎng)地址Apache HiveGitHub地址 GitHub - apache/hive: Apache Hive · GitHub文檔查看地址https://cwiki.apache.org/confluence/display/Hive/GettingStarted目前我們可以采用穩(wěn)定版realease4.0.1Hive天然的就是當(dāng)做數(shù)據(jù)倉(cāng)庫(kù)使用的。什么是數(shù)據(jù)倉(cāng)庫(kù)?數(shù)據(jù)倉(cāng)庫(kù)不是某一款存儲(chǔ)軟件是面向分析、經(jīng)過(guò)統(tǒng)一清洗整合的數(shù)據(jù)集 整套架構(gòu)存儲(chǔ)只是它的底座Oracle 是關(guān)系型數(shù)據(jù)庫(kù)可作為小型數(shù)倉(cāng)的承載載體Hive 是大數(shù)據(jù)生態(tài)下構(gòu)建分布式數(shù)倉(cāng)的 SQL 查詢引擎為了高效取用數(shù)據(jù)數(shù)倉(cāng)必須做分層設(shè)計(jì)這套分層、表結(jié)構(gòu)、維度事實(shí)表的設(shè)計(jì)工作就是數(shù)據(jù)建模和實(shí)體倉(cāng)庫(kù)分區(qū)管理邏輯相二、Hive體系結(jié)構(gòu)圖注意- 包含*的全表查詢比如select * from table 不會(huì)生成MapRedcue任務(wù)- 包含*的limit查詢比如select * from table limit 3 不會(huì)生成MapRedcue任務(wù)三、本地模式安裝下載hive安裝包并上傳hive的下載地址(包含歷史歸檔版本)https://archive.apache.org/dist/hive/上傳壓縮包 /opt/modules解壓tar -zxvf apache-hive-4.0.1-bin.tar.gz -C /opt/installs/重命名mv apache-hive-4.0.1-bin hive配置hive環(huán)境變量配置環(huán)境變量vi /etc/profile.d/myenv.shexport HIVE_HOME/opt/installs/hiveexport PATH$PATH:$HIVE_HOME/bin刷新環(huán)境變量source /etc/profile配置hive-env.sh配置hive-env.sh進(jìn)入這個(gè)文件夾下cd /opt/installs/hive/confcp hive-env.sh.template hive-env.sh修改hive-env.sh 中的內(nèi)容vim hive-env.shexport HIVE_CONF_DIR/opt/installs/hive/confexport JAVA_HOME/opt/installs/jdkexport HADOOP_HOME/opt/installs/hadoopexport HIVE_AUX_JARS_PATH/opt/installs/hive/libexport HADOOP_HEAPSIZE2048配置hive的log日志位置修改hive的log的位置cp hive-log4j2.properties.template hive-log4j2.properties打開(kāi) hive-log4j2.properties (從hive-log4j2.properties.template 重命名得來(lái))將property.hive.log.dir ${sys:java.io.tmpdir}/${sys:user.name}修改為: property.hive.log.dir /opt/installs/hive/logs啟動(dòng)集群(hadoop)如果以前沒(méi)有啟動(dòng)hadoop集群可以一下start-all.sh在hdfs創(chuàng)建hive相關(guān)文件夾hdfs dfs -mkdir -p /user/hive/warehouse hdfs dfs -mkdir -p /tmp/hive/ hdfs dfs -chmod 777 /user/hive/warehouse hdfs dfs -chmod 777 /tmp/hive使用mysql存儲(chǔ)hive核心元數(shù)據(jù)使用本地模式的最大特點(diǎn)是將元數(shù)據(jù)從derby數(shù)據(jù)庫(kù)變?yōu)閙ysql數(shù)據(jù)庫(kù)并且支持多窗口同時(shí)使用。第一步檢查你的mysql是否正常systemctl status mysqld第二步進(jìn)入到conf 文件夾下創(chuàng)建這個(gè)文件hive-site.xml,t添加如下內(nèi)容?xml version1.0 encodingUTF-8 standaloneno? ?xml-stylesheet typetext/xsl hrefconfiguration.xsl?!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the License); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --configuration !--配置MySql的連接字符串-- property namejavax.jdo.option.ConnectionURL/name valuejdbc:mysql://localhost:3306/hive?createDatabaseIfNotExisttrue/value descriptionJDBC connect string for a JDBC metastore/description /property !--配置MySql的連接驅(qū)動(dòng)-- property namejavax.jdo.option.ConnectionDriverName/name valuecom.mysql.cj.jdbc.Driver/value descriptionDriver class name for a JDBC metastore/description /property !--配置登錄MySql的用戶-- property namejavax.jdo.option.ConnectionUserName/name valueroot/value descriptionusername to use against metastore database/description /property !--配置登錄MySql的密碼-- property namejavax.jdo.option.ConnectionPassword/name value123456/value descriptionpassword to use against metastore database/description /property !-- 以下兩個(gè)不需要修改只需要了解即可 -- !-- 該參數(shù)主要指定Hive的數(shù)據(jù)存儲(chǔ)目錄 -- property namehive.metastore.warehouse.dir/name value/user/hive/warehouse/value /property !-- 該參數(shù)主要指定Hive的臨時(shí)文件存儲(chǔ)目錄 -- property namehive.exec.scratchdir/name value/tmp/hive/value /property /configuration第三步將mysql的驅(qū)動(dòng)包上傳至 hive 的lib 文件夾下第四步初始化元數(shù)據(jù)本質(zhì)就是在mysql中創(chuàng)建數(shù)據(jù)庫(kù)并且添加元數(shù)據(jù)schematool --initSchema -dbType mysql我們打開(kāi)數(shù)據(jù)庫(kù)可以看到現(xiàn)在自動(dòng)創(chuàng)建和初始化了hive數(shù)據(jù)庫(kù)我們?cè)偈褂妹铗?yàn)證一下是hive否安裝成功在 Hive 4.0 中hive命令默認(rèn)直接啟動(dòng)Beeline這是官方為了強(qiáng)制棄用舊版 CLI所做的設(shè)計(jì)變更。你設(shè)置的環(huán)境變量USE_DEPRECATED_CLItrue在 Hive 4.0 中已經(jīng)失效無(wú)法再通過(guò)這種方式切換回舊版界面。如果你確實(shí)需要使用舊版 CLI 的交互體驗(yàn)直接使用 Beeline 的“嵌入式模式”推薦這是官方推薦的替代方案它不需要啟動(dòng) HiveServer2直接在本地啟動(dòng)一個(gè) Hive 進(jìn)程體驗(yàn)與舊版 CLI 幾乎一致命令如下beeline -u jdbc:hive2://進(jìn)入后可以編寫(xiě)sql 0: jdbc:hive2:// show databases; OK default -- 進(jìn)入后可以執(zhí)行下面命令進(jìn)行操作 0: jdbc:hive2://show databases; -- 查看數(shù)據(jù)庫(kù) 0: jdbc:hive2://show tables; -- 查看表 -- 創(chuàng)建表 0: jdbc:hive2:// create table dog(id int,name string); 0: jdbc:hive2:// select * from dog; 0: jdbc:hive2:// insert into dog values(1,wangcai); 0: jdbc:hive2:// desc dog; -- 查看表結(jié)構(gòu) 0: jdbc:hive2:// !quit -- 退出實(shí)際上當(dāng)我們執(zhí)行insert語(yǔ)句的時(shí)候hive會(huì)將它轉(zhuǎn)換成MapReduce任務(wù)不過(guò)我們不用關(guān)心后臺(tái)的處理稍微了解下這個(gè)過(guò)程就行。然后在hdfs的hive目錄下已經(jīng)能看到剛才創(chuàng)建的dog表注意mysql僅僅只是存儲(chǔ)hive的元數(shù)據(jù)我們創(chuàng)建的表還是存在剛才配置的hdfs中四、Hive的遠(yuǎn)程模式比如你想通過(guò)Dbeaver操作hive這個(gè)時(shí)候必須開(kāi)啟遠(yuǎn)程模式。1、創(chuàng)建臨時(shí)目錄cd /opt/installs/hive/ mkdir iotmp chmod 777 iotmp2、前期準(zhǔn)備工作hive-site.xml 追加!--Hive工作的本地臨時(shí)存儲(chǔ)空間-- property namehive.exec.local.scratchdir/name value/opt/installs/hive/iotmp/root/value /property !--如果啟用了日志功能則存儲(chǔ)操作日志的頂級(jí)目錄-- property namehive.server2.logging.operation.log.location/name value/opt/installs/hive/iotmp/root/operation_logs/value /property !--Hive運(yùn)行時(shí)結(jié)構(gòu)化日志文件的位置-- property namehive.querylog.location/name value/opt/installs/hive/iotmp/root/value /property !--用于在遠(yuǎn)程文件系統(tǒng)中添加資源的臨時(shí)本地目錄-- property namehive.downloaded.resources.dir/name value/opt/installs/hive/iotmp/${hive.session.id}_resources/value /propertyhive.downloaded.resources.dir:在 hdfs 上下載的一些資源會(huì)被存放在這個(gè)目錄下hive 一定要小寫(xiě)否則報(bào)cause: java.net.URISyntaxException: Illegal character in path at index 26: /opt/installs/hive/iotmp/${Hive.session.id}_resources/json-serde-1.3.8-jar-with-dependencies.jar修改 core-site.xml【hadoop】的property namehadoop.proxyuser.root.hosts/name value*/value /property property namehadoop.proxyuser.root.groups/name value*/value /property property namehadoop.http.staticuser.user/name valueroot/value /property !-- 不開(kāi)啟權(quán)限檢查 -- property namedfs.permissions.enabled/name valuefalse/value /property重啟hdfsstop-dfs.sh start-dfs.sh3、開(kāi)始配置遠(yuǎn)程服務(wù)兩個(gè)配置 hiveserver2服務(wù)修改hive-site.xmlproperty namehive.server2.thrift.bind.host/name valuebigdata001/value descriptionBind host on which to run the HiveServer2 Thrift service./description /property property namehive.server2.thrift.port/name value10000/value descriptionPort number of HiveServer2 Thrift interface when hive.server2.transport.mode is binary./description /property可以啟動(dòng)1. 該服務(wù)端口號(hào)默認(rèn)是100002. 可以單獨(dú)啟動(dòng)此服務(wù)進(jìn)程供遠(yuǎn)程客戶端連接此服務(wù)內(nèi)置metastore服務(wù)。3. 啟動(dòng)方式方法1直接調(diào)用hiveserver2。會(huì)進(jìn)入監(jiān)聽(tīng)狀態(tài)不退出。方法2hive --service hiveserver2 # 進(jìn)入后臺(tái)啟動(dòng)方法3nohup hive --service hiveserver2 /dev/null 21 #信息送入黑洞。演示第一種啟動(dòng)方式hiveserver2出現(xiàn)二個(gè) Hive Session ID 就可以連接了。我們開(kāi)啟一個(gè)新窗口使用beeline進(jìn)行測(cè)試hive4.0 以后hive這個(gè)命令不讓使用了而是需要使用beeline這個(gè)命令連接方式方式1step1. beeline 回車step2. !connect jdbc:hive2://bigdata001:10000 回車step3. 輸入用戶名 回車 rootstep4. 輸入密碼 回車 此處沒(méi)有密碼方法2(直連)beeline -u jdbc:hive2://bigdata001:10000 -n 用戶名解析:hive2是Hive的協(xié)議名稱ip: Hiveserver2服務(wù)所在的主機(jī)IP。10000是Hiveserver2的端口號(hào)退出Ctrl C 可以退出客戶端而且當(dāng)我們成功開(kāi)啟hiveserver2的時(shí)候使用jps查詢會(huì)多出一個(gè)名叫RunJar的進(jìn)程然后如果能找到這個(gè)進(jìn)程里面的hiveserver2信息說(shuō)明就成功了。如果想結(jié)束hiveserver2直接kill -9 進(jìn)程號(hào)配置 metastore 服務(wù)metastore服務(wù)意義為別人連接mysql元數(shù)據(jù)提供服務(wù)的。以上這個(gè)圖是 hive3.x 的示意圖現(xiàn)在 hive4.0 之后已經(jīng)沒(méi)有 hive 的客戶端形式了。警告假如 hive 直接進(jìn)入的操作了數(shù)據(jù)庫(kù)其實(shí)底層已經(jīng)幫助創(chuàng)建了一個(gè)metastore服務(wù)器可能叫ms01通過(guò)hiveserver2 運(yùn)行的命令默認(rèn)底層幫你創(chuàng)建了一個(gè)metastore服務(wù)器可能叫ms02假如有很多人連接我的mysql就會(huì)有很多個(gè)metastore非常的占用資源。解決方案就是配置一個(gè)專門(mén)的metastore,只有它可以代理mysql服務(wù)別人必須經(jīng)過(guò)它跟mysql進(jìn)行交互。這樣解決內(nèi)存。修改hive-site.xml修改hive-site.xml的配置 注意想要連接metastore服務(wù)的客戶端必須配置如下屬性和屬性值 property namehive.metastore.uris/name valuethrift://bigdata001:9083/value /property 解析thrift:是協(xié)議名稱 ip為metastore服務(wù)所在的主機(jī)ip地址 9083是默認(rèn)端口號(hào)啟動(dòng)方式方法1hive --service metastore 方法2nohup hive --service metastore 21 /dev/null #信息送入黑洞。解析21 /dev/null 意思就是把錯(cuò)誤輸出2重定向到標(biāo)準(zhǔn)輸出1也就是屏幕標(biāo)準(zhǔn)輸出進(jìn)了“黑洞”也就是標(biāo)準(zhǔn)輸出進(jìn)了黑洞錯(cuò)誤輸出打印到屏幕。Linux系統(tǒng)預(yù)留可三個(gè)文件描述符0、1和2他們的意義如下所示0——標(biāo)準(zhǔn)輸入stdin-- System.in1——標(biāo)準(zhǔn)輸出stdout--System.out2——標(biāo)準(zhǔn)錯(cuò)誤stderr --System.err警告只要配置了metastore以后必須先啟動(dòng)metastore否則報(bào)錯(cuò)我們先啟動(dòng)metastore然后啟動(dòng)hiveserver2然后我們使用jps查看到兩個(gè)RunJar進(jìn)程分別對(duì)應(yīng)hiveserver2 和 metastore后面如果想單獨(dú)關(guān)閉直接殺對(duì)應(yīng)進(jìn)程就行了。測(cè)試沒(méi)有啟動(dòng)metastore 服務(wù)器之前hive進(jìn)入報(bào)錯(cuò)hive show databases;FAILED: HiveException java.lang.RuntimeException: Unable to instantiate org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStoreClient啟動(dòng)之后直接測(cè)試發(fā)現(xiàn)可以使用。hive show databases;OKdefaultTime taken: 1.211 seconds, Fetched: 1 row(s)4、使用客戶端工具連接Hive我們先看Hadoop啟動(dòng)了沒(méi)因?yàn)镠ive是需要在Hadoop上運(yùn)行的。進(jìn)入虛擬機(jī)可以看到Hadoop沒(méi)有運(yùn)行我們先啟動(dòng)Hadoop再次查看hadoop是否正確運(yùn)行start-all.sh然后檢查你的 metastore和hiveserver2是否啟動(dòng)ps -ef|grep metastoreps -ef|grep hiveserver2也可以使用jps查看到兩個(gè)RunJar進(jìn)程分別對(duì)應(yīng)hiveserver2 和 metastore如果出現(xiàn)下面的情況就是已經(jīng)運(yùn)行了假如沒(méi)有啟動(dòng) nohup hive --service metastore 21 /dev/null nohup hive --service hiveserver2 21 /dev/null 由于沒(méi)辦法看到2個(gè)session ID,等一下。然后過(guò)一會(huì)再次驗(yàn)證可以看到已經(jīng)成功了接下來(lái)使用dbeaver工具連接可以看連接成功然后就可以寫(xiě)SQL了當(dāng)然Hive相較于Mysql等數(shù)據(jù)庫(kù)還有其他的強(qiáng)大特性后面會(huì)說(shuō)到五、啟動(dòng)hiveserver2、metastore的腳本經(jīng)常啟動(dòng)metastore 以及hiveserver2這兩個(gè)服務(wù)命令有點(diǎn)長(zhǎng)為了長(zhǎng)期使用可以編寫(xiě)一個(gè)命令#!/bin/bash # hive 服務(wù)控制腳本可以控制 Hive 的 metastore 和 hiveserver2 服務(wù)的啟停 # 使用方式: hive-server-manager.sh [start|stop|status] [metastore|hiveserver2] # - start : 一鍵開(kāi)啟metastore和hiveserver2服務(wù)也可以指定服務(wù)開(kāi)啟 # - stop : 一鍵停止metastore和hiveserver2服務(wù)也可以指定服務(wù)停止 # - status : 一鍵查看metastore和hiveserver2服務(wù)也可以指定服務(wù)查看 help_info() { echo --------------------------------------------------------------------------------- echo | 本腳本可以一鍵控制 Hive 的 metastore 和 hiveserver2 服務(wù) | echo | 使用方式: hive-server-manager.sh [start|stop|status] [metastore|hiveserver2] | echo --------------------------------------------------------------------------------- echo | 第一個(gè)參數(shù)用來(lái)指定操作命令可以選擇 開(kāi)始(start)、停止(stop)、狀態(tài)查看(status) | echo | 第二個(gè)參數(shù)用來(lái)指定操作的服務(wù)可以選擇 metastore、hiveserver2默認(rèn)為全部 | echo --------------------------------------------------------------------------------- echo | - start : 一鍵開(kāi)啟metastore和hiveserver2服務(wù)也可以指定服務(wù)開(kāi)啟 | echo | - stop : 一鍵停止metastore和hiveserver2服務(wù)也可以指定服務(wù)停止 | echo | - status : 一鍵查看metastore和hiveserver2服務(wù)也可以指定服務(wù)查看 | echo --------------------------------------------------------------------------------- exit -1 } # 獲取操作命令 op$1 # 獲取操作的服務(wù) server$2 # 檢查參數(shù)是否正確 if [ ! $op ]; then help_info elif [ $op ! start -a $op ! stop -a $op ! status ]; then help_info fi # 檢查進(jìn)程狀態(tài) metastore_pidps aux | grep org.apache.hadoop.hive.metastore.HiveMetaStore | grep -v grep | awk {print $2} hiveserver2_pidps aux | grep proc_hiveserver2 | grep -v grep | awk {print $2} # 檢查日志文件夾的存在情況如果不存在則創(chuàng)建這個(gè)文件夾 log_dir/var/log/my_hive_log if [ ! -e $log_dir ]; then mkdir -p $log_dir fi # 開(kāi)啟服務(wù) start_metastore() { # 檢查是否開(kāi)啟如果未開(kāi)啟則開(kāi)啟 metastore 服務(wù) if [ $metastore_pid ]; then echo metastore 服務(wù)已經(jīng)開(kāi)啟進(jìn)程號(hào): $metastore_pid已跳過(guò) else nohup hive --service metastore $log_dir/metastore.log 21 echo metastore 服務(wù)已經(jīng)開(kāi)啟日志輸出在 $log_dir/metastore.log fi } start_hiveserver2() { # 檢查是否開(kāi)啟如果未開(kāi)啟則開(kāi)啟 hiveserver2 服務(wù) if [ $hiveserver2_pid ]; then echo hiveserver2 服務(wù)已經(jīng)開(kāi)啟進(jìn)程號(hào): $hiveserver2_pid已跳過(guò) else nohup hive --service hiveserver2 $log_dir/hiveserver2.log 21 echo hiveserver2 服務(wù)已經(jīng)開(kāi)啟日志輸出在 $log_dir/hiveserver2.log fi } # 停止服務(wù) stop_metastore() { if [ $metastore_pid ]; then kill -9 $metastore_pid fi echo metastore 服務(wù)已停止 } stop_hiveserver2() { if [ $hiveserver2_pid ]; then kill -9 $hiveserver2_pid fi echo hiveserver2 服務(wù)已停止 } # 查詢服務(wù) status_metastore() { if [ $metastore_pid ]; then echo metastore 服務(wù)已開(kāi)啟進(jìn)程號(hào): $metastore_pid else echo metastore 服務(wù)未開(kāi)啟 fi } status_hiveserver2() { if [ $hiveserver2_pid ]; then echo hiveserver2 服務(wù)已開(kāi)啟進(jìn)程號(hào): $hiveserver2_pid else echo hiveserver2 服務(wù)未開(kāi)啟 fi } # 控制操作 if [ ! $server ]; then ${op}_metastore ${op}_hiveserver2 elif [ $server metastore ]; then ${op}_metastore elif [ $server hiveserver2 ]; then ${op}_hiveserver2 else echo 服務(wù)選擇錯(cuò)誤 help_info fi上傳到環(huán)境變量的目錄下選一個(gè)目錄下沒(méi)有腳本的 /usr/local/bincd /usr/local/binchmod 777 hive-server-manager.sh停止開(kāi)啟