python-pyhs2

#!/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()

转载于:https://www.cnblogs.com/fengzzi/p/10044222.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值