【问题标题】:How to query Hive from python connecting using Zookeeper?如何使用 Zookeeper 从 python 连接查询 Hive?
【发布时间】:2022-08-12 04:30:58
【问题描述】:
我可以使用pyhive 连接到 Hive 数据库,并且可以查询修复服务器主机的数据库。这是一个代码示例:
from pyhive import hive
host_name = \"vrt1553.xxx.net\"
port = 10000
connection = hive.Connection(
host=host_name,
port=port,
username=user,
kerberos_service_name=\'hive\',
auth=\'KERBEROS\',
)
cursor = connection.cursor()
cursor.execute(\'show databases\')
print(cursor.fetchall())
如何使用 Zookeeper 进行连接以获取服务器名称?
标签:
hive
database-connection
apache-zookeeper
【解决方案1】:
您必须安装 Kazoo 包来查询 Zookeeper 并找到您的 Hive 服务器的主机和端口:
import random
from kazoo.client import KazooClient
zk = KazooClient(hosts='vrt1554.xxx.net:2181,vrt1552.xxx.net:2181,vrt1558.xxx.net:2181', read_only=True)
zk.start()
servers = [hiveserver2.split(';')[0].split('=')[1].split(':')
for hiveserver2
in zk.get_children(path='hiveserver2')]
hive_host, hive_port = random.choice(servers)
zk.stop()
print(hive_host, hive_port)
然后只需将 hive_host 和 hive_port 传递给您的 Connection 构造函数。