python通过thrift方式连接hive
2016-04-29 00:00
567 查看
hive安装完成后,如果只是本地使用,启用
如果是要脚本调用,则需要启用HiveServer2,确保10000端口已经被监听(可在hive-site.xml中修改端口)
HiveServer2为客户端在远程执行hive查询提供了接口,通过Thrift RPC来实现,还提供了多用户并发和认证功能。目前python可以通过pyhs2这个模块来连接HiveServer2,实现查询和取回结果的操作。
不过pyhs2已经不在维护,追新的可以参考另外2个很好的python package(已经被证明pyhs2存在性能瓶颈,最好尽快切换到pyhive)
https://github.com/dropbox/PyHive
https://github.com/cloudera/impyla
pyhs2的项目托管在github之上,地址为https://github.com/BradRuderman/pyhs2或在https://pypi.python.org/pypi/pyhs2/0.2直接下载
如果安装不成功,可以尝试先安装以下的组件:
yum install cyrus-sasl-plain
yum install cyrus-sasl-devel
安装时如果遇到报错:
可以尝试先安装sasl , ubantu可以用sudo apt-get install libsasl2-dev, CentOS可以使用anaconda的pip安装, 或者按照以下步骤安装:
nohup hive --service metastore & [hadoop@master1 usr]$ hive Logging initialized using configuration in file:/data/usr/hive/conf/hive-log4j.properties hive> use fmcm; OK Time taken: 0.874 seconds
如果是要脚本调用,则需要启用HiveServer2,确保10000端口已经被监听(可在hive-site.xml中修改端口)
nohup hive --service hiveserver2 & [hadoop@master1 usr]$ netstat -an|grep 10000 tcp 0 0 0.0.0.0:10000 0.0.0.0:* LISTEN
HiveServer2为客户端在远程执行hive查询提供了接口,通过Thrift RPC来实现,还提供了多用户并发和认证功能。目前python可以通过pyhs2这个模块来连接HiveServer2,实现查询和取回结果的操作。
不过pyhs2已经不在维护,追新的可以参考另外2个很好的python package(已经被证明pyhs2存在性能瓶颈,最好尽快切换到pyhive)
https://github.com/dropbox/PyHive
https://github.com/cloudera/impyla
安装sasl失败的话,先安装: yum install gcc-c++ python-devel.x86_64 cyrus-sasl-devel.x86_64
pyhs2的项目托管在github之上,地址为https://github.com/BradRuderman/pyhs2或在https://pypi.python.org/pypi/pyhs2/0.2直接下载
如果安装不成功,可以尝试先安装以下的组件:
yum install cyrus-sasl-plain
yum install cyrus-sasl-devel
安装时如果遇到报错:
error: sasl/sasl.h: No such file or directory
可以尝试先安装sasl , ubantu可以用sudo apt-get install libsasl2-dev, CentOS可以使用anaconda的pip安装, 或者按照以下步骤安装:
curl -O -L ftp://ftp.cyrusimap.org/cyrus-sasl/cyrus-sasl-2.1.26.tar.gz tar xzf cyrus-sasl-2.1.2.26.tar.gz cd cyrus-sasl-2.1.26.tar.gz ./configure && make install 最后附上测试代码:
# -*- coding:utf-8 -*- ''' 采用Hive和thrift方式连接数据库 ''' import pyhs2 import sys reload(sys) sys.setdefaultencoding('utf8') class HiveClient: def __init__(self, db_host, user, password, database, port=10000, authMechanism="PLAIN"): self.conn = pyhs2.connect(host=db_host, port=port, authMechanism=authMechanism, user=user, password=password, database=database, &n 7fe0 bsp; ) def query(self, sql): with self.conn.cursor() as cursor: cursor.execute(sql) return cursor.fetch() def close(self): self.conn.close() def main(): """ main process @rtype: @return: @note: """ hive_client = HiveClient(db_host='10.24.33.3', port=10000, user='hadoop', password='hadoop', database='fmcm', authMechanism='PLAIN') result = hive_client.query('select * from fm_news_newsaction limit 10') print result hive_client.close() if __name__ == '__main__': main()
相关文章推荐
- python中的Queue与多进程(multiprocessing)
- 利用Python的PIL库进行简单的图像处理
- Python基础(一)
- Python排序算法之直接插入排序
- Ubuntu下使用Eclipse和PyDev搭建完美Python开发环境
- Python 帮助使用
- Python 初学者容易犯的错误
- python 子包引用父包和其他子包
- python 子包引用父包和其他子包
- python rforward.py
- 使用ArcPy对栅格数据进行批量投影坐标转换
- python基础之字符串格式化
- 关于Python的super用法研究
- python学习之argparse模块
- python import模块出错
- Python 字典 dictionary changed size during iteration
- python写文件illegal multibyte sequence问题
- python nosetests
- python get操作
- python 数据库