安裝就安裝 ,不扯其他的
hive 依賴
在 hive 安裝前必須具備如下條件
1. 一個可連接的關系型數據庫,如 Mysql,postgresql 等,用於存儲元數據
2. hadoop,並啟動 hdfs
3. HBase,非必須,但是如果不裝,會有警告,不過不影響使用
4. java,1.8 以上版本
准備工作
1. 下載安裝包
https://mirrors.tuna.tsinghua.edu.cn/apache/hive/ 清華鏡像,下載速度快
http://apache.org/dist/hive/ 官網,下載速度慢
選擇含有 bin 的 tar 包,本文安裝 hive-2.3.6
2. 上傳服務器
最好上傳到 hadoop 的 master 上,我是這么做的;無需所有節點都上傳
3. 解壓,或許你可以重命名一下,方便操作
環境變量
export HIVE_HOME=/opt/SoftWare/Hive/hive‐2.3.2
export PATH=$PATH:$HIVE_HOME/bin
按照自己的路徑修改即可
此時 hive 已安裝成功,執行 hive --version 可查看版本
配置
1. 首先修改 hive-env.sh 文件,本身不存在
cp hive-env.sh.template hive-env.sh
添加如下內容
export JAVA_HOME=/usr/lib/jvm/jre-1.8.0-openjdk.x86_64 export HADOOP_HOME=/usr/lib/hadoop-2.6.5 export HIVE_HOME=/usr/lib/hive2.3.6 export SPARK_HOME=/usr/lib/spark
2. 修改 hive-site.xml 文件,本身也不存在
cp hive-default.xml.template hive-site.xml
注意hive- default.xml.template這個要復制二份,一個是hive-default.xml,另一個是hive-site.xml,其中 hive-site.xml為用戶自定義配置,hive-default.xml為全局配置;
hive啟動時,hive-site.xml自定義配置會覆蓋 hive-default.xml全局配置的相同配置項。
非常不建議直接將hive-default.xml 直接復制為hive-site后進行修改,因為這樣的話,我們根本不記得對那些配置項進行過修改,由於hive-site的作用是覆蓋默認的配置,我們只需要將需要修改地方配置到hive-site.xml文件中即可。 【本文省略該過程】
修改如下內容 【只是基礎配置,此配置是首先把 hive 跑起來,如果應用在特殊場景,可能還需其他配置】
主要是配置數據庫的連接信息, Hive 默認使用 Derby 數據庫作為元數據庫,這里修改為 postgres
<!‐‐數據庫配置‐‐> <property> <name>javax.jdo.option.ConnectionURL</name><!‐‐數據庫連接地址‐‐> <value>jdbc:mysql://192.168.100.103:3306/ccx_hive?createDatabaseIfNotExist=true</value><!‐‐使用MySQL存儲元數據信息‐‐> <value>jdbc:postgresql://172.16.89.80:5432/db?ssl=true;databaseName=metastore_db;create=true</value><!‐‐使用postgres存儲元數據信息‐‐> <value>jdbc:postgresql://172.16.89.80:5432/ball</value><!‐‐ball是數據庫,最好事先建立,否則可能要其他配置,如create=true, 可以自己試試‐‐> <description> JDBC connect string for a JDBC metastore. To use SSL to encrypt/authenticate the connection, provide database-specific SSL flag in the connection URL. For example, jdbc:postgresql://myhost/db?ssl=true for postgres database. </description> </property> <property><!‐‐數據庫驅動‐‐> <name>javax.jdo.option.ConnectionDriverName</name> <value>com.mysql.jdbc.Driver</value><!‐‐mysql‐‐> <value>org.postgresql.Driver</value><!‐‐postgres‐‐> <description>Driver class name for a JDBC metastore</description> </property> <property><!‐‐數據庫用戶名‐‐> <name>javax.jdo.option.ConnectionUserName</name> <value>u_ccx_hive</value> </property> <property><!‐‐數據庫密碼‐‐> <name>javax.jdo.option.ConnectionPassword</name> <value>123456</value> </property> <!‐‐hive 執行引擎‐‐> <property> <name>hive.execution.engine</name> <value>mr</value><!‐‐mapreduce 作為引擎‐‐> <value>spark</value><!‐‐spark 作為引擎‐‐> <description> Expects one of [mr, tez, spark]. Chooses execution engine. Options are: mr (Map reduce, default), tez, spark. While MR remains the default engine for historical reasons, it is itself a historical engine and is deprecated in Hive 2 line. It may be removed without further warning. </description> </property> <property> <name>hive.metastore.schema.verification</name> <value>False</value> <description> Enforce metastore schema version consistency. True: Verify that version information stored in is compatible with one from Hive jars. Also disable automatic schema migration attempt. Users are required to manually migrate schema after Hive upgrade which ensures proper metastore schema migration. (Default) False: Warn if the version information stored in metastore doesn't match with one from in Hive jars. </description> </property>
每個 value 值不能有空格
上傳 數據庫驅動
上傳驅動 postgresql-9.2-1003.jdbc4.jar 到 hive 的 lib 目錄下
存儲元數據到 數據庫
數據庫需提前建立,這一步就是初始化該數據庫
bin/schematool -dbType mysql -initSchema
bin/schematool -dbType postgres -initSchema
此時可查看數據庫,新建了一堆表
啟動 hive 服務
以這種方式啟動的 hive 可以用客戶端直接訪問,試試這個工具 DBVisualizer
/bin/hive --service hiveserver2
HiveServer2 很好的解決 HiveServer 存在的安全性、並發性等問題,HiveServer 目前已經不用了
這個服務啟動程序在${HIVE_HOME}/bin/hiveserver2里面,也可以這樣啟動
hive --service hiveserver2 --hiveconf hive.server2.thrift.port=10001 #指定端口
也可以在配置文件中指定端口號
<property> <name>hive.server2.thrift.port</name> <value>10000</value> <description>Port number of HiveServer2 Thrift interface when hive.server2.transport.mode is 'binary'.</description> </property>
啟動 hive 的 shell 客戶端
shell 客戶端方便操作
[root@master bin]# hive #輸入show tables;顯示以下信息,說明Hive已經啟動 hive> show tables; OK Time taken: 1.594 seconds
Hive 檢測
Hive 裝好了,但是 Hive 和 hadoop 什么關系呢?實操解釋
在 hive 中創建數據庫
hive> create database hive1; # 創建數據庫 OK Time taken: 0.478 seconds hive> show databases; # 顯示數據庫 OK default hive1 # 創建成功 Time taken: 0.132 seconds, Fetched: 2 row(s)
問題來了,創建成功了,但是這個庫在哪呢?跟 hadoop 什么關系?跟元數據什么關系?
1. 首先我們看元數據,在 存儲元數據 的數據庫中有個表叫 DBS,很顯然,存儲數據庫名
我們看到了新建的 hive1 數據庫
2. 然后我們看 hdfs
我們也看到了新建的 hive1 數據庫
這個路徑在 hive-site 中可以找到對應配置
在 hive 的數據庫中創建數據表
hive> use hive1; # 切換到 hive1 數據庫環境 OK Time taken: 0.042 seconds hive> create table hive_01 (id int,name string); # 創建數據表 OK Time taken: 0.984 seconds hive> show tables; #查詢表 OK hive_01 # 創建成功 Time taken: 0.067 seconds, Fetched: 1 row(s)
1. 同樣我們看元數據,有一張 TBLS 表,很顯然,存儲表名
2. 然后我們看 hdfs
都找到了對應表
結論:在 hive 中創建的數據庫和表存儲在 hdfs 中,元數據存儲在 元數據庫 中;
hive-site.xml里的 hive.user.install.directory 參數,定義了HDFS的路徑,默認/user
異常記錄
1. 執行 schematool 命令時報錯如下
no dbType supplied,原因是數據庫沒配好,且檢查初始化 schematool 的命令,答案就是我上面的操作
2. 執行 schematool 命令時報錯如下
org.apache.hadoop.hive.metastore.HiveMetaException: Failed to get schema version. Underlying cause: org.postgresql.util.PSQLException : The server does not support SSL. SQL Error code: 0 Use --verbose for detailed stacktrace. *** schemaTool failed ***
SSL 的問題,hive-site 的 javax.jdo.option.ConnectionURL 參數修改為 jdbc:postgresql://myhost/db?ssl=true; true 改為 false 或者 刪掉
3. 啟動 hive shell 報錯如下
Caused by: org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.hdfs.server.namenode.SafeModeException): Cannot create directory /tmp/hive/root/a71af7ab-060a-465e-91ba-124ba4b07e36. Nam e node is in safe mode.The reported blocks 200 has reached the threshold 0.9990 of total blocks 200. The number of live datanodes 1 has reached the minimum number 0. In safe mode extension. Safe mode will be tur ned off automatically in 15 seconds.
原因:namenode 處於安全狀態,關閉安全模式即可
#關閉安全模式 hadoop dfsadmin -safemode leave
4. 啟動 hive shell 報錯如下
Caused by: java.lang.IllegalArgumentException: java.net.URISyntaxException: Relative path in absolute URI: ${system:java.io.tmpdir%7D/$%7Bsystem:user.name%7D
解決方案如下:
1.查看hive-site.xml配置,會看到配置值含有"system:java.io.tmpdir"的配置項
2.新建文件夾/home/grid/hive-0.14.0-bin/iotmp,注意權限問題
3.將含有"system:java.io.tmpdir"的配置項的值修改為如上地址
5. 啟動 hive shell 警告如下
WARNING: Hive-on-MR is deprecated in Hive 2 and may not be available in the future versions. Consider using a different execution engine (i.e. spark, tez) or using Hive 1.X releases.
注意只是警告,因為 hive2.x 版本已經不支持 mr,解決方法就是換成 spark
參考資料:
https://blog.csdn.net/u013384984/article/details/80435531
https://www.cnblogs.com/jiangnange/p/9460034.html 配置稍微多點
https://blog.csdn.net/cjfeii/article/details/49423459
https://www.cnblogs.com/dxxblog/p/8193967.html#top
http://www.tianshouzhi.com/api/tutorials/hive/151
https://blog.csdn.net/kongxx/article/details/79418977 hive postgres
https://www.cnblogs.com/slymonkey/p/9967619.html 踩坑記錄
https://blog.csdn.net/pengjunlee/article/details/81737814 踩坑記錄
https://blog.csdn.net/lby0307/article/details/80309225 hive本地模式 schematool無法初始化mysql數據庫