Python与HiveServer2连接

1.开启metastore和hiveserver2服务

$hive --service metastore &
$hive --service hiveserver2 &

2.beeline调试,远程连接到HiveServer2

$cd {HIVE_HOME}/bin

$./beeline -u connect jdbc:hive2://localhost:10000

3.python连接hiveserver2代码

[python] view plain copy
# -*- coding: utf-8 -*-  

import pyhs2  
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=10008,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()  
if __name__ == '__main__':  
    hive_client = HiveClient(db_host='localhost',port=10000,user='user——name',password='passswd',database='default', authMechanism='PLAIN')  
    sql = "show tables"  
    result = hive_client.query(sql)  
    print result  
    hive_client.close()  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值