Python接口到Hive
项目描述
项目目前不受支持
PyHive
PyHive是Python DB-API 和 SQLAlchemy 接口集合,用于 Presto 、 Hive 和 Trino。
用法
DB-API
from pyhive import presto # or import hive or import trino
cursor = presto.connect('localhost').cursor() # or use hive.connect or use trino.connect
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)
SQLAlchemy
首先安装此包,并将其与SQLAlchemy注册,参见 setup.py 中的 entry_points。
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+pyhive://localhost:8080/hive/default')
# Hive
engine = create_engine('hive://localhost:10000/default')
# SQLAlchemy < 2.0
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()
# SQLAlchemy >= 2.0
metadata_obj = MetaData()
books = Table("books", metadata_obj, Column("id", Integer), Column("title", String), Column("primary_author", String))
metadata_obj.create_all(engine)
inspector = inspect(engine)
inspector.get_columns('books')
with engine.connect() as con:
data = [{ "id": 1, "title": "The Hobbit", "primary_author": "Tolkien" },
{ "id": 2, "title": "The Silmarillion", "primary_author": "Tolkien" }]
con.execute(books.insert(), data[0])
result = con.execute(text("select * from books"))
print(result.fetchall())
注意:查询生成功能可能并不完整或经过全面测试,但应该不会对原始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+pyhive://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'},
)
需求
使用以下命令进行安装
pip install 'pyhive[hive]' 或 pip install 'pyhive[hive_pure_sasl]' 用于Hive接口
pip install 'pyhive[presto]' 用于Presto接口
pip install 'pyhive[trino]' 用于Trino接口
注意:'pyhive[hive]'附加组件使用的是不兼容Python 3.11的sasl,请参阅github问题。因此,PyHive还通过额外的附加组件'pyhive[hive_pure_sasl]'支持纯-sasl,该附加组件支持Python 3.11。
PyHive与以下版本兼容
Python 2.7 / Python 3
对于Presto:Presto安装
对于Trino:Trino安装
对于Hive:HiveServer2守护程序
变更日志
贡献
请在https://opensource.dropbox.com/cla/上填写Dropbox贡献者许可协议,并在您的pull请求中注明。
更改必须附带测试,除非是像修复注释这样的简单事情。请参阅.travis.yml以获取测试环境设置。
关于项目范围的一些说明
该项目旨在成为最小的Hive/Presto客户端,只做那件事,不做其他。可以在PyHive之上实现的功能,例如与您最喜欢的数据分析库的集成,可能超出了范围。
我们更倾向于拥有少量通用功能,而不是大量专业且不灵活的功能。例如,Presto代码接受任意的requests_session参数来定制HTTP调用,而不是为每个requests选项都有单独的参数/分支。
测试环境设置提示
您可以通过遵循此存储库中的.travis.yaml来设置测试环境。它使用Cloudera的CDH 5,这需要用户名和密码才能下载。可能不是每个人都能够获得这些凭据。因此,以下提供替代说明来设置测试环境。
您可以克隆此存储库,该存储库具有Presto和Hive的Docker Compose设置。您可以将以下行添加到其docker-compose.yaml中,以在同一环境中启动Trino
trino: image: trinodb/trino:351 ports: - "18080:18080" volumes: - ./trino:/etc/trino
注意:在上述Docker卷中定义的./trino是来自PyHive存储库的trino配置
- 然后运行:
docker-compose up -d
测试
在具有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的版本。
项目详细信息
PyHive-0.7.0.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 585beff9578a61b99aed47140fec97e26323e8c685a5b5d0c8550a8ebf8a24e0 |
|
MD5 | 94fe30ee618df449105d4b2f04ec3a55 |
|
BLAKE2b-256 | f6ec5c658b3a4d99a6d9145030cc8e003c3f7efc668d866e88544812ab0af310 |