Python如何连接和启动Hive

伊谢尔伦
伊谢尔伦 原创
2017-04-29 10:22:13 1907浏览

1.在使用Python连接hive之前,需要将hive安装包下的lib/py中的文件拷贝到python的sys.path中的site-packages下,否则引入对应的包会报错,这个是使用hive提供的Python接口来调用hive客户端。

2 启动hive 的thrift

确保以下服务开启:

hive --service hiveserver

默认端口是10000

from hive_service import ThriftHive
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
def ReadHiveTest(sql):
try:
tSocket = TSocket.TSocket('172.18.1.88',10000)
tTransport = TTransport.TBufferedTransport(tSocket)
protocol = TBinaryProtocol.TBinaryProtocol(tTransport)
client = ThriftHive.Client(protocol)
tTransport.open()
client.execute(sql)
return client.fetchAll()
except Thrift.TException, tx:
print '%s' % (tx.message)
finally:
tTransport.close()
if __name__ == '__main__':
showDatabasesSql = 'show databases'
showTablesSql = 'show tables'
selectSql = 'SELECT * FROM 07_jn_mysql_2'
result = ReadHiveTest(selectSql)
print(result[1])

以上就是Python如何连接和启动Hive的详细内容,更多请关注php中文网其它相关文章!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。