前言
本文檔基於hive 3.1.2編寫
hive的基礎知識
基本架構
- 整個hive由hiveserver2和hive 客戶端組成
- hive客戶端有三種,beeline 、使用jdbc鏈接hiveserver、或使用hive CLI(這個已經過時,hive官方已經不推薦,推薦beeline)
- hive server本身由hive server2和metastore組成
- metastore是hive的元數據管理組件
- hcatalog 架設在metastore上,暴露一組api,使得其它框架,比如Pig,FLink能夠使用hive的元數據管理功能,從而以表視角去管理數據
- webchat 是在hcatalog基礎上暴露restful接口
- hive 的實際數據存儲在hadoop的hdfs中
- hue提供一個圖形化的方式,方便用戶做基於sql的開發,當然還有其他附加功能
metastore
hive的數據本質上存儲在hdfs中的。如何以表的視角看到數據,這就是metastore的功勞,它存儲了表的schema信息、序列化信息、存儲位置信息等
metastore本身由兩部分組成
- metastore server
- metatore db
這個經典的架構,像任何一個單體java應用一樣,server是應用本身,db來存儲數據。但具體metastore整體的部署模式上,有三種
內嵌服務和數據庫
metastore server和metastore DB同hive server部署在一起,以內嵌的方式部署
其中metastore DB是啟動了一個內嵌的Derby數據庫
內嵌服務
metastore server還是跟hive一起部署。
但metastore DB使用獨立的Mysql來承接
服務和數據庫單獨部署
除了數據庫獨立部署之外,metastore service本身也獨立部署
hcatalog
hcatalog 架設在metastore上,暴露一組api,使得其它框架,比如Pig,FLink能夠使用hive的元數據管理功能,從而以表視角去管理數據
demo
hadoop distcp file:///file.dat hdfs://data/rawevents/20100819/data
hcat "alter table rawevents add partition (ds='20100819') location 'hdfs://data/rawevents/20100819/data'"
上述命令先將文件拷貝到hdfs,然后通過hcatalog,將這份數據做為表rawevents的一個新分區
客戶端
客戶端的本地模式
上述介紹的metastore的嵌入或remote部署,都是以hiveserver的視角來說的。hiveserver本身是獨立的部署。但在hive客戶端來說,可以通過remote模式,連接到已經部署好的remote server 。 還可以啟動客戶端的時候,順帶起一個本地的hive server和其對應的metastore。這一點一定要搞清楚
beeline
做為hive推薦的新一代客戶端。他使用Thrift 遠程調用。 beeline的本地模式
$HIVE_HOME/bin/hiveserver2 #先獨立部署hiveserver
$HIVE_HOME/bin/beeline -u jdbc:hive2://$HS2_HOST:$HS2_PORT # 然后以host加port的形式跟hiveserver鏈接
beeline的本地模式
$HIVE_HOME/bin/beeline -u jdbc:hive2:// #就是不加host和port,該操作會在同一個進程啟動hiveserver 和metastore,以及beeline 。 不建議這樣使用,只做單元測試
本地和遠程的區別,就是是否指定遠程的Host和port。沒有的話,就是本地模式
beeline的自動模式
每次通過beeline連接遠端的hiveserver時,需要指定很長一段的jdbc url,很麻煩,如果我們想敲擊beeline命令,直接就連接遠端的hiveserver2,則可以在hive的配置文件目錄添加beeline-site.xml
配置文件,文件內容大致如下
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
<name>beeline.hs2.jdbc.url.tcpUrl</name>
<value>jdbc:hive2://localhost:10000/default;user=hive;password=hive</value>
</property>
<property>
<name>beeline.hs2.jdbc.url.httpUrl</name>
<value>jdbc:hive2://localhost:10000/default;user=hive;password=hive;transportMode=http;httpPath=cliservice</value>
</property>
<property>
<name>beeline.hs2.jdbc.url.default</name>
<value>tcpUrl</value>
</property>
</configuration>
其中配置了beeline連接的兩種方式,和具體的jdbc連接字符串。一種使用tcp,一種使用http。默認是用tcp
jdbc
jdbc鏈接hive也有兩種模式
- For a remote server, the URL format is
jdbc:hive2://<host>:<port>/<db>;initFile=<file>
(default port for HiveServer2 is 10000). - For an embedded server, the URL format is
jdbc:hive2:///;initFile=<file>
(no host or port).
部署
一個hive的完整部署涉及五個組件,主要需要部署前三個
- metastore的部署
- hiveserver2的部署
- client的部署
- hcatalog server 的部署(非必須)
- webhcat server的部署(非必須)
其中1,2兩部可以何在一起,即hiveserver2內嵌metastore, 只是將metastore的DB,外掛到Mysql。
具體到物理服務器上,上述三個組件,可以部署到三個不同機器上,當然hive client一般會在多個機器上。
三個組件對應的配置都可以是hive-site.xml,但配置內容不一定相同。比如hiveserver2需要配置,meta DB的連接信息,但client則不需要這些信息。
除了hive-site.xml之外,hive還支持在其它幾個地方進行配置
- 在啟動命令時 通過
--hiveconf
參數,指定定制化的配置,如bin/hive --hiveconf hive.exec.scratchdir=/tmp/mydir
- 在 hivemetastore-site.xml文件中指定,metastore相關的配置
- 在hiveserver2-site.xml中指定,hiveserver2獨享的配置。
以上這些路徑和配置文件中擁有相同配置時,hive識別優先級如下,從左至右,依次升高
hive-site.xml -> hivemetastore-site.xml -> hiveserver2-site.xml -> '-hiveconf'
所以,最佳的配置策略是:
- 在hive-site.xml中配置,hiveserver2和hive client都共用的配置,這樣方便該配置直接分發到多個機器
- 將metastore相關的配置放到 hivemetastore-site.xml 中。meta數據庫相關配置,只在metaserver部署的機器上存在,不會四處分發數據庫密碼
- 將hiveserver2獨有的配置放到hiveserver2-site.xml 中
部署hiveserver2
以下配置,采用內嵌metastore server , remote metastore DB的方式進行hiveserver 部署
在需要部署hive的機器上。創建一個hive賬號,adduser hive
並將其加入hadoop組,所有后續配置和啟動都以hive用戶進行
下載hive安裝包。將其中conf下的各template文件去掉,並按需進行配置。
hive-default.xml.template需要改成hive-site.xml
hive-default.xml.template包含了所有的hive各組件相關的默認配置。
在hdfs中創建hive數據存放路徑
在hdfs中創建以下文件,並給hive所屬組賦寫權限
$ $HADOOP_HOME/bin/hadoop fs -mkdir /tmp
$ $HADOOP_HOME/bin/hadoop fs -mkdir /user/hive/warehouse
$ $HADOOP_HOME/bin/hadoop fs -chmod g+w /tmp
$ $HADOOP_HOME/bin/hadoop fs -chmod g+w /user/hive/warehouse
/user/hive/warehouse是hive中的表數據實際存儲的地方。它是默認路徑,你當然可以在hive-site.xml中通過設置hive.metastore.warehouse.dir
屬性來指定新的路徑
配置hive的環境變量
export HIVE_HOME=/opt/apache-hive-3.1.2-bin/
export PATH=$HIVE_HOME/bin:$PATH
配置日志輸出路徑
hive的默認輸出路徑為/tmp/<user.name>/hive.log
,如果我們以hive用戶啟動,那么該路徑為/tmp/hive/hive.log
/tmp路徑是linux用來存放各應用程序運行時的中間狀態數據,一般會在操作系統重啟后,自動進行清理。
當然你可以修改hive的log4j文件,來指定hive.log.dir=<other_location>
路徑
也可以在啟動hiveserver2時,動態的去通過hiveconf參數指定路徑bin/hiveserver2 --hiveconf hive.root.logger=INFO,DRFA
hive的臨時文件配置
hive運行時也會在host本地,和hdfs中存放臨時文件,稱之為scratch文件。存放的文件路徑為
hdfs :/tmp/hive-<username>
本地:/tmp/<username>
值得說明的是,hive-site.xml模板中,有大量路徑配置是 ${java.io.tmpdir}/${user.name}/
,${java.io.tmpdir}
表示該配置的默認值是取Java使用臨時目錄,一般在linux下也即/tmp
,${user.name}
表示取當前啟動hive的用戶。你如果不特殊指定,可刪除對應的配置項。而不要直接配置中也寫成${java.io.tmpdir}/${user.name}/
,xml中是識別不了這些占位符的。
配置metastore 的DB信息並初始化
在hive-site.xml對應的目錄下,創建一個hivemetastore-site.xml文件,用於配置metastore相關信息
初始化metastore db
$HIVE_HOME/bin/schematool -dbType <db type> -initSchema
其中dbType的取值可以是derby, oracle, mysql, mssql, postgres
參考資料
https://cwiki.apache.org/confluence/display/Hive/AdminManual+Metastore+Administration
啟動hiveserver2
$HIVE_HOME/bin/hiveserver2
上述命令為前台運行,最好以no hang up 加后后台方式運行
nohup $HIVE_HOME/bin/hiveserver2 > /opt/apache-hive-3.1.2-bin/logs/hive_runtime_log.log < /dev/null &
啟動后hive的web界面對應的端口是:10002
基本客戶端部署
軟件包分發
將上述hive發型包,同配置,拷貝到需要啟動beeline的機器上,即完成客戶端的配置。配置文件只需要hive-site.xml,可根據具體機器環境,去修改相應的路徑配置信息,不需要hivemetastore-site.xml 和 hiveserver2-site.xml文件
環境變量配置
export HIVE_HOME=/opt/apache-hive-3.1.2-bin/
export PATH=$HIVE_HOME/bin:$PATH
日志路徑配置
同hiveserver一樣,根據具體情況,做一些路徑配置修改
啟動
使用$HIVE_HOME/bin/beeline -u jdbc:hive2://$HS2_HOST:$HS2_PORT
即可連接hiveserver2。
由於hive在hdfs中使用的目錄,默認是/user/hive/warehouse
,所以為了避免權限相關的錯,需要在通過beeline鏈接是加-n
參數,用於指定當前客戶端使用的用戶。並且該用戶要有/user/hive/warehouse
和其下文件的相關權限,沒有的話需要單獨加。權限模型,同Linux類似。如果有權限問題,一般的錯誤類似
Error: Error while processing statement: FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. MetaException(message:Got exception: org.apache.hadoop.security.AccessControlException Permission denied: user=yarn, access=WRITE, inode="/user/hive/warehouse/test.db":hive:hadoop:drwxr-xr-x
帶用戶名鏈接為形式為:
beeline -u jdbc:hive2://master:10000 -n hive
這里以hive用戶鏈接到hiveserver
beeline整體使用文檔:https://cwiki.apache.org/confluence/display/Hive/HiveServer2+Clients
hiveserver高可用部署
服務端配置
在所有需要啟動hiverserver的機器上,配置hiveserver2-site.xml。在其中做如下配置
<!--啟動高可用配置-->
<property>
<name>hive.server2.support.dynamic.service.discovery</name>
<value>true</value>
</property>
<!--配置在zk中的地址空間-->
<property>
<name>hive.server2.zookeeper.namespace</name>
<value>hiveserver2</value>
</property>
<!--配置在zk的地址-->
<property>
<name>hive.zookeeper.quorum</name>
<value>master:2181,slave1:2181,slave2:2181</value>
</property>
同時記住,需要部署的hiverserver的機器,都要有相同metastore的配置,保證他們連得時同一個mysql,可將hivemetastore-site.xml配置拷貝至多個需要啟動hiveserver的機器
參考資料:http://lxw1234.com/archives/2016/05/675.htm
客戶端連接
用beeline的連接方式如下:
beeline -u "jdbc:hive2://master:2181,slave1:2181,slave2:2181/;serviceDiscoveryMode=zooKeeper;zooKeeperNamespace=hiveserver2" -n hive
注意其中的jdbc url一定要加引號
認證
這里選擇Kerberos作為認證選項。其需要配置的三個配置項如下:
Authentication mode:
hive.server2.authentication – Authentication mode, default NONE. Options are NONE (uses plain SASL), NOSASL, KERBEROS, LDAP, PAM and CUSTOM.
Set following for KERBEROS mode:
hive.server2.authentication.kerberos.principal – Kerberos principal for server.
hive.server2.authentication.kerberos.keytab – Keytab for server principal.
使用了kerberos認證后的beeline鏈接方式
beeline -u "jdbc:hive2://master:2181,slave1:2181,slave2:2181/;serviceDiscoveryMode=zooKeeper;zooKeeperNamespace=hiveserver2"
上述命令使用前,一定保證已經通過kinit實現了當前機器的kerberos認證。否者beeline命令會報錯,因為沒有取到kerberos認證票據。
該命令會自動去讀取,當前登錄的kerberos用戶信息,在執行的命令的時候帶上
INFO : Executing with tokens: [Kind: HDFS_DELEGATION_TOKEN, Service: ha-hdfs:haixue-hadoop, Ident: (token for test: HDFS_DELEGATION_TOKEN owner=test, renewer=yarn, realUser=hive/master@HAIXUE.COM, issueDate=1594627135369, maxDate=1595231935369, sequenceNumber=66, masterKeyId=27)]
比如當前kerberos的tgt是test用戶,那么該hivesql 對應的owner就是test。實際組件之間的通信認證是使用的hive/master@HAIXUE.COM 用戶。但授權粒度會控制到test上。
這個特性是hive配置的的hive.server2.enable.doAs
屬性來控制的,該屬性為true時,表示運行以提交用戶作為最終sql執行的用戶
另外,最好我們在hiveserver的配置文件中,將hive.server2.allow.user.substitution
關閉為false. 因為該選項,會允許用戶以-n
參數指定一個用戶。這樣會導致一個用戶以自己的kerberos憑證,操作別人的庫表。但禁用后,hue就無法將其登錄用戶做為提交job的人。
基於hive-site.xml的客戶端連接
上述記錄的連接hiveserver2的方式,是通過jdbc來實現的。但有些依賴hive的程序,則只能通過hive-site.xml 這種方式連接hiveserver。典型的就是hue
hue會去${HIVE_CONF_DIR}
環境變量或者/etc/hive/conf
路徑下找hive-site.xml
和beeline-hs2-connection.xml
兩個配置文件,讀取其中的信息,實現對hiveserver的連接。
如果集群配置了kerberos,那么需要在hive-site.xml中配置,跟hiveserver2-site.xml一樣的kerberos認證配置,例如
<property>
<name>hive.server2.authentication</name>
<value>KERBEROS</value>
</property>
<property>
<name>hive.server2.authentication.kerberos.principal</name>
<value>hive/master@HAIXUE.COM</value>
</property>
<property>
<name>hive.server2.authentication.kerberos.keytab</name>
<value>/opt/keytab_store/hive.service.keytab</value>
</property>
像hue 是使用beeline連接,還可以配置beeline-hs2-connection.xml,在其中指定一些些連接hiveserver2的代理用戶信息,不過目前發現不配置依然可以使用。如下:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
<name>beeline.hs2.connection.user</name>
<value>hive</value>
</property>
<property>
<name>beeline.hs2.connection.password</name>
<value>hive</value>
</property>
</configuration>
一些錯誤
錯誤1 guava
初始化metastore schema是報錯
Exception in thread "main" java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkArgument(ZLjava/lang/String;Ljava/lang/Object;)V
原因是hadoop路徑share/hadoop/common/lib/
下的guava包同hive的lib下的guava包版本不一致。
解決辦法,刪除hive的guava包,將hadoop的對應guava包拷貝過來
錯誤2,mysql驅動
[hive@master bin]$ ./schematool -dbType mysql -initSchema
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/opt/apache-hive-3.1.2-bin/lib/log4j-slf4j-impl-2.10.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/opt/hadoop-3.2.1/share/hadoop/common/lib/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]
Metastore connection URL: jdbc:mysql://xxx.xx.xx.xx:3306/hive?createDatabaseIfNotExist=true
Metastore Connection Driver : com.mysql.jdbc.Driver
Metastore connection User: root
org.apache.hadoop.hive.metastore.HiveMetaException: Failed to load driver
Underlying cause: java.lang.ClassNotFoundException : com.mysql.jdbc.Driver
Use --verbose for detailed stacktrace.
*** schemaTool failed ***
問題原因,hive缺乏mysql的驅動
解決辦法,下載一個mysql的驅動,安裝到hive的lib下
錯誤3
當使用beeline客戶端:beeline -u jdbc:hive2://master:10000
鏈接,hiveserver2時,報以下錯誤
Error: Could not open client transport with JDBC Uri: jdbc:hive2://master:10000: Failed to open new session: java.lang.RuntimeException: org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.security.authorize.AuthorizationException): User: hive is not allowed to impersonate anonymous (state=08S01,code=0)
Beeline version 3.1.2 by Apache Hive
我們是以hive用戶,啟動的hiveserver2。所以,所有客戶端,無論以書面用戶鏈接到hiveserver2,最終hiveserver2去訪問hadoop集群時,都是以hive的用戶去訪問的。
但如果你沒在hadoop中,做相關的配置,那hadoop默認是不允許hive這個用戶作為其他用戶的代理用戶使用集群的,所以需要在hadoop的core-site.xml中,做以下配置
<!--基礎權限控制,允許來之master,slave1,slave2上的所有組的用戶,以hive用戶身份使用Hadoop集群,這里hive也是一個用戶-->
<property>
<name>hadoop.proxyuser.hive.hosts</name>
<value>master,slave1,slave2</value>
</property>
<property>
<name>hadoop.proxyuser.hive.groups</name>
<value>*</value>
</property>
<!--以下配置跟hadoop.proxyuser.hive.groups二選一,不用兩個都配置。用於指定hadoop.proxyuser.hive.hosts機器上,具體哪些用戶可以代理到hive上-->
<property>
<name>hadoop.proxyuser.super.users</name>
<value>user1,user2</value>
</property>
參考資料
https://cwiki.apache.org/confluence/display/Hive/GettingStarted
https://cwiki.apache.org/confluence/display/Hive/HCatalog+InstallHCat
https://stackoverflow.com/questions/22533814/what-is-use-of-hcatalog-in-hadoop
https://cwiki.apache.org/confluence/display/Hive/HiveServer2+Clients
歡迎關注我的個人公眾號"西北偏北UP",記錄代碼人生,行業思考,科技評論