Hive 安裝配置教程 (Deepin/Ubuntu)
一、環境準備
1. 前置條件
- ✅ JDK 8 已安裝
- ✅ Hadoop 已安裝並正常運行
- ✅ MariaDB 已安裝並配置好 Hive 數據庫
- Hive 版本: 3.1.3 (推薦)
- 安裝路徑:
/usr/local/hive
2. 驗證前置環境
# 驗證 Hadoop
jps
# 應該看到 NameNode, DataNode, ResourceManager, NodeManager
# 驗證 MariaDB
mysql -u hive -phive123456 -e "SHOW DATABASES;"
# 應該能看到 hive_metastore 數據庫
二、下載 Hive
1. 官方下載地址
# Apache Hive 官網
https://hive.apache.org/downloads.html
# 推薦版本: Hive 3.1.3
https://archive.apache.org/dist/hive/hive-3.1.3/apache-hive-3.1.3-bin.tar.gz
2. 使用 wget 下載
cd ~/Downloads
wget https://archive.apache.org/dist/hive/hive-3.1.3/apache-hive-3.1.3-bin.tar.gz
三、安裝 Hive
1. 解壓並移動
# 解壓
tar -zxvf apache-hive-3.1.3-bin.tar.gz
# 移動到安裝目錄
sudo mv apache-hive-3.1.3-bin /usr/local/hive
# 設置所有權
sudo chown -R $USER:$USER /usr/local/hive
2. 配置環境變量
編輯系統環境變量:
sudo vim /etc/profile
在文件末尾添加:
# Hive Environment
export HIVE_HOME=/usr/local/hive
export HIVE_CONF_DIR=$HIVE_HOME/conf
export PATH=$PATH:$HIVE_HOME/bin
使配置生效:
source /etc/profile
3. 驗證安裝
hive --version
輸出示例:
Hive 3.1.3
Git git://chaos-mbp.lan/Users/chao/git/hive -r 4df4d75bf1e16fe0af75aad0b4179c34c07fc975
Compiled by chao on Sun Nov 15 09:04:22 PST 2020
四、安裝 MySQL JDBC 驅動
Hive 需要 JDBC 驅動連接 MariaDB。
cd ~/Downloads
# 下載 MySQL Connector/J
wget https://repo1.maven.org/maven2/mysql/mysql-connector-java/8.0.33/mysql-connector-java-8.0.33.jar
# 複製到 Hive lib 目錄
cp mysql-connector-java-8.0.33.jar /usr/local/hive/lib/
五、配置 Hive
1. 創建 Hive 配置文件
cd /usr/local/hive/conf cp hive-default.xml.template hive-site.xml
2. 編輯 hive-site.xml
重要: hive-default.xml.template 文件太大,建議刪除後重新創建:
rm -f hive-site.xml vim hive-site.xml
完整配置內容:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<!-- Hive Metastore 數據庫連接 -->
<property>
<name>javax.jdo.option.ConnectionURL</name>
<value>jdbc:mysql://localhost:3306/hive_metastore?createDatabaseIfNotExist=true&useSSL=false&useUnicode=true&characterEncoding=UTF-8</value>
<description>JDBC connect string for a JDBC metastore</description>
</property>
<property>
<name>javax.jdo.option.ConnectionDriverName</name>
<value>com.mysql.cj.jdbc.Driver</value>
<description>Driver class name for a JDBC metastore</description>
</property>
<property>
<name>javax.jdo.option.ConnectionUserName</name>
<value>hive</value>
<description>Username to use against metastore database</description>
</property>
<property>
<name>javax.jdo.option.ConnectionPassword</name>
<value>hive123456</value>
<description>Password to use against metastore database</description>
</property>
<!-- Hive 元數據存儲版本驗證 -->
<property>
<name>hive.metastore.schema.verification</name>
<value>false</value>
<description>Enforce metastore schema version consistency</description>
</property>
<property>
<name>hive.metastore.event.db.notification.api.auth</name>
<value>false</value>
</property>
<!-- Hive Metastore 服務地址 -->
<property>
<name>hive.metastore.uris</name>
<value>thrift://localhost:9083</value>
<description>Thrift URI for the remote metastore</description>
</property>
<!-- Hive 數據倉庫目錄 -->
<property>
<name>hive.metastore.warehouse.dir</name>
<value>/user/hive/warehouse</value>
<description>Location of default database for the warehouse</description>
</property>
<!-- HiveServer2 配置 -->
<property>
<name>hive.server2.thrift.bind.host</name>
<value>localhost</value>
<description>Bind host on which to run the HiveServer2 Thrift service</description>
</property>
<property>
<name>hive.server2.thrift.port</name>
<value>10000</value>
<description>Port number of HiveServer2 Thrift interface</description>
</property>
<!-- 解決日誌衝突 -->
<property>
<name>hive.metastore.event.db.listener.timetolive</name>
<value>600s</value>
</property>
<!-- 顯示列名 -->
<property>
<name>hive.cli.print.header</name>
<value>true</value>
<description>Whether to print the names of the columns in query output</description>
</property>
<property>
<name>hive.cli.print.current.db</name>
<value>true</value>
<description>Whether to include the current database in the Hive prompt</description>
</property>
</configuration>
3. 配置 hive-env.sh
cp hive-env.sh.template hive-env.sh vim hive-env.sh
在文件末尾添加:
# Hadoop 路徑
export HADOOP_HOME=/usr/local/hadoop
# Hive 配置目錄
export HIVE_CONF_DIR=/usr/local/hive/conf
# Hive lib 目錄
export HIVE_AUX_JARS_PATH=/usr/local/hive/lib
六、初始化 Hive Metastore
1. 在 HDFS 上創建 Hive 目錄
# 啓動 Hadoop (如果還未啓動)
start-dfs.sh
start-yarn.sh
# 創建 Hive 倉庫目錄
hdfs dfs -mkdir -p /user/hive/warehouse
hdfs dfs -mkdir -p /tmp
# 設置權限
hdfs dfs -chmod g+w /user/hive/warehouse
hdfs dfs -chmod 777 /tmp
2. 初始化 Metastore 數據庫
cd /usr/local/hive
# 使用 schematool 初始化元數據庫
schematool -dbType mysql -initSchema
看到以下輸出表示成功:
Initialization script completed schemaTool completed
3. 驗證初始化
登錄 MariaDB 查看錶:
mysql -u hive -phive123456 hive_metastore -e "SHOW TABLES;"
應該看到很多 Hive 元數據表,如: TBLS, DBS, COLUMNS_V2 等。
七、解決 Guava 版本衝突問題
Hive 和 Hadoop 的 Guava 版本可能不一致,需要統一。
1. 檢查版本
# 查看 Hadoop 的 Guava 版本
ls /usr/local/hadoop/share/hadoop/common/lib/guava-*.jar
# 查看 Hive 的 Guava 版本
ls /usr/local/hive/lib/guava-*.jar
2. 刪除 Hive 的舊版本
rm /usr/local/hive/lib/guava-*.jar
3. 複製 Hadoop 的 Guava
cp /usr/local/hadoop/share/hadoop/common/lib/guava-27.0-jre.jar /usr/local/hive/lib/
八、啓動 Hive 服務
1. 啓動 Hive Metastore (必須先啓動)
nohup hive --service metastore > ~/hive-metastore.log 2>&1 &
保存進程 ID:
echo $! > ~/hive-metastore.pid
2. 驗證 Metastore 啓動
# 檢查端口
netstat -tlnp | grep 9083
# 查看日誌
tail -f ~/hive-metastore.log
3. 啓動 HiveServer2 (可選,用於遠程連接)
nohup hive --service hiveserver2 > ~/hive-server2.log 2>&1 &
保存進程 ID:
echo $! > ~/hive-server2.pid
4. 驗證 HiveServer2 啓動
# 檢查端口
netstat -tlnp | grep 10000
# 查看日誌
tail -f ~/hive-server2.log
5. 啓動 Hive CLI
hive
如果能進入 Hive 命令行,説明安裝成功!
hive> show databases;
OK
default
Time taken: 0.5 seconds, Fetched: 1 row(s)
九、常見問題與解決方案
問題1: Hive Metastore 連接失敗
錯誤信息:
MetaException(message:Could not connect to meta store using any of the URIs provided)
原因: Metastore 服務未啓動
解決方案:
# 啓動 Metastore
nohup hive --service metastore > ~/hive-metastore.log 2>&1 &
# 檢查端口
netstat -tlnp | grep 9083
# 查看日誌
tail -f ~/hive-metastore.log
問題2: MySQL 連接失敗
錯誤信息:
Communications link failure
檢查項:
- MariaDB 服務是否運行
- hive 用户權限是否正確
- JDBC 驅動是否存在
解決方案:
# 檢查 MariaDB
sudo systemctl status mariadb
# 測試連接
mysql -u hive -phive123456 -e "SHOW DATABASES;"
# 檢查 JDBC 驅動
ls /usr/local/hive/lib/mysql-connector-java-*.jar
問題3: Guava 版本衝突
錯誤信息:
java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkArgument
解決方案:
# 刪除 Hive 的 Guava
rm /usr/local/hive/lib/guava-*.jar
# 複製 Hadoop 的 Guava
cp /usr/local/hadoop/share/hadoop/common/lib/guava-*.jar /usr/local/hive/lib/
問題4: 無法創建表或插入數據
錯誤信息:
Permission denied: user=xxx, access=WRITE, inode="/user/hive/warehouse"
解決方案:
# 設置 HDFS 目錄權限
hdfs dfs -chmod -R 777 /user/hive/warehouse
hdfs dfs -chmod -R 777 /tmp
問題5: 虛擬機掛起後 Hive 服務停止
解決方案:
# 停止服務 (如果還在運行)
kill $(cat ~/hive-metastore.pid)
kill $(cat ~/hive-server2.pid)
# 清理殘留進程
ps aux | grep RunJar | grep hive | awk '{print $2}' | xargs kill -9
# 重新啓動
nohup hive --service metastore > ~/hive-metastore.log 2>&1 &
echo $! > ~/hive-metastore.pid
nohup hive --service hiveserver2 > ~/hive-server2.log 2>&1 &
echo $! > ~/hive-server2.pid
問題6: 無法通過端口檢測 Hive 服務
現象: jps 看到 RunJar,但不知道是什麼服務
解決方案: 通過端口檢測
# Metastore 端口 9083
netstat -tlnp | grep 9083
# HiveServer2 端口 10000
netstat -tlnp | grep 10000
十、Hive 基本使用
1. 數據庫操作
-- 顯示所有數據庫
SHOW DATABASES;
-- 創建數據庫
CREATE DATABASE test_db;
-- 使用數據庫
USE test_db;
-- 刪除數據庫
DROP DATABASE test_db;
2. 創建表
-- 創建內部表
CREATE TABLE students (
id INT,
name STRING,
age INT
)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '\t'
STORED AS TEXTFILE;
-- 創建外部表 (數據存儲在 HDFS)
CREATE EXTERNAL TABLE orders (
order_id INT,
user_id INT,
amount DOUBLE
)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '\t'
LOCATION '/user/data/orders';
3. 加載數據
-- 從本地文件加載
LOAD DATA LOCAL INPATH '/path/to/data.txt' INTO TABLE students;
-- 從 HDFS 加載
LOAD DATA INPATH '/user/data/data.txt' INTO TABLE students;
4. 查詢數據
-- 查詢所有
SELECT * FROM students;
-- 條件查詢
SELECT * FROM students WHERE age > 20;
-- 統計
SELECT COUNT(*) FROM students;
十一、停止 Hive 服務
# 停止 HiveServer2
kill $(cat ~/hive-server2.pid)
rm ~/hive-server2.pid
# 停止 Metastore
kill $(cat ~/hive-metastore.pid)
rm ~/hive-metastore.pid
# 如果進程未正常停止,強制停止
ps aux | grep RunJar | grep -E 'metastore|hiveserver2' | awk '{print $2}' | xargs kill -9
十二、重要端口與服務説明
|
服務
|
端口
|
説明
|
檢測命令
|
|
Metastore
|
9083
|
Hive 元數據服務
|
|
|
HiveServer2
|
10000
|
Hive 查詢服務
|
|
十三、日誌文件位置
- Metastore 日誌:
~/hive-metastore.log - HiveServer2 日誌:
~/hive-server2.log - Hive 系統日誌:
/usr/local/hive/logs/
十四、配置文件位置彙總
所有配置文件位於: /usr/local/hive/conf/
hive-site.xml- Hive 核心配置hive-env.sh- Hive 環境變量hive-log4j2.properties- 日誌配置
安裝完成後,繼續安裝 DataX 和 DataX Web