通过python从远程服务器访问hive

dxxyhpgq  于 2021-05-29  发布在  Hadoop
关注(0)|答案(2)|浏览(356)

我在远程服务器上安装了以下必要的包,以便通过python访问hive。
python 2.7.6版,
python开发工具,
pyhs2号,
sasl-0.1.3,
节俭-0.9.1,
Hive-0.1.0
下面是访问配置单元的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()

这是我得到的结果:

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

在执行脚本之后,我没有看到输出中有任何错误,但是在屏幕上我没有看到任何查询结果。我不知道为什么它没有显示任何查询结果,配置单元服务器ip、端口、用户和密码是否正确。我还验证了配置单元服务器和远程服务器之间的连接,没有连接问题。

izkcnapc

izkcnapc1#

我已经通过以下方法获得了访问权限

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
6qqygrtg

6qqygrtg2#

尝试使用以下代码:

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

相关问题