Home  >  Article  >  Backend Development  >  How to connect and start Hive in Python

How to connect and start Hive in Python

伊谢尔伦
伊谢尔伦Original
2017-04-29 10:22:132383browse

1. Before using Python to connect to hive, you need to copy the files in lib/py under the hive installation package to site-packages in sys.path of python, otherwise an error will be reported when introducing the corresponding package. This is to use The Python interface provided by hive is used to call the hive client.

2 Start hive's thrift

Make sure the following services are enabled:

hive --service hiveserver

The default port is 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])

The above is the detailed content of How to connect and start Hive in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn