当部署的 Metastore 宕机或 HiveServer2 服务宕机时,两个服务可能持续相当长的时间不可用,直到服务被重新拉起。为了避免这种服务中断情况,在真实生产环境中需要部署 Hive Metastore 高可用及 HiveServer2 的高可用。
那么怎样实现 Hive 高可用呢,下面分别从实现 HiveServer2 高可用和实现 Metastore 高可用两个方面讲解。
HiveServer2 高可用
Hive 从 0.14 开始,使用 Zookeeper 实现了 HiveServer2 的 HA 功能(ZooKeeper Service Discovery),Client 端可以通过指定一个 nameSpace 来连接 HiveServer2,而不是指定某一个 host 和 port,本文学习和研究 HiveServer2 的高可用配置。
xjxBOb
假设现在准备在 node1 和 node3 上分别启用两个 HiveServer2 的实例,并通过 zookeeper 完成 HA 的配置。
node01 节点修改 hive-site.xml 配置
<property>
<name>hive.server2.support.dynamic.service.discovery</name>
<value>true</value>
</property>
<property>
<name>hive.server2.zookeeper.namespace</name>
<value>hiveserver2_zk</value>
</property>
<property>
<name>hive.zookeeper.quorum</name>
<value> node01:2181,node02:2181,node03:2181</value>
</property>
<property>
<name>hive.zookeeper.client.port</name>
<value>2181</value>
</property>
复制代码
node03 节点同步配置及信息修改
将安装好的 hive 文件夹同步到 node03 节点上,修改 node03 上的 hive-site.xml 配置如下:
<property>
<name>hive.server2.thrift.bind.host</name>
<value>node03</value>
</property>
复制代码
重启服务
分别重启 node01,node03 节点上的的 hiveServer2 和 metaStore 服务
nohup hive --service hiveserver2 >> /opt/module/apache-hive-2.1.1-bin/hiveserver.log 2>&1 &
nohup hive --service metastore >> /opt/module/apache-hive-2.1.1-bin/metastore.log 2>&1 &
复制代码
在 Zookeeper 中检查配置
修改完配置后,可通过 zookeeper_client 命令进行查看,hiveserver2 在 zookeeper 中是否注册成功
[zk: localhost:2181(CONNECTED) 1] ls /hiveserver2_zk
[serverUri=0.0.0.0:10001;version=2.1.1-cdh6.3.2;sequence=0000000000]
复制代码
beeline 连接测试
beeline> !connect jdbc:hive2://node01:2181,node02:2181,node03:2181/;serviceDiscoveryMode=zooKeeper;zooKeeperNamespace=hiveserver2_zk
复制代码
JDBC 连接的 URL 格式和参数含义说明:
jdbc:hive2://<zookeeper quorum>/<dbName>;serviceDiscoveryMode=zooKeeper;zooKeeperNamespace=hiveserver2
// 参数含义
<zookeeper quorum> 为Zookeeper的集群链接串,如node1:2181,node2:2181,node3:2181
<dbName> 为Hive数据库,默认为default
serviceDiscoveryMode=zooKeeper 指定模式为zooKeeper
zooKeeperNamespace=hiveserver2 指定ZK中的nameSpace,即参数hive.server2.zookeeper.namespace所定义
复制代码
Metastore 高可用
原理说明
常规连接原理:
高可用原理:
修改节点配置
修改 node01、node03 节点 hive 配置文件 hive-site.xml
<property>
<name>hive.metastore.uris</name>
<value>thrift://node01:9083,thrift://node03:9083</value>
</property>
复制代码
重启服务
分别重启 node01,node02 节点上的的 hiveServer2 和 metaStore 服务
nohup hive --service hiveserver2 >> /opt/module/apache-hive-2.1.1-bin/hiveserver.log 2>&1 &
nohup hive --service metastore >> /opt/module/apache-hive-2.1.1-bin/metastore.log 2>&1 &
复制代码
测试验证
验证 HiveServer2 是否是高可用
在 node03 节点上,杀掉占用 10000 端口的进程,即杀掉 node03 的 hiveServer2 进程
[root@node03 logs]$ netstat -ntpl |grep 10000
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
tcp6 0 0 :::10000 :::* LISTEN 87776/java
复制代码
在 Zookeeper 中检查配置
[zk: localhost:2181(CONNECTED) 1] ls /hiveserver2_zk
[serverUri=0.0.0.0:10001;version=2.1.1-cdh6.3.2;sequence=0000000000]
复制代码
beeline 测试连接
[root@node01 ~]# beeline
WARNING: Use "yarn jar" to launch YARN applications.
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/opt/cloudera/parcels/CDH-6.3.2-1.cdh6.3.2.p0.1605554/jars/log4j-slf4j-impl-2.8.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/opt/cloudera/parcels/CDH-6.3.2-1.cdh6.3.2.p0.1605554/jars/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]
Beeline version 2.1.1-cdh6.3.2 by Apache Hive
beeline>
beeline> !connect jdbc:hive2://node01:2181,node02:2181,node03:2181/;serviceDiscoveryMode=zooKeeper;zooKeeperNamespace=hiveserver2_zk
Connecting to jdbc:hive2://node01:2181,node02:2181,node03:2181/;serviceDiscoveryMode=zooKeeper;zooKeeperNamespace=hiveserver2_zk
Enter username for jdbc:hive2://node01:2181,node02:2181,node03:2181/: hive
Enter password for jdbc:hive2://node01:2181,node02:2181,node03:2181/:
21/07/01 09:31:18 [main]: INFO jdbc.HiveConnection: Connected to 0.0.0.0:10001
Connected to: Apache Hive (version 2.1.1-cdh6.3.2)
Driver: Hive JDBC (version 2.1.1-cdh6.3.2)
Transaction isolation: TRANSACTION_REPEATABLE_READ
0: jdbc:hive2://node01:2181,node02:2181,node0> show tables;
INFO : Compiling command(queryId=hive_20210701093131_c1958b66-3e2a-443d-8562-22f00f4bb463): show tables
INFO : Semantic Analysis Completed
INFO : Returning Hive schema: Schema(fieldSchemas:[FieldSchema(name:tab_name, type:string, comment:from deserializer)], properties:null)
INFO : Completed compiling command(queryId=hive_20210701093131_c1958b66-3e2a-443d-8562-22f00f4bb463); Time taken: 1.164 seconds
INFO : Executing command(queryId=hive_20210701093131_c1958b66-3e2a-443d-8562-22f00f4bb463): show tables
INFO : Starting task [Stage-0:DDL] in serial mode
INFO : Completed executing command(queryId=hive_20210701093131_c1958b66-3e2a-443d-8562-22f00f4bb463); Time taken: 0.046 seconds
INFO : OK
+-----------+
| tab_name |
+-----------+
| score4 |
| stu |
+-----------+
2 rows selected (1.728 seconds)
复制代码
验证 mestastore 是否是高可用
node03 节点杀死 mestastore 进程
[root@node01 ~]# ps -ef | grep metastore
hive 19802 19786 2 09:23 ? 00:00:33 /usr/java/jdk1.8.0_181-cloudera/bin/java -Dproc_jar -Djava.net.preferIPv4Stack=true -Djava.net.preferIPv4Stack=true -Xms576716800 -Xmx576716800 -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=70 -XX:+CMSParallelRemarkEnabled -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/tmp/hive_hive-HIVEMETASTORE-2c60fdac6f3da589eafb946bedf8838a_pid19802.hprof -XX:OnOutOfMemoryError=/opt/cloudera/cm-agent/service/common/killparent.sh -Dlog4j.configurationFile=hive-log4j2.properties -Dlog4j.configurationFile=hive-log4j2.properties -Djava.util.logging.config.file=/opt/cloudera/parcels/CDH-6.3.2-1.cdh6.3.2.p0.1605554/lib/hive/bin/../conf/parquet-logging.properties -Dyarn.log.dir=/opt/cloudera/parcels/CDH-6.3.2-1.cdh6.3.2.p0.1605554/lib/hadoop/logs -Dyarn.log.file=hadoop.log -Dyarn.home.dir=/opt/cloudera/parcels/CDH-6.3.2-1.cdh6.3.2.p0.1605554/lib/hadoop/libexec/../../hadoop-yarn -Dyarn.root.logger=INFO,console -Djava.library.path=/opt/cloudera/parcels/CDH-6.3.2-1.cdh6.3.2.p0.1605554/lib/hadoop/lib/native -Dhadoop.log.dir=/opt/cloudera/parcels/CDH-6.3.2-1.cdh6.3.2.p0.1605554/lib/hadoop/logs -Dhadoop.log.file=hadoop.log -Dhadoop.home.dir=/opt/cloudera/parcels/CDH-6.3.2-1.cdh6.3.2.p0.1605554/lib/hadoop -Dhadoop.id.str=hive -Dhadoop.root.logger=INFO,console -Dhadoop.policy.file=hadoop-policy.xml -Dhadoop.security.logger=INFO,NullAppender org.apache.hadoop.util.RunJar /opt/cloudera/parcels/CDH-6.3.2-1.cdh6.3.2.p0.1605554/lib/hive/lib/hive-service-2.1.1-cdh6.3.2.jar org.apache.hadoop.hive.metastore.HiveMetaStore -p 9083
root 27765 27722 0 09:49 pts/2 00:00:00 grep --color=auto metastore
kill -9 19802
复制代码
执行查询语句
0: jdbc:hive2://node01:2181,node02:2181,node0> select * from stu limit 1;
INFO : Compiling command(queryId=hive_20210701093806_b5920638-19be-42fb-921f-b81206a1f35f): select * from stu limit 1
INFO : Semantic Analysis Completed
INFO : Returning Hive schema: Schema(fieldSchemas:[FieldSchema(name:stu.id, type:int, comment:null), FieldSchema(name:stu.name, type:string, comment:null)], properties:null)
INFO : Completed compiling command(queryId=hive_20210701093806_b5920638-19be-42fb-921f-b81206a1f35f); Time taken: 0.366 seconds
INFO : Executing command(queryId=hive_20210701093806_b5920638-19be-42fb-921f-b81206a1f35f): select * from stu limit 1
INFO : Completed executing command(queryId=hive_20210701093806_b5920638-19be-42fb-921f-b81206a1f35f); Time taken: 0.001 seconds
INFO : OK
+---------+-----------+
| stu.id | stu.name |
+---------+-----------+
| 1 | zhangsan |
+---------+-----------+
1 row selected (0.546 seconds)
复制代码
至此, HiveServer2 及 Metastore 的多实例高可用 Ha 配置完成,能解决生产中的很多问题,比如:并发、负载均衡、单点故障、安全等等,故而强烈建议在生产环境中使用该模式来提供 Hive 服务。
如果你认为作者写的不错!可以关注作者公众号 “白程序员的自习室”获取更多内容。
评论