python连接hive

环境 python 2.7.18

pyhive

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
https://www.cnblogs.com/hider/p/16279747.html
# Liunx系统
pip install sasl
pip install thrift
pip install thrift-sasl
pip install PyHive

pip install pandas

# Windows系统会出现莫名其妙的报错
"""

from pyhive import hive
import pandas as pd

# def read_jdbc(host, port, database: str, table: str, query_sql: str) -> DataFrame:
def read_jdbc(host, port, database, table, query_sql) :
    # 1、连接hive服务端
    conn = hive.Connection(
        host=host, port=10000, database=database)
    cursor = conn.cursor()
    # logger.info('connect hive successfully.')

    # 2、执行hive sql
    cursor.execute(query_sql)
    # logger.info('query hive table successfully.')

    # 3、返回pandas.dataframe
    table_len = len(table) + 1
    columns = [col[0] for col in cursor.description]
    col = list(map(lambda x: x[table_len:], columns))
    result = cursor.fetchall()
    # 关闭连接 释放资源
    cursor.close()
    conn.close()

    return pd.DataFrame(result, columns=col)


if __name__ == "__main__":
    # print(__name__)
    host = '***'
    port = '10000'
    database = 'tdxdb'
    table = ''
    query_sql = 'select * from tdxdb.t_enterprise limit 2'
    
    rs = read_jdbc(host, port, database, table, query_sql)
    print(rs)

impala

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
pip install pure-sasl
pip install thrift_sasl==0.2.1 --no-deps
pip install thrift==0.9.3
pip install thriftpy
pip install impyla

# Windows系统会出现莫名其妙的报错
"""

from impala.dbapi import connect

 
conn = connect(host='***', port=10000, auth_mechanism='PLAIN', user='hdfs', database='default')
cursor = conn.cursor()
cursor.execute('show tables')
for row in cursor:
    print(row)

# 关闭连接
cursor.close()
conn.close()

HiveServer2 

# 使用 Python 代码来连接 HiveServer2
https://www.tencentcloud.com/zh/document/product/1026/31148
pip install pyhs2

暂未成功
    compilation terminated.
    error: command 'gcc' failed with exit status 1
    ----------------------------------------
ERROR: Command errored out with exit status 1: /usr/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-0YDrAQ/sasl/setup.py'"'"'; __file__='"'"'/tmp/pip-install-0YDrAQ/sasl/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-ukxUok/install-record.txt --single-version-externally-managed --compile --install-headers /usr/local/python2.7.18/include/python2.7/sasl Check the logs for full command output.
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值