简体   繁体   English

通过 Python 从远程服务器访问 Hive

[英]Accessing Hive from remote server through Python

I have installed following necessary packages on the remote server to access Hive through Python.我在远程服务器上安装了以下必要的包,以便通过 Python 访问 Hive。

Python 2.7.6, Python 2.7.6,
Python development tools, Python开发工具,
pyhs2, pyhs2,
sasl-0.1.3, sasl-0.1.3,
thrift-0.9.1,节俭-0.9.1,
PyHive-0.1.0 PyHive-0.1.0

Here is the Python script to access Hive.这是访问 Hive 的 Python 脚本。

#!/usr/bin/env python
import pyhs2 as hive
import getpass
DEFAULT_DB = 'camp'
DEFAULT_SERVER = '10.25.xx.xx'
DEFAULT_PORT = 10000
DEFAULT_DOMAIN = 'xxx.xxxxxx.com'

# Get the username and password
u = raw_input('Enter PAM username: ')
s = getpass.getpass()
# Build the Hive Connection
connection = hive.connect(host=DEFAULT_SERVER, port=DEFAULT_PORT,    authMechanism='LDAP', user=u + '@' + DEFAULT_DOMAIN, password=s)
# Hive query statement
statement = "select * from camp.test"
cur = connection.cursor()

# Runs a Hive query and returns the result as a list of list
cur.execute(statement)
df = cur.fetchall()

Here is the output I got:这是我得到的输出:

  File "build/bdist.linux-x86_64/egg/pyhs2/__init__.py", line 7, in connect
  File "build/bdist.linux-x86_64/egg/pyhs2/connections.py", line 46, in __init__
  File "build/bdist.linux-x86_64/egg/pyhs2/cloudera/thrift_sasl.py", line 74, in open
  File "build/bdist.linux-x86_64/egg/pyhs2/cloudera/thrift_sasl.py", line 92, in _recv_sasl_message
  File "build/bdist.linux-x86_64/egg/thrift/transport/TTransport.py", line 58, in readAll
  File "build/bdist.linux-x86_64/egg/thrift/transport/TSocket.py", line 118, in read
thrift.transport.TTransport.TTransportException: TSocket read 0 bytes

I don't see any error in the output after executing the script, however I don't see any query results on the screen.执行脚本后,我没有在输出中看到任何错误,但是我在屏幕上没有看到任何查询结果。 I'm not sure why it's not displaying any query results, Hive server IP, port, user and password are correct.我不知道为什么它不显示任何查询结果,Hive 服务器 IP、端口、用户和密码是正确的。 I also verified connectivity between hive server and remote server, no issues with connectivity.我还验证了配置单元服务器和远程服务器之间的连接,连接没有问题。

Try using this code:尝试使用此代码:

import pyhs2

with pyhs2.connect(host='localhost',
                   port=10000,
                   authMechanism="PLAIN",
                   user='root',
                   password='test',
                   database='default') as conn:
    with conn.cursor() as cur:
        #Show databases
        print cur.getDatabases()

        #Execute query
        cur.execute("select * from table")

        #Return column info from query
        print cur.getSchema()

        #Fetch table results
        for i in cur.fetch():
            print i

I've managed to get access by using the following我设法通过使用以下方法获得访问权限

from pyhive import presto
DEFAULT_DB = 'XXXXX'
DEFAULT_SERVER = 'server.name.blah'
DEFAULT_PORT = 8000

# Username
u = "user"

# Build the Hive Connection
connection = presto.connect(host=DEFAULT_SERVER, port=DEFAULT_PORT, username=u)

# Hive query statement
statement = "select * from public.dudebro limit 5"
cur = connection.cursor()

# Runs a Hive query and returns the result as a list of list
cur.execute(statement)
df = cur.fetchall()
print df

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM