蟒蛇接口

HerePyHive的Python项目详细描述


https://travis-ci.org/dropbox/PyHive.svg?branch=masterhttps://img.shields.io/codecov/c/github/dropbox/PyHive.svg

蜂窝

pyhive是pythonDB-APISQLAlchemy用于PrestoHive

使用量

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()

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许可协议,并在请求中注意这一点。
  • 更改必须附带测试,除了一些琐碎的事情,比如修复注释。有关测试环境设置,请参见.travis.yml。
  • 项目范围说明:
    • 这个项目旨在成为一个最小的hive/presto客户机,它只做一件事而不做其他事情。 可以在pyhive之上实现的特性,例如与您喜爱的数据分析库的集成,可能超出了范围。
    • 我们倾向于在大量专业化的、不灵活的特征上拥有少量的通用特征。 例如,presto代码使用任意的requests_session参数定制http调用,而不是为每个requests选项使用单独的参数/分支。

测试

https://travis-ci.org/dropbox/PyHive.svghttp://codecov.io/github/dropbox/PyHive/coverage.svg?branch=master

在具有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_rowone_row_complexmany_rows的表,并加上 数据库名为pyhive_test_database

更新tcliservice

tcliservice模块是使用TCLIService.thrift文件自动生成的。为了更新它, generate.py文件可以使用:python generate.py <TCLIServiceURL>。当留空时, 将下载配置单元2.3的版本。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何显示因用户而异的SQLite存储数据?   转换java。sql。将映射传递给Jackon的valueToTree方法时StringNode的时间戳   从java中的列表json获取值   unicode Java字符存储在什么编码中?   java如何让Spring数据存储库中的默认方法命中缓存?   java使用readClassDescriptor()和resolveClass()来允许序列化版本控制   数组通过另一个矩阵的一部分填充矩阵   如果包含使用正则表达式的字符串,则替换父XML标记的java   java清除SharedReference中的单个变量   java将变量值从一个jsp页面传输到另一个jsp页面   java JDBC+SQLite:DriveManager不加载所需的驱动程序   相同源代码的java Kotlin构建生成不同的二进制文件   Java中的元组枚举