使用此链接尝试连接到远程配置单元。下面是使用的代码。下面还给出了收到的错误消息
代码
from pyhive import hive
conn = hive.Connection(host="10.111.22.11", port=10000, username="user1" ,database="default")
错误信息
Could not connect to any of [('10.111.22.11', 10000)]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/anaconda3/lib/python3.6/site-packages/pyhive/hive.py", line 131, in __init__
self._transport.open()
File "/opt/anaconda3/lib/python3.6/site-packages/thrift_sasl/__init__.py", line 61, in open
self._trans.open()
File "/opt/anaconda3/lib/python3.6/site-packages/thrift/transport/TSocket.py",line 113, in open
raise TTransportException(TTransportException.NOT_OPEN, msg)
thrift.transport.TTransport.TTransportException: Could not connect to any of [('10.111.22.11', 10000)]
成功连接还需要哪些条件?我能够直接连接到服务器(使用 putty)并运行配置单元。但是当从另一台服务器 X 尝试时,我收到此错误。我也可以从服务器 X ping 配置单元服务器。
端口号是否有问题?如何检查正确的端口号?
正如下面的答案中所讨论的,我尝试启动 hiveserver2 。但该命令似乎不起作用。非常感谢任何帮助。
当我从 hive shell 执行查询时,我在日志中看到的端口是 8088。想知道这是否应该是端口而不是10000(无论如何都不起作用)
无法使用
pyhive
使其工作。必须使用paramiko
安装
下面是示例代码
import os
import paramiko
import time
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.load_host_keys(os.path.expanduser(os.path.join("~", ".ssh", "known_hosts")))
ssh.connect('1.1.1.1', username='uid', password='pwd')
sshin, sshout, ssherr= ssh.exec_command('hive -e "create table test(key varchar(10),keyval varchar(200))"')
HiveServer2
进程必须在远程 Hive 主机中启动。 10000
是默认端口号。
使用此命令启动 HiveServer2。
$HIVE_HOME/bin/hiveserver2
为了让 PyHive 工作,Hive Server 2.0 应使用“二进制”传输模式运行。如果它以 Http 传输方式运行,请将其更改为“二进制”。
请尝试以下代码使用 pyhive 访问远程 Hive 表:
from pyhive import hive
import pandas as pd
#Create Hive connection
conn = hive.Connection(host="10.111.22.11", port=10000, username="user1")
# Read Hive table and Create pandas dataframe
df = pd.read_sql("SELECT * FROM db_Name.table_Name limit 10", conn)
print(df.head())
首先 hive.server2.transport.mode 应设置为二进制
然后使用以下代码
from pyhive import hive
host_name = "10.3.141.44"
port = 10000
database="ncc"
def hiveconnection(host_name, port, database):
conn = hive.Connection(host=host_name, port=port,
database=database, auth='NOSASL')
cur = conn.cursor()
cur.execute('select * from registry')
result = cur.fetchall()
return result
output = hiveconnection(host_name, port, database)
print(output)