
一、Hive簡介Hive 是一個框架可以通過編寫sql的方式自動的編譯為MR任務的一個工具。在這個世界上會寫SQL的人遠遠大于會寫java代碼的人所以假如可以將MR通過sql實現(xiàn)這個將是一個巨大的市場FaceBook就這么干。臉書在大數(shù)據(jù)中發(fā)展趨勢所有的技術全部都變?yōu)镾QL。1、Hive是一個數(shù)據(jù)倉庫工具2、可以將數(shù)據(jù)加載到表中編寫sql進行分析3、底層依賴Hadoop所以每一次都需要啟動hadoop(hdfs以及yarn)4、Hive的底層計算框架可以使用MR、也可以使用Spark、TEZ5、Hive不是數(shù)據(jù)庫而是一個將MR包了一層殼兒。類似于一個中介。Hive官網(wǎng)地址Apache HiveGitHub地址 GitHub - apache/hive: Apache Hive · GitHub文檔查看地址https://cwiki.apache.org/confluence/display/Hive/GettingStarted目前我們可以采用穩(wěn)定版realease4.0.1Hive天然的就是當做數(shù)據(jù)倉庫使用的。什么是數(shù)據(jù)倉庫?數(shù)據(jù)倉庫不是某一款存儲軟件是面向分析、經(jīng)過統(tǒng)一清洗整合的數(shù)據(jù)集 整套架構存儲只是它的底座Oracle 是關系型數(shù)據(jù)庫可作為小型數(shù)倉的承載載體Hive 是大數(shù)據(jù)生態(tài)下構建分布式數(shù)倉的 SQL 查詢引擎為了高效取用數(shù)據(jù)數(shù)倉必須做分層設計這套分層、表結構、維度事實表的設計工作就是數(shù)據(jù)建模和實體倉庫分區(qū)管理邏輯相二、Hive體系結構圖注意- 包含*的全表查詢比如select * from table 不會生成MapRedcue任務- 包含*的limit查詢比如select * from table limit 3 不會生成MapRedcue任務三、本地模式安裝下載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進入這個文件夾下cd /opt/installs/hive/confcp hive-env.sh.template hive-env.sh修改hive-env.sh 中的內容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打開 hive-log4j2.properties (從hive-log4j2.properties.template 重命名得來)將property.hive.log.dir ${sys:java.io.tmpdir}/${sys:user.name}修改為: property.hive.log.dir /opt/installs/hive/logs啟動集群(hadoop)如果以前沒有啟動hadoop集群可以一下start-all.sh在hdfs創(chuàng)建hive相關文件夾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存儲hive核心元數(shù)據(jù)使用本地模式的最大特點是將元數(shù)據(jù)從derby數(shù)據(jù)庫變?yōu)閙ysql數(shù)據(jù)庫并且支持多窗口同時使用。第一步檢查你的mysql是否正常systemctl status mysqld第二步進入到conf 文件夾下創(chuàng)建這個文件hive-site.xml,t添加如下內容?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的連接驅動-- 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 !-- 以下兩個不需要修改只需要了解即可 -- !-- 該參數(shù)主要指定Hive的數(shù)據(jù)存儲目錄 -- property namehive.metastore.warehouse.dir/name value/user/hive/warehouse/value /property !-- 該參數(shù)主要指定Hive的臨時文件存儲目錄 -- property namehive.exec.scratchdir/name value/tmp/hive/value /property /configuration第三步將mysql的驅動包上傳至 hive 的lib 文件夾下第四步初始化元數(shù)據(jù)本質就是在mysql中創(chuàng)建數(shù)據(jù)庫并且添加元數(shù)據(jù)schematool --initSchema -dbType mysql我們打開數(shù)據(jù)庫可以看到現(xiàn)在自動創(chuàng)建和初始化了hive數(shù)據(jù)庫我們再使用命令驗證一下是hive否安裝成功在 Hive 4.0 中hive命令默認直接啟動Beeline這是官方為了強制棄用舊版 CLI所做的設計變更。你設置的環(huán)境變量USE_DEPRECATED_CLItrue在 Hive 4.0 中已經(jīng)失效無法再通過這種方式切換回舊版界面。如果你確實需要使用舊版 CLI 的交互體驗直接使用 Beeline 的“嵌入式模式”推薦這是官方推薦的替代方案它不需要啟動 HiveServer2直接在本地啟動一個 Hive 進程體驗與舊版 CLI 幾乎一致命令如下beeline -u jdbc:hive2://進入后可以編寫sql 0: jdbc:hive2:// show databases; OK default -- 進入后可以執(zhí)行下面命令進行操作 0: jdbc:hive2://show databases; -- 查看數(shù)據(jù)庫 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; -- 查看表結構 0: jdbc:hive2:// !quit -- 退出實際上當我們執(zhí)行insert語句的時候hive會將它轉換成MapReduce任務不過我們不用關心后臺的處理稍微了解下這個過程就行。然后在hdfs的hive目錄下已經(jīng)能看到剛才創(chuàng)建的dog表注意mysql僅僅只是存儲hive的元數(shù)據(jù)我們創(chuàng)建的表還是存在剛才配置的hdfs中四、Hive的遠程模式比如你想通過Dbeaver操作hive這個時候必須開啟遠程模式。1、創(chuàng)建臨時目錄cd /opt/installs/hive/ mkdir iotmp chmod 777 iotmp2、前期準備工作hive-site.xml 追加!--Hive工作的本地臨時存儲空間-- property namehive.exec.local.scratchdir/name value/opt/installs/hive/iotmp/root/value /property !--如果啟用了日志功能則存儲操作日志的頂級目錄-- property namehive.server2.logging.operation.log.location/name value/opt/installs/hive/iotmp/root/operation_logs/value /property !--Hive運行時結構化日志文件的位置-- property namehive.querylog.location/name value/opt/installs/hive/iotmp/root/value /property !--用于在遠程文件系統(tǒng)中添加資源的臨時本地目錄-- property namehive.downloaded.resources.dir/name value/opt/installs/hive/iotmp/${hive.session.id}_resources/value /propertyhive.downloaded.resources.dir:在 hdfs 上下載的一些資源會被存放在這個目錄下hive 一定要小寫否則報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 !-- 不開啟權限檢查 -- property namedfs.permissions.enabled/name valuefalse/value /property重啟hdfsstop-dfs.sh start-dfs.sh3、開始配置遠程服務兩個配置 hiveserver2服務修改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可以啟動1. 該服務端口號默認是100002. 可以單獨啟動此服務進程供遠程客戶端連接此服務內置metastore服務。3. 啟動方式方法1直接調用hiveserver2。會進入監(jiān)聽狀態(tài)不退出。方法2hive --service hiveserver2 # 進入后臺啟動方法3nohup hive --service hiveserver2 /dev/null 21 #信息送入黑洞。演示第一種啟動方式hiveserver2出現(xiàn)二個 Hive Session ID 就可以連接了。我們開啟一個新窗口使用beeline進行測試hive4.0 以后hive這個命令不讓使用了而是需要使用beeline這個命令連接方式方式1step1. beeline 回車step2. !connect jdbc:hive2://bigdata001:10000 回車step3. 輸入用戶名 回車 rootstep4. 輸入密碼 回車 此處沒有密碼方法2(直連)beeline -u jdbc:hive2://bigdata001:10000 -n 用戶名解析:hive2是Hive的協(xié)議名稱ip: Hiveserver2服務所在的主機IP。10000是Hiveserver2的端口號退出Ctrl C 可以退出客戶端而且當我們成功開啟hiveserver2的時候使用jps查詢會多出一個名叫RunJar的進程然后如果能找到這個進程里面的hiveserver2信息說明就成功了。如果想結束hiveserver2直接kill -9 進程號配置 metastore 服務metastore服務意義為別人連接mysql元數(shù)據(jù)提供服務的。以上這個圖是 hive3.x 的示意圖現(xiàn)在 hive4.0 之后已經(jīng)沒有 hive 的客戶端形式了。警告假如 hive 直接進入的操作了數(shù)據(jù)庫其實底層已經(jīng)幫助創(chuàng)建了一個metastore服務器可能叫ms01通過hiveserver2 運行的命令默認底層幫你創(chuàng)建了一個metastore服務器可能叫ms02假如有很多人連接我的mysql就會有很多個metastore非常的占用資源。解決方案就是配置一個專門的metastore,只有它可以代理mysql服務別人必須經(jīng)過它跟mysql進行交互。這樣解決內存。修改hive-site.xml修改hive-site.xml的配置 注意想要連接metastore服務的客戶端必須配置如下屬性和屬性值 property namehive.metastore.uris/name valuethrift://bigdata001:9083/value /property 解析thrift:是協(xié)議名稱 ip為metastore服務所在的主機ip地址 9083是默認端口號啟動方式方法1hive --service metastore 方法2nohup hive --service metastore 21 /dev/null #信息送入黑洞。解析21 /dev/null 意思就是把錯誤輸出2重定向到標準輸出1也就是屏幕標準輸出進了“黑洞”也就是標準輸出進了黑洞錯誤輸出打印到屏幕。Linux系統(tǒng)預留可三個文件描述符0、1和2他們的意義如下所示0——標準輸入stdin-- System.in1——標準輸出stdout--System.out2——標準錯誤stderr --System.err警告只要配置了metastore以后必須先啟動metastore否則報錯我們先啟動metastore然后啟動hiveserver2然后我們使用jps查看到兩個RunJar進程分別對應hiveserver2 和 metastore后面如果想單獨關閉直接殺對應進程就行了。測試沒有啟動metastore 服務器之前hive進入報錯hive show databases;FAILED: HiveException java.lang.RuntimeException: Unable to instantiate org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStoreClient啟動之后直接測試發(fā)現(xiàn)可以使用。hive show databases;OKdefaultTime taken: 1.211 seconds, Fetched: 1 row(s)4、使用客戶端工具連接Hive我們先看Hadoop啟動了沒因為Hive是需要在Hadoop上運行的。進入虛擬機可以看到Hadoop沒有運行我們先啟動Hadoop再次查看hadoop是否正確運行start-all.sh然后檢查你的 metastore和hiveserver2是否啟動ps -ef|grep metastoreps -ef|grep hiveserver2也可以使用jps查看到兩個RunJar進程分別對應hiveserver2 和 metastore如果出現(xiàn)下面的情況就是已經(jīng)運行了假如沒有啟動 nohup hive --service metastore 21 /dev/null nohup hive --service hiveserver2 21 /dev/null 由于沒辦法看到2個session ID,等一下。然后過一會再次驗證可以看到已經(jīng)成功了接下來使用dbeaver工具連接可以看連接成功然后就可以寫SQL了當然Hive相較于Mysql等數(shù)據(jù)庫還有其他的強大特性后面會說到五、啟動hiveserver2、metastore的腳本經(jīng)常啟動metastore 以及hiveserver2這兩個服務命令有點長為了長期使用可以編寫一個命令#!/bin/bash # hive 服務控制腳本可以控制 Hive 的 metastore 和 hiveserver2 服務的啟停 # 使用方式: hive-server-manager.sh [start|stop|status] [metastore|hiveserver2] # - start : 一鍵開啟metastore和hiveserver2服務也可以指定服務開啟 # - stop : 一鍵停止metastore和hiveserver2服務也可以指定服務停止 # - status : 一鍵查看metastore和hiveserver2服務也可以指定服務查看 help_info() { echo --------------------------------------------------------------------------------- echo | 本腳本可以一鍵控制 Hive 的 metastore 和 hiveserver2 服務 | echo | 使用方式: hive-server-manager.sh [start|stop|status] [metastore|hiveserver2] | echo --------------------------------------------------------------------------------- echo | 第一個參數(shù)用來指定操作命令可以選擇 開始(start)、停止(stop)、狀態(tài)查看(status) | echo | 第二個參數(shù)用來指定操作的服務可以選擇 metastore、hiveserver2默認為全部 | echo --------------------------------------------------------------------------------- echo | - start : 一鍵開啟metastore和hiveserver2服務也可以指定服務開啟 | echo | - stop : 一鍵停止metastore和hiveserver2服務也可以指定服務停止 | echo | - status : 一鍵查看metastore和hiveserver2服務也可以指定服務查看 | echo --------------------------------------------------------------------------------- exit -1 } # 獲取操作命令 op$1 # 獲取操作的服務 server$2 # 檢查參數(shù)是否正確 if [ ! $op ]; then help_info elif [ $op ! start -a $op ! stop -a $op ! status ]; then help_info fi # 檢查進程狀態(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)建這個文件夾 log_dir/var/log/my_hive_log if [ ! -e $log_dir ]; then mkdir -p $log_dir fi # 開啟服務 start_metastore() { # 檢查是否開啟如果未開啟則開啟 metastore 服務 if [ $metastore_pid ]; then echo metastore 服務已經(jīng)開啟進程號: $metastore_pid已跳過 else nohup hive --service metastore $log_dir/metastore.log 21 echo metastore 服務已經(jīng)開啟日志輸出在 $log_dir/metastore.log fi } start_hiveserver2() { # 檢查是否開啟如果未開啟則開啟 hiveserver2 服務 if [ $hiveserver2_pid ]; then echo hiveserver2 服務已經(jīng)開啟進程號: $hiveserver2_pid已跳過 else nohup hive --service hiveserver2 $log_dir/hiveserver2.log 21 echo hiveserver2 服務已經(jīng)開啟日志輸出在 $log_dir/hiveserver2.log fi } # 停止服務 stop_metastore() { if [ $metastore_pid ]; then kill -9 $metastore_pid fi echo metastore 服務已停止 } stop_hiveserver2() { if [ $hiveserver2_pid ]; then kill -9 $hiveserver2_pid fi echo hiveserver2 服務已停止 } # 查詢服務 status_metastore() { if [ $metastore_pid ]; then echo metastore 服務已開啟進程號: $metastore_pid else echo metastore 服務未開啟 fi } status_hiveserver2() { if [ $hiveserver2_pid ]; then echo hiveserver2 服務已開啟進程號: $hiveserver2_pid else echo hiveserver2 服務未開啟 fi } # 控制操作 if [ ! $server ]; then ${op}_metastore ${op}_hiveserver2 elif [ $server metastore ]; then ${op}_metastore elif [ $server hiveserver2 ]; then ${op}_hiveserver2 else echo 服務選擇錯誤 help_info fi上傳到環(huán)境變量的目錄下選一個目錄下沒有腳本的 /usr/local/bincd /usr/local/binchmod 777 hive-server-manager.sh停止開啟