蟒蛇接口
PyHive的Python项目详细描述
蜂窝
pyhive是pythonDB-API和 SQLAlchemy用于Presto和 Hive。
使用量
DB-API
frompyhiveimportpresto# or import hivecursor=presto.connect('localhost').cursor()cursor.execute('SELECT * FROM my_awesome_data LIMIT 10')printcursor.fetchone()printcursor.fetchall()
DB-API(异步)
frompyhiveimporthivefromTCLIService.ttypesimportTOperationStatecursor=hive.connect('localhost').cursor()cursor.execute('SELECT * FROM my_awesome_data LIMIT 10',async=True)status=cursor.poll().operationStatewhilestatusin(TOperationState.INITIALIZED_STATE,TOperationState.RUNNING_STATE):logs=cursor.fetch_logs()formessageinlogs:printmessage# If needed, an asynchronous query can be cancelled at any time with:# cursor.cancel()status=cursor.poll().operationStateprintcursor.fetchall()
在python 3.7中,async变成了一个关键字;您可以使用async代替:
cursor.execute('SELECT * FROM my_awesome_data LIMIT 10',async_=True)
sql炼金术
首先安装这个包以向sqlalchemy注册它(请参见setup.py)。
fromsqlalchemyimport*fromsqlalchemy.engineimportcreate_enginefromsqlalchemy.schemaimport*# Prestoengine=create_engine('presto://localhost:8080/hive/default')# Hiveengine=create_engine('hive://localhost:10000/default')logs=Table('my_awesome_data',MetaData(bind=engine),autoload=True)printselect([func.count('*')],from_obj=logs).scalar()
注意:查询生成功能不是详尽的,也不是完全测试过的,但是应该没有 原始SQL有问题。
传递会话配置
# DB-APIhive.connect('localhost',configuration={'hive.exec.reducers.max':'123'})presto.connect('localhost',session_props={'query_max_run_time':'1234m'})# SQLAlchemycreate_engine('presto://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 LDAPcreate_engine('hive://user:password@host:10000/database',connect_args={'auth':'LDAP'},)
要求
使用安装
- pip install pyhive[hive]用于配置单元接口和
- pip install pyhive[presto]用于presto接口。
pyhive与
- python 2.7/python 3
- 对于presto:presto安装
- 对于配置单元:HiveServer2守护进程
贡献
- 请在https://opensource.dropbox.com/cla/填写Dropbox Contributor许可协议,并在请求中注意这一点。
- 项目范围说明:
- 这个项目旨在成为一个最小的hive/presto客户机,它只做一件事而不做其他事情。 可以在pyhive之上实现的特性,例如与您喜爱的数据分析库的集成,可能超出了范围。 我们倾向于在大量专业化的、不灵活的特征上拥有少量的通用特征。 例如,presto代码使用任意的requests_session参数定制http调用,而不是为每个requests选项使用单独的参数/分支。
测试
在具有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>。当留空时, 将下载配置单元2.3的版本。