docker 构建hive,并持久化metastore和warehouse

docker-doc

注意

(1)官方的--mount source=warehouse,target=/opt/hive/data/warehouse 中的warehouse是本地位置,docker 默认指定的文件夹内
(2)不可以执行通过文件卷volume -v或者 --mount 方式直接覆盖/opt/hive/conf或者/opt/hive/conf/hive-site.xml会导致启动不成功。必须使用官方教程的SERVICE_OPTS来制定hive-site.xml的几项内容。

mysql 数据库操作

drop database hive_metastore;
CREATE DATABASE hive_metastore;
DROP USER hive;
CREATE USER 'hive'@'%' IDENTIFIED BY 'qaz123';

-- 2. 授予hive_metastore库全部权限(建库、建表、alter、drop,推荐)
GRANT ALL PRIVILEGES ON hive_metastore.* TO 'hive'@'%';
-- 3. 刷新权限
FLUSH PRIVILEGES;

启动hms

docker stop hms; docker rm hms

# 如果有数据库了,可以添加,告知hive不在创建数据库:--env IS_RESUME="true" \

docker run -d -p 9083:9083 \
--env SERVICE_NAME=metastore \
--env DB_DRIVER=mysql \
--env SERVICE_OPTS="-Djavax.jdo.option.ConnectionDriverName=com.mysql.cj.jdbc.Driver -Djavax.jdo.option.ConnectionURL=jdbc:mysql://192.168.100.101:3306/hive_metastore?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true -Djavax.jdo.option.ConnectionUserName=hive -Djavax.jdo.option.ConnectionPassword=qaz123" \
--mount type=bind,src=/var/lib/docker-workspace/containers-data/hive313/warehouse,dst=/user/hive/warehouse \
--mount type=bind,src=/var/lib/docker-workspace/containers-data/hive313/mysql-connector-j-8.4.0.jar,dst=/opt/hive/lib/mysql-connector-j-8.4.0.jar \
--name hms \
apache/hive:3.1.3

docker logs -f hms

启动hs2

docker stop hs2 ; docker rm hs2
chown -R 1000:1000 /var/lib/docker-workspace/containers-data/hive313
rm -rf /var/lib/docker-workspace/containers-data/hive313/warehouse/*

docker run -d \
-p 10000:10000 \
-p 10002:10002 \
--env SERVICE_NAME=hiveserver2 \
--env SERVICE_OPTS="-Dhive.metastore.uris=thrift://192.168.100.101:9083" \
--mount type=bind,src=/var/lib/docker-workspace/containers-data/hive313/warehouse,dst=/user/hive/warehouse \
--env IS_RESUME="true" \
--name hs2 \
apache/hive:3.1.3

docker logs -f hs2

docker exec -it hs2 beeline -u jdbc:hive2://localhost:10000

必须创建数据库成功才算没有报错。

create database shop;

metastore正确的日志:
注意使用了外部数据库第一行肯定是+ : mysql类的,不是derby

[root@ts ~]# docker logs hms
+ : mysql
+ SKIP_SCHEMA_INIT=true
+ export HIVE_CONF_DIR=/opt/hive/conf
+ HIVE_CONF_DIR=/opt/hive/conf
+ '[' -d '' ']'
+ export 'HADOOP_CLIENT_OPTS= -Xmx1G -Djavax.jdo.option.ConnectionDriverName=com.mysql.cj.jdbc.Driver -Djavax.jdo.option.ConnectionURL=jdbc:mysql://10.90.36.226:3306/hive_metastore?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true -Djavax.jdo.option.ConnectionUserName=hive -Djavax.jdo.option.ConnectionPassword=qaz123'
+ HADOOP_CLIENT_OPTS=' -Xmx1G -Djavax.jdo.option.ConnectionDriverName=com.mysql.cj.jdbc.Driver -Djavax.jdo.option.ConnectionURL=jdbc:mysql://10.90.36.226:3306/hive_metastore?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true -Djavax.jdo.option.ConnectionUserName=hive -Djavax.jdo.option.ConnectionPassword=qaz123'
+ [[ true == \f\a\l\s\e ]]
+ '[' metastore == hiveserver2 ']'
+ '[' metastore == metastore ']'
+ export METASTORE_PORT=9083
+ METASTORE_PORT=9083
+ exec /opt/hive/bin/hive --skiphadoopversion --skiphbasecp --service metastore
2026-09-18 01:52:38: Starting Hive Metastore Server
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/opt/hive/lib/log4j-slf4j-impl-2.17.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/opt/hadoop/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]

hiveserver2正确的日志
其日志显示信息极少,和传统的hive不一样。没法设置debug模式。
如果报错:[WARN] Failed to create directory: /home/hive/.beeline No such file or directory,那就是初始化hiveserver2失败了。即使新建了文件夹也没用

docker exec -u root hs2 mkdir -p /home/hive/.beeline
docker exec -u root hs2 chown hive:hive /home/hive/.beeline

hiveserver2日志显示:此处的+ : derby无所谓

[root@ts-graph ~]# docker logs hs2
+ : derby
+ SKIP_SCHEMA_INIT=true
+ export HIVE_CONF_DIR=/opt/hive/conf
+ HIVE_CONF_DIR=/opt/hive/conf
+ '[' -d '' ']'
+ export 'HADOOP_CLIENT_OPTS= -Xmx1G -Dhive.metastore.uris=thrift://10.90.36.226:9083'
+ HADOOP_CLIENT_OPTS=' -Xmx1G -Dhive.metastore.uris=thrift://10.90.36.226:9083'
+ [[ true == \f\a\l\s\e ]]
+ '[' hiveserver2 == hiveserver2 ']'
+ export 'HADOOP_CLASSPATH=/opt/tez/*:/opt/tez/lib/*:'
+ HADOOP_CLASSPATH='/opt/tez/*:/opt/tez/lib/*:'
+ exec /opt/hive/bin/hive --skiphadoopversion --skiphbasecp --service hiveserver2
2026-09-18 01:58:13: Starting HiveServer2
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/opt/tez/lib/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/opt/hive/lib/log4j-slf4j-impl-2.17.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/opt/hadoop/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.slf4j.impl.Log4jLoggerFactory]
Hive Session ID = 9c70cf08-0493-459e-86b0-a86c81770a14
Hive Session ID = 688e20bc-4fe2-47fa-99c1-1b89c60cf25a
OK
posted @ 2026-09-18 10:43  lisacumt  阅读(15)  评论(0)    收藏  举报