1. 程式人生 > 實用技巧 >python 訪問hive

python 訪問hive

1、安裝

pip install impyla==0.14.1
pip install pure_sasl==0.5.1
pip install thriftpy==0.3.9
pip install thrift-sasl==0.2.1 
pip install thrift
pip install bitarray

1、報錯:thriftpy.parser.exc.ThriftParserError: ThriftPy does not support generating module with path in protocol 'c'
解決:
將 C:\Python27\Lib\site-packages\thriftpy\parser\parser.py , line 488

if url_scheme == '':
改為:
if len(url_scheme) <= 1:
2、報錯:TypeError: can't concat str to bytes
定位到錯誤的最後一條,在init.py第94行(標黃的部分)
header = struct.pack(">BI", status, len(body))
#按照網上的提供的辦法增加對BODY的處理
if (type(body) is str):
body = body.encode()
self._trans.write(header + body)
self._trans.flush()

2、使用

import impala.dbapi as ipdb

class HiveControl:
    def __init__(self, db_name='default'):
        self.conn = ipdb.connect(host='192.168.XX.XX', port=10000, database=db_name,  user='user', password='password',
auth_mechanism='PLAIN') self.cursor = self.conn.cursor()
# 預設為NONE,另外還可以為’NOSASL’, ‘PLAIN’, ‘KERBEROS’, ‘LDAP’, ‘CUSTOM’. def excuteSql(self, sql): try: self.cursor.execute(sql) res = self.cursor.fetchall() except Exception as e: self.cursor.close() self.conn.close() raise (e) self.cursor.close() self.conn.close() return res hiv = HiveControl() print(hiv.excuteSql('show databases'))