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中文網其他相關文章!