python 访问 hive2

来源:互联网 发布:linux输入法 编辑:程序博客网 时间:2024/05/24 23:14

HiveServer2为客户端在远程执行hive查询提供了接口,通过Thrift RPC来实现,还提供了多用户并发和认证功能。目前使用python的用户可以通过pyhs2这个模块来连接HiveServer2,实现查询和取回结果的操作。

hive_client.py

#!/usr/bin/env python#coding:utf-8import pyhs2''' hive client'''class HiveClient:    '''    docstring for HiveClient    '''    def __init__(self, db_host, user, password, database,  port=10000, authMechanism="PLAIN"):        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 queryNoResult(self,sql):        '''        query no result        '''        with self.conn.cursor() as cursor:            cursor.execute(sql)    def close(self):        '''        close connection        '''        self.conn.close()

hive_conf.py

#!/usr/bin/env python#coding:utf-8import sysimport osclass HiveConnAttribute:    def getHost(self):        return '**'    def getUser(self):        return '**'    def getPasswd(self):        return '**'    def getDatabase(self):        return '**'    def getPort(self):        return 10000    def getAuthMechanism(self):        return 'PLAIN'

query.py

#!/usr/bin/env python#coding:utf-8from hive_client import HiveClientfrom hive_conf import HiveConnAttributeif __name__ == '__main__':    hiveConn = HiveConnAttribute()    hiveHost = hiveConn.getHost()    hiveUser = hiveConn.getUser()    hivePasswd = hiveConn.getPasswd()    hiveDatabase = hiveConn.getDatabase()    hivePort = hiveConn.getPort()    hiveAuthMechanism = hiveConn.getAuthMechanism()    hive_client = HiveClient(db_host = hiveHost, port = hivePort, user = hiveUser, password = hivePasswd,       database = hiveDatabase, authMechanism=hiveAuthMechanism)    hql = '''.........'''    hive_client.query(hql)    hive_client.queryNoResult(hql)    hive_client.close()
原创粉丝点击