Python连接Hive

1.在使用Python连接hive之前,需要将hive安装包下的lib/py中的文件拷贝到python的sys.path中的site-packages下,否则引入对应的包会报错,这个是使用hive提供的Python接口来调用hive客户端。


2 启动hive 的thrift

确保以下服务开启:
hive --service hiveserver
默认端口是10000

from hive_service import ThriftHive
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol

def ReadHiveTest(sql):
    try:
        tSocket = TSocket.TSocket('172.18.1.88',10000)
        tTransport = TTransport.TBufferedTransport(tSocket)
        protocol = TBinaryProtocol.TBinaryProtocol(tTransport)
        client = ThriftHive.Client(protocol)
        tTransport.open()
        client.execute(sql)
        return client.fetchAll()
    except Thrift.TException, tx:
        print '%s' % (tx.message)
    finally:
        tTransport.close()

if __name__ == '__main__':
    showDatabasesSql = 'show databases'
    showTablesSql = 'show tables'
    selectSql = 'SELECT * FROM 07_jn_mysql_2'
    result = ReadHiveTest(selectSql)
    print(result[1])
















 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值