更新时间:2022-07-19 GMT+08:00
分享

Python样例代码

功能简介

通过连接zookeeper上的对应znode获取到当前主JDBCServer的IP和PORT,然后使用pyhive连接到这个JDBCServer,从而实现在JDBCServer-ha模式下,出现主备倒换后不需要修改代码依旧就能直接访问新的主JDBCServer服务。

该功能仅支持普通集群(未开启Kerberos认证的集群)使用。

环境准备

  1. 安装支持环境。(开发环境请参考环境简介准备)

    执行以下命令安装编译工具:

    yum install cyrus-sasl-devel -y

    yum install gcc-c++ -y

  2. 安装相应的python模块。

    需要安装sasl,thrift,thrift-sasl,PyHive。

    pip install sasl

    pip install thrift

    pip install thrift-sasl

    pip install PyHive

  3. 安装python连接zookeeper工具。

    pip install kazoo

  4. 从MRS集群上获取相应参数。
    • zookeeper的IP和PORT:

      可以查看配置文件/opt/client/Spark/spark/conf/hive-site.xml中的配置项spark.deploy.zookeeper.url

    • zookeeper 上存放JDBCServer主节点的IP和PORT:

      可以查看配置文件/opt/client/Spark/spark/conf/hive-site.xml中的配置项spark.thriftserver.zookeeper.dir(默认是/thriftserver),在此znode子节点(active_thriftserver)上存放了JDBCServer主节点的IP和PORT

样例代码

from kazoo.client import KazooClient
zk = KazooClient(hosts='ZookeeperHost')
zk.start()
result=zk.get("/thriftserver/active_thriftserver")
result=result[0].decode('utf-8')
JDBCServerHost=result[0].split(":")[0]
JDBCServerPort=result[0].split(":")[1]
from pyhive import hive
conn = hive.Connection(host=JDBCServerHost, port=JDBCServerPort,database='default')
cursor=conn.cursor()
cursor.execute("select * from test")
for result in cursor.fetchall():
    print result

其中,ZookeeperHost使用4获取到的zookeeper IP和PORT替换。

分享:

    相关文档

    相关产品