#!/usr/bin/env python
# -*- coding: utf-8 -*-
# hive util with hive server2
"""
@author:
@create:
"""
__author__ = 'knktc'
__version__ = '0.1'
import pyhs2
class HiveClient:
def __init__(self, db_host, user, password, database, port=10000, authMechanism="PLAIN"):
"""
create connection to hive server2
"""
self.conn = pyhs2.connect(host=db_host,
port=port,
authMechanism=authMechanism,
user=user,
password=password,
database=database
)
def query(self, sql):
"""
query
"""
with self.conn.cursor() as cursor:
cursor.execute(sql)
return cursor.fetch()
def close(self):
"""
close connection
"""
self.conn.close()
def main():
"""
main process
@rtype:
@return:
@note:
"""
hive_client = HiveClient(db_host='127.0.0.1', port=10086, user='', password='', database='db', authMechanism='PLAIN')
print hive_cient.getDatabases()
result = hive_client.query("select * from test_db t where t.dt = '2017-03-01' limit 1")
print result
hive_client.close()
if __name__ == '__main__':
main()
windows下32位没成功,报错(64位可以http://www.lfd.uci.edu/~gohlke/pythonlibs/ 下载
File "build\bdist.win32\egg\pyhs2\cloudera\thrift_sasl.py", line 66, inopen
thrift.transport.TTransport.TTransportException: Could not start SASL: Errorinsasl_client_start (-4) SASL(-4): no mechanism available: Unable to find a callba
python -m pip install -U pip
python-m pip install pyOpenSSL
https://pypi.python.org/pypi/pyhs2
python-m pip install sasl
http://aka.ms/vcpython27
python-m pip install sasl-0.2.1-cp27-cp27m-win32.whl
python-m pip install thrift-0.10.0-cp27-cp27m-win32.whl
linux安装
pyhs2,cyrus-sasl(cyrus-sasl-plain cyrus-sasl-devel cyrus-sasl-gssapi),gcc,libxml2-devel,libxslt-devel