Yellow 就怕你碌碌无为,还安慰自己平凡可贵 -------------yolo

python3.7 利用pyhive 连接上hive(亲测可用)

来python爬虫中,经常会遇到数据的存储问题,如果有大量数据,hive存储是个不错的选择。

那么python如何来连接hive呢?网上有各种教程但是都不是很好用,亲自测试pyhive可用

要求:可用的hive环境  python3++  hive环境必须要安装hiveserver2(

HiveServer是一种可选服务,允许远程客户端可以使用各种编程语言向Hive提交请求并检索结果。HiveServer是建立在Apache ThriftTM(http://thrift.apache.org/) 之上的,因此有时会被称为Thrift Server,这可能会导致混乱,因为新服务HiveServer2也是建立在Thrift之上的.自从引入HiveServer2后,HiveServer也被称为HiveServer1。

)

下载需求包

pip install sasl

pip install thrift

pip install thrift-sasl

pip install PyHive

 

连接hive 注意端口 这里是hiveserver2的端口 默认为10000

from pyhive import hive
conn = hive.Connection(host='10.8.13.120', port=10000, username='hdfs', database='default')
cursor = conn.cursor()
cursor.execute('show tables')

for result in cursor.fetchall():
    print(result)

 可能会有报错

thrift.transport.TTransport.TTransportException: Could not start SASL: b'Error in sasl_client_start (-4) SASL(-4): no mechanism available: No worthy mechs found'

解决办法

yum install cyrus-sasl-devel cyrus-sasl-gssapi cyrus-sasl-md5 cyrus-sasl-plain

  

 

 

WINDOS篇  参考 https://ask.hellobi.com/blog/ysfyb/18251

注意 WINDOWS 用pyhive会有问题,且目前无法解决。  所以选择

impala

python3 通过ldap连接impala
python3.7 对impala支持不好
pip3 install impyla==0.15a1
  报错 ModuleNotFoundError: No module named 'thrift_sasl'
  pip3 install thrift_sasl
 
from impala.dbapi import connect
host='10.0.5.66'
username='hadoop'
password='2Ghlmcl'
port=21050
data_base_name='zftest'
db_connection = connect(host=host, port=port, user=username, password=password, database=data_base_name, auth_mechanism='LDAP')
cursor = db_connection.cursor()
cursor.execute('show databases')
print(cursor.fetchall())
cursor.close()
db_connection.close()

 

 

posted @ 2019-06-11 21:59  zfno11  阅读(28406)  评论(0编辑  收藏  举报