蟒蛇接口

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

在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许可协议,并在请求中注意这一点。
  • 更改必须附带测试,除了一些琐碎的事情,比如修复注释。有关测试环境设置,请参见.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第三方库


热门话题
arraylist IndexOutOfBoundsException预防措施不起作用[Java]   java在多个字段上设置唯一约束   java Sonar没有显示测试覆盖范围   java有没有一种直接从我的应用程序发送反馈的方法?   java如何将字符串中的两个值相加?   优先级队列Java TreeSet使用的内存是否比PriorityQueue少?   java如何合并两个参数相同的XML文件?   java RxJava过期订阅   编译器优化反编译工具修改的源代码。java中的类文件   Java Swing GUI按钮和文本字段不会出现。这是一个包含Jbutton和JTextField的测试代码   java如何在stringBuilder中存储日志   swing获取Java中的屏幕大小   使用PDFBox和Swing显示嵌入式字体的java   在Java中打印1到100之间的小数字   数据结构如何在Java中创建数组、ArrayList、堆栈和队列?   java Hibernate onetomany外键   java如何从Groovy中没有“${str}”的字符串创建GString   在Android Studio(java)中创建巨大网格   Java中如何将字符串值转换为布尔值