使用python来操作hive(通过 pyhs2 和 HiveServer2)
2016-05-09 09:32
549 查看
HiveServer2为客户端在远程执行hive查询提供了接口,通过Thrift RPC来实现,还提供了多用户并发和认证功能。目前使用python的用户可以通过pyhs2这个模块来连接HiveServer2,实现查询和取回结果的操作。
pyhs2的项目托管在github之上,地址为https://github.com/BradRuderman/pyhs2
可通过以下方式来安装:
easy_install pyhs2
如果安装不成功,可以尝试先安装以下的组件:
yum install cyrus-sasl-plain
yum install cyrus-sasl-devel
以下为一段测试用的代码,pyhs2提供了基本的功能,查询输出的结果为list,做点每天的定时任务用这个来写脚本还是很方便的:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# hive util with hive server2
"""
@author:knktc
@create:2014-04-08 16:55
"""
__author__ = 'knktc'
__version__ = '0.1'
import pyhs2
class HiveClient:
def __init__(self, db_host, user, password, database, port=10000, 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()
def main():
"""
main process
@rtype:
@return:
@note:
"""
hive_client = HiveClient(db_host='hiveserver2.hadoop', port=10000, user='hdfs', password='mypass',
database='test_log', authMechanism='PLAIN')
result = hive_client.query('select * from t_test limit 10')
print result
hive_client.close()
if __name__ == '__main__':
main()
pyhs2的项目托管在github之上,地址为https://github.com/BradRuderman/pyhs2
可通过以下方式来安装:
easy_install pyhs2
如果安装不成功,可以尝试先安装以下的组件:
yum install cyrus-sasl-plain
yum install cyrus-sasl-devel
以下为一段测试用的代码,pyhs2提供了基本的功能,查询输出的结果为list,做点每天的定时任务用这个来写脚本还是很方便的:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# hive util with hive server2
"""
@author:knktc
@create:2014-04-08 16:55
"""
__author__ = 'knktc'
__version__ = '0.1'
import pyhs2
class HiveClient:
def __init__(self, db_host, user, password, database, port=10000, 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()
def main():
"""
main process
@rtype:
@return:
@note:
"""
hive_client = HiveClient(db_host='hiveserver2.hadoop', port=10000, user='hdfs', password='mypass',
database='test_log', authMechanism='PLAIN')
result = hive_client.query('select * from t_test limit 10')
print result
hive_client.close()
if __name__ == '__main__':
main()
相关文章推荐
- python读取大文件并逐行写入另外一个文件
- wxPython-创建一个最小的空的wxPython程序
- python+scapy 抓包与解析
- ubuntu安装python3.5
- PyCharm使用技巧归纳
- Python中new方法的应用
- python基础
- AWS 命令行界面 + Python 的 AWS 开发工具包 (Boto3)
- 利用pyqt4写Python的图形化界面的笔记
- python学习——python中的文件处理之open()、file()函数
- python学习——python os.path模块常用方法详解
- python学习——函数strip() 与 split()
- mac 安装python-magic
- Python中的yield
- Python脚本备份数据库
- python浓缩(20)
- python浓缩(21)
- QColor中的预定义颜色
- python常用模块
- 利用python爬取58同城简历数据