Hive 的 Python 接口
项目描述
PyHive
PyHive 是Presto和 Hive的 Python DB-API和 SQLAlchemy接口的集合。
用法
数据库API
from pyhive import presto # or import hive or import trino
cursor = presto.connect('localhost').cursor()
cursor.execute('SELECT * FROM my_awesome_data LIMIT 10')
print cursor.fetchone()
print cursor.fetchall()
DB-API(异步)
from pyhive import hive
from TCLIService.ttypes import TOperationState
cursor = hive.connect('localhost').cursor()
cursor.execute('SELECT * FROM my_awesome_data LIMIT 10', async=True)
status = cursor.poll().operationState
while status in (TOperationState.INITIALIZED_STATE, TOperationState.RUNNING_STATE):
logs = cursor.fetch_logs()
for message in logs:
print message
# If needed, an asynchronous query can be cancelled at any time with:
# cursor.cancel()
status = cursor.poll().operationState
print cursor.fetchall()
在 Python 3.7 中async成为关键字;你可以使用async_代替:
cursor.execute('SELECT * FROM my_awesome_data LIMIT 10', async_=True)
SQL炼金术
首先安装此包以将其注册到 SQLAlchemy(请参阅setup.py)。
from sqlalchemy import *
from sqlalchemy.engine import create_engine
from sqlalchemy.schema import *
# Presto
engine = create_engine('presto://localhost:8080/hive/default')
# Trino
engine = create_engine('trino://localhost:8080/hive/default')
# Hive
engine = create_engine('hive://localhost:10000/default')
logs = Table('my_awesome_data', MetaData(bind=engine), autoload=True)
print select([func.count('*')], from_obj=logs).scalar()
# Hive + HTTPS + LDAP or basic Auth
engine = create_engine('hive+https://username:password@localhost:10000/')
logs = Table('my_awesome_data', MetaData(bind=engine), autoload=True)
print select([func.count('*')], from_obj=logs).scalar()
注意:查询生成功能并非详尽无遗或经过全面测试,但原始 SQL 应该没有问题。
传递会话配置
# DB-API
hive.connect('localhost', configuration={'hive.exec.reducers.max': '123'})
presto.connect('localhost', session_props={'query_max_run_time': '1234m'})
trino.connect('localhost', session_props={'query_max_run_time': '1234m'})
# SQLAlchemy
create_engine(
'presto://user@host:443/hive',
connect_args={'protocol': 'https',
'session_props': {'query_max_run_time': '1234m'}}
)
create_engine(
'trino://user@host:443/hive',
connect_args={'protocol': 'https',
'session_props': {'query_max_run_time': '1234m'}}
)
create_engine(
'hive://user@host:10000/database',
connect_args={'configuration': {'hive.exec.reducers.max': '123'}},
)
# SQLAlchemy with LDAP
create_engine(
'hive://user:password@host:10000/database',
connect_args={'auth': 'LDAP'},
)
要求
安装使用
用于 Hive 接口的pip install 'pyhive[hive]'和
pip install 'pyhive[presto]'用于 Presto 界面。
Trino接口的pip install 'pyhive[trino]'
PyHive 与
蟒蛇 2.7 / 蟒蛇 3
对于 Presto:Presto 安装
对于 Trino:安装 Trino
对于 Hive:HiveServer2守护进程
变更日志
贡献
请在https://opensource.dropbox.com/cla/填写 Dropbox 贡献者许可协议,并在您的拉取请求中注明。
更改必须伴随着测试,除了一些琐碎的事情,比如修复评论。有关测试环境设置,请参阅 .travis.yml。
项目范围说明:
这个项目旨在成为一个最小的 Hive/Presto 客户端,它只做一件事而不做其他事情。可以在 PyHive 之上实现的功能,例如与您最喜欢的数据分析库的集成,可能超出了范围。
我们更喜欢拥有少量的通用特性,而不是大量专门的、不灵活的特性。例如,Presto 代码采用任意requests_session参数来自定义 HTTP 调用,而不是为每个请求选项提供单独的参数/分支。
测试
在带有 Hive/Presto 的环境中运行以下命令:
./scripts/make_test_tables.sh virtualenv --no-site-packages env source env/bin/activate pip install -e . pip install -r dev_requirements.txt py.test
警告:这会删除/创建名为one_row、one_row_complex和many_rows的表,以及一个名为pyhive_test_database的数据库。
更新 TCLIService
TCLIService 模块是使用TCLIService.thrift文件自动生成的。要更新它, 可以使用generate.py文件: python generate.py <TCLIServiceURL>。留空时,将下载 Hive 2.3 的版本。
项目详情
下载文件
下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。
源分布
内置分布
Acryl- PyHive -0.6.13.tar.gz 的哈希值
| 算法 | 哈希摘要 | |
|---|---|---|
| SHA256 | 8a5340d6f8f83690dbe9d5c7159c6933a205d6c1fb8f886f1f7aa6e66986dbd0 |
|
| MD5 | 63266a8a87358208e9984b8b0bc94e90 |
|
| 布莱克2-256 | b3716aef0444527dbf925f02e39b4328b69e7172712f0b427e6e994a72db7318 |
acryl_PyHive -0.6.13-py3-none-any.whl 的哈希值
| 算法 | 哈希摘要 | |
|---|---|---|
| SHA256 | a46c793be3e0f483969c408b6efead6a2163b9ea12a05b34e012e799d9a1c025 |
|
| MD5 | 361462fe1dac990ef40bba23a9199864 |
|
| 布莱克2-256 | f1d127f530f69576afba582d994d1ec0efd4a671941d4bb5346e9de76a701b0f |