python訪問hive2
HiveServer2為客戶端在遠程執行hive查詢提供了接口,通過Thrift RPC來實現,還提供了多用戶並發和認證功能。目前使用python的用戶可以通過pyhs2這個模塊來連接HiveServer2,實現查詢和取回結果的操作。
1.安裝pyhs2
pip install pyhs2 yum install cyrus-sasl-plain yum install cyrus-sasl-devel
yum install ython-devel.x86_64
yum install cyrus-sasl-devel.x86_64
#如果有報錯根據提示處理就行了,比較簡單
2.實例展示
以下為一段小實例的代碼,pyhs2提供了基本的功能,查詢輸出的結果為list,再將list的內容寫入到exel里面,我要根據每個sql語句寫入到對應的sheet中,設計到20多個,還有目前都是寫入到了代碼中,一些配置文件可以寫到configparser配置文件中
#!/usr/bin/env python # -*- coding: utf-8 -*- # hive util with hive server2 """ @author:wyf @create:2016-06-29 16:55 """ __author__ = 'wyf' __version__ = '0.1' import pyhs2 import xlrd import xlwt import sys default_encoding = 'utf-8' if sys.getdefaultencoding() != default_encoding: reload(sys) sys.setdefaultencoding(default_encoding) 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 writeXlwt(filename,result): book=xlwt.Workbook() #打開一個工作薄 sheet1=book.add_sheet('sheel1')#添加一個sheet頁 for i in range(len(result)+1): if i ==0: sheet1.row(i).write(0,'日期') sheet1.row(i).write(1,'小時') sheet1.row(i).write(2,'樓層') sheet1.row(i).write(3,'店鋪號') sheet1.row(i).write(4,'店鋪名稱') sheet1.row(i).write(5,'人數') else: for a in range(len(result[i-1])): sheet1.row(i).write(a,result[i-1][a]) book.save(filename) def main(): """ main process """ try: hive_client = HiveClient(db_host='192.168.14.44', port=10000, user='hive', password='hive', database='test', authMechanism='PLAIN') sql = 'select * from test limit 10'#實例sql語句 result = hive_client.query(sql) hive_client.close() except pyhs2.error, tx: print '%s' % (tx.message) sys.exit(1) writeXlwt('test.xls',result)
if __name__ == '__main__': main()