python读取hive数据最佳实践

python读取hive数据最佳实践

使用pyhive库来连接hive server2提供的对外接口,使用sql语句来对数据进行查询,并处理返回结果。

安装依赖:

yum install python-pip gcc gcc-c++ python-virtualenv cyrus-sasl-devel
pip install pyhive
pip install thift
pip install sasl
pip install thrift-sasl

目前大部分公司都使用cdh来安装整个hadoop的处理平台,可以在cloudera manager中查看HiveServer2的配置。

代码如下:

# -*- coding: utf-8 -*-
from pyhive import hive

conn = hive.Connection(host='HiveServer2 host', port=10000, username='hdfs', database='default')
cursor = conn.cursor()
cursor.execute('select * from demo_table limit 10')
for result in cursor.fetchall():
    print result

参考:

https://github.com/dropbox/PyHive

猜你喜欢

转载自blog.csdn.net/lusic01/article/details/89574868