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()

python中第三方库whl文件下载地址: https://www.lfd.uci.edu/~gohlke/pythonlibs/

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'))

 

posted on 2020-09-10 19:27  蜗牛也是妞  阅读(431)  评论(0)    收藏  举报