1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
""" @author:knktc @create:2014-04-08 16:55 """
__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='hiveserver2.hadoop', port=10000, user='hdfs', password='mypass', database='test_log', authMechanism='PLAIN') result = hive_client.query('select * from t_test limit 10') print result hive_client.close()
if __name__ == '__main__': main()
|